本文介绍了如何在 Windows Phone 8 中的页面之间传递非字符串参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 Windows 商店应用程序转换为 Windows Phone 8.对于 WinRT,您可以在调用 frame.navigate 时将任何对象作为参数传递.(frame.navigate(type sourcePageType, object parameter))

I'm working on converting a windows store app to windows phone 8. For WinRT, you could pass any object as a parameter when calling frame.navigate. (frame.navigate(type sourcePageType, object parameter))

Windows 手机的导航似乎有所不同,您可以通过调用 uri 进行导航,例如: frame.navigate(new uri("mypage.xaml", UriKind.Relative))

The navigation seems to work differently for windows phone, you navigate by calling into a uri, like: frame.navigate(new uri("mypage.xaml", UriKind.Relative))

windows 文档指出,您可以通过将字符串添加到 uri 来将字符串作为参数传递.

The windows documentation notes that you can pass a string as a parameter by adding it to the uri.

是否有一种可接受的方式在我尚未找到的页面之间传递复杂对象?

Is there an accepted way of passing complex objects between pages that I just haven't found?

推荐答案

我最终扩展了 NavigationService 类,如下所示:

I ended up extending the NavigationService class, like so:

public static class NavigationExtensions
{
    private static object _navigationData = null;

    public static void Navigate(this NavigationService service, string page, object data)
    {
        _navigationData = data;
        service.Navigate(new Uri(page, UriKind.Relative));
    }

    public static object GetLastNavigationData(this NavigationService service)
    {
        object data = _navigationData;
        _navigationData = null;
        return data;
    }
}

然后你会在源页面上调用 NavigationService.Navigate("mypage.xaml", myParameter);,在目标页面的 OnNavigatedTo 方法中调用 var myParameter = NavigationService.GetLastNavigationData(); 获取参数数据.

Then you'd call NavigationService.Navigate("mypage.xaml", myParameter); on the source page, and in the OnNavigatedTo method of the target page call var myParameter = NavigationService.GetLastNavigationData(); to get the parameter data.

这篇关于如何在 Windows Phone 8 中的页面之间传递非字符串参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 10:39