Caliburn.Micro - 当视图可见时执行某些操作

发布于 2024-12-13 03:11:09 字数 736 浏览 2 评论 0原文

我目前正在研究 WPF 和 Caliburn.Micro,暂时没有像 MEF 或 Autofac 这样的东西。

现在我正在尝试在视图模型可见后立即执行视图模型中的一些代码。

在相关教程中,此代码在显示视图之前显示一个消息框:

protected override void OnActivate() 
{   
    MessageBox.Show("Page Two Activated"); //Don't do this in a real VM.   
    base.OnActivate();   
}  

Eisenberg 先生随后写道:

请记住,如果您有任何依赖于 视图已加载,您应该覆盖 Screen.OnViewLoaded 代替/与 OnActivate 结合使用。

这就是我所拥有的:

protected override void OnViewLoaded(object view)
{
    base.OnViewLoaded(view);
    MessageBox.Show("OnPageTwoViewLoaded");
}

我还通过 Grid EventTrigger 和 cal:ActionMessage 进行了尝试。 但在所有三种情况下,MessageBox 都会在视图可见之前出现。

我肯定错过了一些东西,我做错了什么?

I am currently getting into WPF and Caliburn.Micro ,for now without something like MEF or Autofac.

Right now i am trying to execute some code in a viewmodel right after its view becomes visible.

In a related tutorial this code displays a messagebox just before a view is shown:

protected override void OnActivate() 
{   
    MessageBox.Show("Page Two Activated"); //Don't do this in a real VM.   
    base.OnActivate();   
}  

Mr. Eisenberg then writes this:

Remember, if you have any activation logic that is dependent on the
view being already loaded, you should override Screen.OnViewLoaded
instead of/in combination with OnActivate.

This is what i have:

protected override void OnViewLoaded(object view)
{
    base.OnViewLoaded(view);
    MessageBox.Show("OnPageTwoViewLoaded");
}

I also tried it via a Grid EventTrigger and a cal:ActionMessage.
But in all three cases the MessageBox appears before the view is visible.

Surely i am missing something, what am i doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生活了然无味 2024-12-20 03:11:09

也许不是最优雅的解决方案,但我想你可以从代码隐藏中做到这一点,因为严格来说,这是你试图在这里做的一个非常视图/GUI 特定的事情。例如在 OnInitialized 或 OnRender 中。如果您为视图提供对 EventAggregator 的引用,则可以引发一个事件并创建视图模型 - 或任何您想要的类,订阅此事件并执行该操作。或者在显示 MessageBox 的情况下,除了 View 之外,您实际上不会在其他任何地方看到它。

Maybe not the most elegant solution, but I guess you can do this from the code-behind, since - strictly speaking - this is a very view/gui specific thing you're trying to do here. For instance in OnInitialized or OnRender. If you give your view a reference to the EventAggregator, you could raise an event and make the view model - or whatever class you want, subscribe to this event and do it's thing. Or in the case of showing a MessageBox, you really wouldn't have that any place else than in the View anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文