简体   繁体   English

WPF Caliburn.Micro:生命周期问题

[英]WPF Caliburn.Micro : Life Cicyle problems

I have a class called AppViewModel, this class it's responsible to control the screens. 我有一个名为AppViewModel的类,该类负责控制屏幕。 AppViewModel extends my BaseConductor: AppViewModel扩展了我的BaseConductor:

public class BaseConductor : Conductor<Screen>.Collection.OneActive
{
   ...
}

Then, I call a viewmodel (UserControl) on the constructor of AppViewModel: 然后,在AppViewModel的构造函数上调用一个viewmodel(UserControl):

        this.ActivateItem(new FirstViewModel());

On FirstViewModel , after the user clicks on a button I want to open SecondViewModel and close the FirstViewModel : FirstViewModel上 ,用户单击按钮后,我想打开SecondViewModel并关闭FirstViewModel

var conductor = this.Parent as IConductor;
conductor.DeactivateItem(this, true);
conductor.ActivateItem(new SecondViewModel(param));

I already tried to do this: 我已经尝试这样做:

((IApp)this.Parent).ActivateItem(new SecondViewModel(param));
TryClose();

SecondViewModel extends my BaseScreen: SecondViewModel扩展了我的BaseScreen:

public class BaseSceen : Screen
{
   ...
}

I want to close the FirstViewModel, because on the FirstViewModel and SecondViewModel I have shortcuts. 我想关闭FirstViewModel,因为在FirstViewModel和SecondViewModel上我有快捷方式。 When I'm with the SecondViewModel opened I hit a shortcut, and the method that is executed it's from FirstViewModel. 当我打开SecondViewModel时,我点击了一个快捷方式,执行的方法来自FirstViewModel。 So, the FirstViewModel still running. 因此,FirstViewModel仍在运行。

How can I close the FirstViewModel, and avoid this problem with shortcuts? 如何关闭FirstViewModel,并通过快捷方式避免此问题?

Thanks! 谢谢!

Do you really need to use Conductor<T>.Collection.OneActive ? 您真的需要使用Conductor<T>.Collection.OneActive吗? You can just use Conductor<T> so that activating an item will automatically deactivate and close the previously active item. 您可以只使用Conductor<T>以便激活一个项目将自动停用并关闭先前处于活动状态的项目。 And also, is it required that the button/action pair reside in the FirstViewModel ? 而且,是否要求按钮/动作对位于FirstViewModel I suggest that you just put those in the AppViewModel and let it orchestrate the navigation and activation/deactivation of the two child screens. 我建议您只将它们放在AppViewModel并让它协调两个子屏幕的导航和激活/停用。

public AppViewModel : Conductor<Screen>
{
    public void AppViewModel()
    {
        ActivateItem(new FirstViewModel());
    }

    public void ActivateSecondViewModel()
    {
        // FirstViewModel will automatically be deactivated
        // and closed since we are using plain Conductor<T>
        ActivateItem(new SecondViewModel());
    }
}

I found it! 我找到了! The shortcut event was attached to the window, not to the usercontrol. 快捷方式事件附加到窗口,而不附加到用户控件。 So, event when usercontrol was ended the event still "attached" to the window. 因此,事件在usercontrol结束时,事件仍“附加”到窗口。 Now, I added an method that is called when UserControl is Unloaded, to "deattach" the event. 现在,我添加了一个在卸载UserControl时调用的方法,以“取消附加”事件。 Bad mistake! 严重的错误!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM