加载事件未完全加载 [英] loaded event not exactly loaded

查看:29
本文介绍了加载事件未完全加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得很傻,但我就是找不到答案.假设我想在 MainPage 加载时向用户显示一些消息.问题是,如果我在 Loaded 事件 (MessageBox.Show()) 上执行此操作,则在加载我的页面之前会显示弹出窗口,这会给用户留下此消息和黑色背景.这是一种沉闷的情况.任何事件都可以解决问题的想法.还有其他方法,例如后台工作者或 NavigationInTransition_EndTransition 事件,但应该有更优雅的方法吗?

I feel silly, but I just can't find an answer to this. Let's say I want to show some message to the users when the MainPage loads. The problem is that if I do this on Loaded event (MessageBox.Show()), the popup shows just before my page is loaded which leaves the user with this message and a black background. This is kind of dull situation. Any ideas which event can do the trick. There other ways around like background worker or NavigationInTransition_EndTransition event but there should be more elegant way ?

推荐答案

您可以轻松测试所有标准方法.我花的时间比写这篇文章要少.

You can easily test all standard methods. It took me less time than writing this text to do so.

在页面构造函数中:

Loaded += new RoutedEventHandler(TestPage_Loaded);
LayoutUpdated += new EventHandler(TestPage_LayoutUpdated);

相应的方法:

    void TestPage_LayoutUpdated(object sender, EventArgs e)
    {
        Debug.WriteLine("LayoutUpdated");
    }

    void TestPage_Loaded(object sender, RoutedEventArgs e)
    {
        Debug.WriteLine("Loaded");
        Dispatcher.BeginInvoke(() =>
        {
            Debug.WriteLine("Loaded -> BeginInvoke");
        });
    }

向 OnNavigateTo() 添加调试写入.

Add debug write to OnNavigateTo() as well.

如果你的页面和我的一样复杂,你会得到类似的结果:

If your page is similarly complex than mine, then you get something like:

  1. OnNavigatedTo
  2. 布局已更新
  3. 已加载
  4. 布局已更新
  5. 已加载 -> 开始调用
  6. 多次布局更新

Last LayoutUpdated 将是最佳候选者,但如何检测它呢?因此,Loaded -> BeginInvoke"似乎是微不足道的最佳选择.

Last LayoutUpdated would be the best candidate, but how would one detect it? Hence "Loaded -> BeginInvoke" seems to be the best option among the trivial ones.

您还可以使用页面上各个组件的 Loaded 事件.这也非常简单.它们有时可能会在第 4 步和第 5 步之间发生.

You can also use the Loaded events of individual components on your page. That's also trivially easy. They will probably happen sometimes between 4th and 5th step.

如果以上选项都没有帮助(我不这么认为),则必须使用非标准逻辑.例如上面提到的一次性定时器,它会在一段时间后显示消息.或者基于 LayoutUpdated 计数器的逻辑.

If none of above options helps (I don't think so), you have to use non-standard logic. Such as above mentioned one-shot timer that shows the message after some time. Or base your logic on LayoutUpdated counter.

这篇关于加载事件未完全加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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