Win 8 中的所有页面的 Frame 属性是引用同一个地址,为在 App 类中的 Frame 对象

在 App 类中:

       protected override void OnLaunched(LaunchActivatedEventArgs args)
        {
         
            if (args.PreviousExecutionState == ApplicationExecutionState.Running)
            {
                Window.Current.Activate();
                return;
            }

            if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
            }
            

//全局的 Frame 对象 var rootFrame = new Frame(); if (!rootFrame.Navigate(typeof(MainPage))) { throw new Exception("Failed to create initial page"); } //赋值给当前的 Windows 对象 Window.Current.Content = rootFrame; Window.Current.Activate(); }

在 MainPage 页面中,添加一个按钮,并修改 Codebehind 页面:

       //声明一个静态的 MainPage 的对象,引用到当前的对象
        public static MainPage main;

        public MainPage()
        {
            this.InitializeComponent();
            main = this;
        }


        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //导航到第二个页面
            this.Frame.Navigate(typeof(BlankPage1));
        }

在第二个页面中添加一个 TextBlock,显示结果,在 Codebehind 页面中:

       //判断它们的 Frame 对象是否是引用相同的地址
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            txtR.Text = "MainPage.Frame == BlankPage1 ?  Equals :" + this.Frame.Equals(MainPage.main.Frame) 
+ "\r\nReferenceEquals :" + ReferenceEquals(MainPage.main.Frame, this.Frame); txtR.Text += "\r\n\r\nWindow.Current.Content == this.Frame ? ReferenceEquals :" + ReferenceEquals(Window.Current.Content, this.Frame); }

显示结果:

原文地址:https://www.cnblogs.com/hebeiDGL/p/2736935.html