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

发布于 2024-08-26 05:33:14 字数 902 浏览 4 评论 0原文

我在 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。
我还想将值从 MainPage.xaml 传递到另一个页面 SavePage.xaml - 但这有同样的问题 - 我该如何解决这个问题?

I have a Page in Silverlight which is Navigated from the MainPage.xaml, called OpenPage.xaml, I then want to pass a value back to the MainPage.xaml - this OpenPage.xaml is called using this:

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

From the mainpage - this is not a child of the MainPage as the RootVisual is replaced - I can call this:

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

To return to the MainPage - however I need to pass a value from OpenPage.xaml to the MainPage.xaml - how to I access the MainPage instance - I have a Property called Document however when I do this:

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

Or this:

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

I get an invalid cast exception because I think it is trying to cast the OpenPage.xaml to MainPage.xaml - how to I get the NavigatedTo Page, I want to set the property on MainPage.xaml from OpenPage.xaml.
I also want to pass values from the MainPage.xaml to another page SavePage.xaml - but this has the same issue - how do I resolve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

节枝 2024-09-02 05:33:14

使用查询字符串值:-

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)
}

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

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

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

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

Use a query string value:-

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

MainPage can then acquire this value using:-

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

Edit:

In response to comment re passing other types.

Once you have the above inplace you can use other service patterns to pass other types. For example consider a MessageService that implements:-

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

You would then implement this interface and expose the implementation as singleton say:-

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

Your OpenPage calls MessageService.Default.Store and places the resulting Guid in the query string.

The MainPage then tests for the presence of such a query string value, if present uses its value to call MessageService.Default.Retrieve which removes the item from the service.

格子衫的從容 2024-09-02 05:33:14
Partial Public Class MainPage
    Inherits UserControl

    Public Sub New()

        InitializeComponent()

        ContentFrame.Source = New Uri("/About", UriKind.Relative)

        ...............
Partial Public Class MainPage
    Inherits UserControl

    Public Sub New()

        InitializeComponent()

        ContentFrame.Source = New Uri("/About", UriKind.Relative)

        ...............
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文