Do code after OnNavigatedTo

前端 未结 3 570
孤街浪徒
孤街浪徒 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:53

    You shouldn't use MessageBoxes in OnNavigatedTo because if the user does not press a button your app will crash since the framework thinks that navigation has failed. MessageBoxes in the constructor are equally as bad.

    I can think of two options (I use #1 for these sorts of things):

    1. Show the MessageBox in the Loaded event. But be careful it can be fired more than once. In the constructor you might add the handler for the Loaded event and then in the handler you'd detach from the handler so that it is called only once.

    2. Use Dispatcher.BeginInvoke around the MessageBox.Show call so that it does not block navigation. That might still block the Dispatcher thread. If you really wanted to go this route you could use ThreadPool.QueueUserWorkItem or a TPL Task.

    I also have used OnLayoutUpdated in place of the Loaded event but I can't remember exactly why :) It seems like it might have been that the page hasn't yet displayed in Loaded and it has in the other event.

提交回复
热议问题