Do code after OnNavigatedTo

前端 未结 3 566
孤街浪徒
孤街浪徒 2021-01-24 04:03

I Have Code get IdUsers From Other Page

String IdUsers;

        public Main_Wallets_Page()
        {
            InitializeComponent();            
                     


        
3条回答
  •  有刺的猬
    2021-01-24 04:43

    DO NOT place MessageBox into OnNavigatedTo event. Try to create an empty project with MainPage and Page2. Place button on MainPage to navigate to Page2. In Page2 place MessageBox in OnNavigatedTo event. Then everythig will work fine if you Start Debugging from VS. BUT if you deploy and run it you will see that when you navigate to Page2 you see MessageBox. Then don't do anything, just wait for about 15 sec. MessageBox will react as Canceled and APPLICATION WILL BE CRASHED! without any navigation to Page2 or MainPage. The same thing happens if you use Dispatcher.BeginInvoke around the MessageBox.Show. I assume that OnNavigatedTo event has a timeout which works only when app is deployed. So you should run your MessageBox when Navigation is competed. Everything works if you do

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {
        base.OnNavigatedTo(e);
    
        var lcTimer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 0, 200) };
        lcTimer.Tick += (s2, e2) => {
             (s2 as DispatcherTimer).Stop();
             if (MessageBoxResult.OK == MessageBox.Show("Test, don't push", "", MessageBoxButton.OKCancel))
                 MessageBox.Show("OK");
             else
                 MessageBox.Show("Cancel");
        };
        lcTimer.Start();
    }
    

    Note: If you have some code in OnNavigatedTo run above code at the end of OnNavigatedTo.

    I liked what Austin Thompson(upvote) has adviced with ThreadPool.QueueUserWorkItem. But note that with this approach you need to place MessageBox inside the Dispatcher.BeginInvoke otherwise you will receive cross-thread exception. So the code is the following

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {
        base.OnNavigatedTo(e);
    
        ThreadPool.QueueUserWorkItem((stateInfo) => {
            Dispatcher.BeginInvoke(() => {
                if (MessageBoxResult.OK == MessageBox.Show("Test don't push", "", MessageBoxButton.OKCancel))
                    MessageBox.Show("OK");
                else
                    MessageBox.Show("Cancel");
            });
        });
    }
    

提交回复
热议问题