在Windows Phone 7上解析ShellTile上的URI参数

时间:2011-10-21 15:32:28

标签: c# .net silverlight windows-phone-7

我正在开发我的第一个Windows Phone 7应用程序。我对Silverlight,C#和整个.NET场景都很新鲜,但我想我认为我取得了不错的进展。

我从各种代码示例中了解到,我可以使用ShellTile设置磁贴。我知道我可以使用URI传递参数(如下例所示):

ShellTile.Create(new Uri("/MainPage.xaml?DefaultTitle=FromSecondaryTile", UriKind.Relative), tile );

有人能指出我的方向(或解释)我如何处理从瓷砖传递的参数?所以,当瓷砖打开时,我想打开应用程序的某个部分。

为了记录,我知道我可以为每个人创建一个单独的页面来处理它,但我可以看到快速变得凌乱:)

谢谢! 麦克

2 个答案:

答案 0 :(得分:2)

我发现一种适用于我的特定目的的方法,就是在xaml页面之间传递值的方法相同,这只是在查询字符串中传递它们:     NavigationContext.QueryString [ “XXXXX”]的ToString();

XXXXX是键/名称对中的名称。

答案 1 :(得分:1)

您可以将网址设置为单独的页面(例如OtherPage.xaml),也可以使用提供的URI,并更改OnNavigatedTo覆盖中的网页/视图。

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    ...
}

这里NavigationEventArgs将为您提供您提供的导航参数,作为常规字典。从那些,你可以决定做什么。

此外,您可以通过简单的扩展(这里专门针对整数键的重载,因为我个人更喜欢将它们用于标识符)来使生活更轻松。

namespace System.Windows.Navigation
{
    public static class NavigationExtensions
    {
        public static int? TryGetKey(this NavigationContext source, string key)
        {
            if (source.QueryString.ContainsKey(key))
            {
                string value = source.QueryString[key];

                int result = 0;
                if (int.TryParse(value, out result))
                {
                    return result;
                }
            }

            return null;
        }

        public static string TryGetStringKey(this NavigationContext source, string key)
        {
            if (source.QueryString.ContainsKey(key))
            {
                return source.QueryString[key];
            }

            return null;
        }
    }
}