繁体   English   中英

我如何在页面之间传递值? (Windows Phone)

[英]How I can pass values between pages? (Windows Phone)

我知道我可以用uri在页面之间传递值,例如:

NavigationService.Navigate(
new Uri("/DestinationPage.xaml?parameter1=v1", UriKind.Relative)
);

有没有其他方法可以在页面之间传递值?

你可以做一个单身人士;)。 这是一个例子:

    public class MySingleton
    {
            private static MySingleton _mInstance;

            public static MySingleton Instance
            {
                get { return _mInstance ?? (_mInstance = new MySingleton()); }
            }

        /// <summary>
        /// You can send values through this string.
        /// </summary>
        public string SomeString { get; set; }
    }

编辑:要为其分配值,您可以执行以下操作:

MySingleton.Instance.SomeString = "Text";

您可以使用类似的查询字符串值

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

并且值可以传递给as

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

除了在页面uri中传递字符串并使用NavigationContext检索它们之外,没有任何开箱即用的选项!

但是你总是可以在App类中声明可用于所有页面的公共属性!

您可以传递值使用IsolatedStorageSetting,使用isolatedStorage时必须小心,您必须手动清除它,除非您这样做或卸载应用程序/关闭模拟器,否则不会清除它, IsolatedStorage文档

            <Button Content="Next page" Grid.Column="1" Height="23" Tag="/NextPage.xaml"  HorizontalAlignment="Right" Margin="0,6,12,0" Name="btnViewQuestion" VerticalAlignment="Top" Width="100" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM