0

我喜欢 Catel 框架。现代 UI 看起来很不错。但是我在尝试使它们一起工作时遇到了问题。

我在 mui 项目中Home添加了两个 catels 用户控件。Second问题是当从执行HomeSecond执行的转换HomeViewModel已经创建了 3 次。

这种行为是由下一个代码引起的TransitioningContentControl

    private void StartTransition(object oldContent, object newContent)
    {
        // both presenters must be available, otherwise a transition is useless.
        if (CurrentContentPresentationSite != null && PreviousContentPresentationSite != null) {
            CurrentContentPresentationSite.Content = newContent;

            PreviousContentPresentationSite.Content = oldContent;

            // and start a new transition
            if (!IsTransitioning || RestartTransitionOnContentChange) {
                IsTransitioning = true;
                VisualStateManager.GoToState(this, NormalState, false);
                VisualStateManager.GoToState(this, Transition, true);
            }
        }
    }

如果我评论一些行:

private void StartTransition(object oldContent, object newContent)
    {
        // both presenters must be available, otherwise a transition is useless.
        if (CurrentContentPresentationSite != null && PreviousContentPresentationSite != null) {
            CurrentContentPresentationSite.Content = newContent;

            //PreviousContentPresentationSite.Content = oldContent;

            // and start a new transition
            if (!IsTransitioning || RestartTransitionOnContentChange) {
                IsTransitioning = true;
                //VisualStateManager.GoToState(this, NormalState, false);
                //VisualStateManager.GoToState(this, Transition, true);
            }
        }
    }

在这种情况下,相同的转换会导致创建HomeViewModel1 次,但我不想在从控件HomeViewModel执行导航时创建。Home我怎样才能做到这一点?

项目品尝

4

1 回答 1

2

有 2 个可能的选项可以解决此问题:

1)使用现有功能(CloseViewModelOnUnloaded)。将在过渡期间保持 VM 处于活动状态。

然后您需要在 TransitioningContentControl.StartTransition 中使用此代码

var userControl = oldContent as Catel.Windows.Controls.UserControl;
if (userControl != null)
{
    userControl.CloseViewModelOnUnloaded = false;
}

PreviousContentPresentationSite.Content = oldContent;

将此添加到 OnTransitionCompleted:

var userControl = PreviousContentPresentationSite.Content as Catel.Windows.Controls.UserControl;
if (userControl != null)
{
    userControl.CloseViewModelOnUnloaded = true;
    var vm = userControl.ViewModel;
    if (vm != null)
    {
        vm.CloseViewModel(true);
    }
}

AbortTransition();

2) 使用新功能 (PreventViewModelCreation) 在过渡期间不会使 VM 保持活动状态。

然后您需要在 TransitioningContentControl.StartTransition 中使用此代码

var vmContainer = oldContent as IViewModelContainer;
if (vmContainer != null)
{
    vmContainer.PreventViewModelCreation = true;
}

PreviousContentPresentationSite.Content = oldContent;

将此添加到 OnTransitionCompleted 方法:

var vmContainer = PreviousContentPresentationSite.Content as IViewModelContainer;
if (vmContainer != null)
{
    vmContainer.PreventViewModelCreation = false;
}

AbortTransition();
于 2014-03-19T11:38:19.590 回答