c# - 导航 QueryString 与 PhoneApplicationService.Current.State

标签 c# windows-phone-7 windows-phone-8

我正在为我的 Windows Phone 8 应用程序使用 NavigationContext.QueryString。例如,我在导航字符串中设置了一个 URI 标识符,如 ItemId,在 OnNavigatedTo 中,我解析 Id 并通过 linq 读取 Item。

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        try
        {
            int itemId = int.Parse(NavigationContext.QueryString["itemId"]);

            _item = App.MainViewModel.GetItem(itemId);

            DataContext = _item;
        }
        catch (KeyNotFoundException ex)
        {
            Debug.WriteLine(ex.Message);
            throw;
        }
    }

我找到了一个有趣的替代方案,想听听您的意见:

// in the calling page
PhoneApplicationService.Current.State["Item"] = App.MainViewModel.GetItem(123);

// in the destination page
Item item = PhoneApplicationService.Current.State["Item"] as Item;

这真的是推荐的方式吗?

最佳答案

来自 MSDN :

The PhoneApplicationService class provides access to various aspects of the application’s lifetime. This includes management of the application’s idle behavior and management of the application’s state when it becomes active or inactive. You can use it like that but the data has to be serializable.

link还有其他共享数据的方式,但我认为状态不是推荐的方式。它更多的是用于墓碑目的。

关于c# - 导航 QueryString 与 PhoneApplicationService.Current.State,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14266495/

相关文章:

c# - 如果有人提交了一个 'malware' 的 nuget 包怎么办?

c# - 如何在 Windows Phone 7 中以编程方式检索下载的文件?

c# - 如何创建 shelltoast?

windows-phone-7 - 如何在 Windows Phone 7 上安装根证书?

python - Windows Phone 是否支持脚本?

html - Html/Css 上的透明磁贴,如 Windows Phone 8

silverlight - 使用 Style 动态更改 ListBox 方向

c# - `(arg) => true)` 的 WPF 目的

c# - User.Identity.Name 返回 guid

c# - 从文件夹中获取预制件然后转换为列表<GameObject>