【问题标题】:WPF Caliburn.Micro : Life Cicyle problemsWPF Caliburn.Micro : Life Cicyle 问题
【发布时间】:2016-05-19 20:58:54
【问题描述】:

我有一个名为 AppViewModel 的类,这个类负责控制屏幕。 AppViewModel 扩展了我的 BaseConductor:

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

然后,我在 AppViewModel 的构造函数上调用了一个视图模型(UserControl):

        this.ActivateItem(new FirstViewModel());

FirstViewModel上,用户点击按钮后我想打开SecondViewModel并关闭FirstViewModel

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

我已经尝试过这样做:

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

SecondViewModel 扩展了我的 BaseScreen:

public class BaseSceen : Screen
{
   ...
}

我想关闭 FirstViewModel,因为在 FirstViewModel 和 SecondViewModel 上我有快捷方式。当我打开 SecondViewModel 时,我点击了一个快捷方式,执行它的方法来自 FirstViewModel。 所以,FirstViewModel 仍在运行。

如何关闭 FirstViewModel 并避免快捷方式出现此问题?

谢谢!

【问题讨论】:

    标签: c# wpf caliburn.micro


    【解决方案1】:

    你真的需要使用Conductor&lt;T&gt;.Collection.OneActive吗?您可以只使用Conductor&lt;T&gt; 以便激活项目将自动停用并关闭先前活动的项目。此外,按钮/操作对是否需要驻留在FirstViewModel 中?我建议你把它们放在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());
        }
    }
    

    【讨论】:

    • 嗨,我有很多带按钮的屏幕,所以我更喜欢他们调用下一个屏幕。我将 AppViewModel 编辑为 Conductor 并且仍然无法正常工作:(
    【解决方案2】:

    我找到了!快捷方式事件附加到窗口,而不是用户控件。 因此,当用户控件结束时,事件仍然“附加”到窗口。现在,我添加了一个在卸载 UserControl 时调用的方法,以“分离”事件。 大错特错!

    【讨论】:

      猜你喜欢
      • 2014-01-19
      • 2016-03-19
      • 1970-01-01
      • 2016-03-29
      • 2023-03-07
      • 2017-09-29
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      相关资源
      最近更新 更多