gpt4 book ai didi

c# - 使用导航时将值从另一个页面传递到 Silverlight 中的 MainPage.xaml?

转载 作者:太空狗 更新时间:2023-10-29 23:11:31 26 4
gpt4 key购买 nike

我在 Silverlight 中有一个从 MainPage.xaml 导航的页面,称为 OpenPage.xaml,然后我想将一个值传递回 MainPage.xaml - 使用此调用此 OpenPage.xaml:

NavigationService.Navigate(new Uri("/OpenPage.xaml", UriKind.Relative));

从主页 - 这不是 MainPage 的子项,因为 RootVisual 已被替换 - 我可以这样称呼:

NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));

要返回 MainPage - 但是我需要将一个值从 OpenPage.xaml 传递到 MainPage.xaml - 如何访问 MainPage 实例 - 我有一个名为 Document 的属性,但是当我这样做时:

        MainPage m = (MainPage)Application.Current.RootVisual;
m.Document = "Hello World";

或者这个:

((MainPage)root).Document = "Hello World";

我收到一个无效的转换异常,因为我认为它正在尝试将 OpenPage.xaml 转换为 MainPage.xaml - 如何获取 NavigatedTo 页面,我想从 OpenPage.xaml 设置 MainPage.xaml 上的属性。< br/>我还想将值从 MainPage.xaml 传递到另一个页面 SavePage.xaml - 但这有同样的问题 - 我该如何解决这个问题?

最佳答案

使用查询字符串值:-

NavigationService.Navigate(new Uri("/MainPage.xaml?value=Hello%20World", UriKind.Relative);

MainPage 然后可以使用以下方法获取此值:-

string value =  this.NavigationContext.QueryString["value"];

编辑:

响应重新传递其他类型的评论。

一旦你有了上面的内容,你就可以使用其他服务模式来传递其他类型。例如,考虑一个 MessageService 实现:-

interface IMessageService
{
Guid Store(object value)
object Retrieve(Guid key)
}

然后您将实现此接口(interface)并将实现公开为单例:-

public class MessageService : IMessageService
{
public static IMessageService Default { // singleton stuff here }
}

您的 OpenPage 调用 MessageService.Default.Store 并将生成的 Guid 放入查询字符串中。

然后 MainPage 测试是否存在这样的查询字符串值,如果存在则使用它的值调用 MessageService.Default.Retrieve 从服务中删除该项目。

关于c# - 使用导航时将值从另一个页面传递到 Silverlight 中的 MainPage.xaml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2500300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com