在OnNavigatedTo之后进行编码 [英] Do code after OnNavigatedTo

查看:126
本文介绍了在OnNavigatedTo之后进行编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码从其他页面获取IdUs​​ers

I Have Code get IdUsers From Other Page

String IdUsers;

        public Main_Wallets_Page()
        {
            InitializeComponent();            
            MessageBox.Show(IdUsers);
        }


protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            String Id;
            if (NavigationContext.QueryString.TryGetValue("IdUsers", out Id))
                IdUsers = Id;
        }

MessageBox始终为空.我想让MessageBox在OnNavigationTo之后显示"IdUsers"(不要将MessageBox放在"OnNavigationTo"中).

The MessageBox Alway be Null. I Want to MessageBox Show the "IdUsers" after OnNavigationTo (Do not put the MessageBox IN "OnNavigationTo").

我该怎么办?

推荐答案

您不应在OnNavigatedTo中使用MessageBoxes,因为如果用户不按按钮,则应用程序将崩溃,因为框架认为导航失败.构造函数中的MessageBox同样糟糕.

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.

我可以想到两个选择(我将#1用于这类事情):

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

  1. Loaded事件中显示MessageBox.但是要小心 解雇不止一次.在构造函数中,您可以为Loaded事件添加处理程序,然后在处理程序中将其与处理程序分离,以便仅调用一次.

  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.

MessageBox.Show调用周围使用Dispatcher.BeginInvoke,以便它不会阻止导航.那可能仍然会阻塞Dispatcher线程.如果您真的想走这条路,可以使用ThreadPool.QueueUserWorkItem TPL任务.

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.

我也已经使用OnLayoutUpdated代替了Loaded事件,但是我不记得确切的原因了:)似乎页面尚未显示在Loaded中并且它已经在另一种情况下.

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.

这篇关于在OnNavigatedTo之后进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆