2

我需要根据特定条件切换正在显示的视图。我已经在ViewModel的构造函数中实现了切换逻辑

IRegionMemberLifetimeView上实现并设置KeepAlive为,false以便始终获得 View 和 ViewModel 的新实例。

但是由于某种原因,当我单击Navigation Button时,我的断点KeepAlive永远不会到达,我得到的是MainView而不是WelcomeView

这是供您参考的代码:

导航按钮:

<Controls:SignedButton VerticalAlignment="Top" Width="275" Height="45"
    Foreground="#FFFFFF" 
    LeftSign="&lt;" Text="Back to Accounts" 
    TextSize="20" ButtonBackground="#666666" 
    HoverBackground="#0FBDAC" HoverOpacity="1" Margin="0,25,0,0"
    Command="{x:Static Infrastructure:ApplicationCommands.NavigateCommand}"
    CommandParameter="{x:Type Views:MainView}"/>

查看型号:

[RegionMemberLifetime(KeepAlive = false)]
public class MainViewModel : ViewModel, IMainViewModel
{
    private readonly IUnityContainer _container;
    private readonly IRegionManager _regionManager;

    public MainViewModel(IUnityContainer container, IRegionManager regionManager)
    {
        _container = container;
        _regionManager = regionManager;

        Accounts = new List<Account>();

        if (Accounts.Any()) return;

        IRegion region = _regionManager.Regions[Regions.Main];

        var views = region.Views;
        foreach (var view in views)
        {
            region.Remove(view);
        }

        region.Add(_container.Resolve<IWelcomeView>());
    }

    public IList<Account> Accounts { get; private set; }
}

查看模型库:

public abstract class ViewModel : IViewModel
{
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

看法:

[RegionMemberLifetime(KeepAlive = false)]
public partial class MainView : UserControl, IMainView
{
    public MainView(IMainViewModel mainViewModel)
    {
        InitializeComponent();

        ViewModel = mainViewModel;
    }

    public IViewModel ViewModel
    {
        get { return (IViewModel) DataContext; }
        set { DataContext = value; }
    }
}

外壳视图模型:

public class ShellViewModel : ViewModel, IShellViewModel
{
    private readonly IRegionManager _regionManager;

    public ShellViewModel(IRegionManager regionManager)
    {
        _regionManager = regionManager;

        NavigateCommand = new DelegateCommand<object>(Navigate);
        ApplicationCommands.NavigateCommand.RegisterCommand(NavigateCommand);
    }

    private void Navigate(object navigatePath)
    {
        if (navigatePath != null)
        {
            _regionManager.RequestNavigate(Regions.Main, navigatePath.ToString());
        }
    }

    public DelegateCommand<object> NavigateCommand { get; private set; }
}
4

1 回答 1

0

查看 Prism 库中的 RegionMemberLifetimeBehavior 类对您很有用(对我来说,它位于 C:\Prism4\PrismLibrary\Desktop\Prism\Regions\Behaviors)

IRegionMemberLifetime 接口和 RegionMemberLifetimeAttribute 完成相同的事情,并且可以在您的 View 或 ViewModel 上定义(前提是 viewmodel 设置为 DataContext)。

这是相关的代码:

private void OnActiveViewsChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        // We only pay attention to items removed from the ActiveViews list.
        // Thus, we expect that any ICollectionView implementation would
        // always raise a remove and we don't handle any resets
        // unless we wanted to start tracking views that used to be active.
        if (e.Action != NotifyCollectionChangedAction.Remove) return;

        var inactiveViews = e.OldItems;
        foreach (var inactiveView in inactiveViews)
        {
            if (!ShouldKeepAlive(inactiveView))
            {
                this.Region.Remove(inactiveView);
            }
        }
    }

    private static bool ShouldKeepAlive(object inactiveView)
    {
        IRegionMemberLifetime lifetime = GetItemOrContextLifetime(inactiveView);
        if (lifetime != null)
        {
            return lifetime.KeepAlive;
        }

        RegionMemberLifetimeAttribute lifetimeAttribute = GetItemOrContextLifetimeAttribute(inactiveView);
        if (lifetimeAttribute != null)
        {
            return lifetimeAttribute.KeepAlive;
        }

        return true;
    }

我什至可以单步执行此代码以查看它如何与我的应用程序交互。要回答您的问题,如果您的 KeepAlive 属性没有被击中,那么它不会从 ActiveViews 中删除。还要确保如果您通过 IoC 从容器解析视图,则您尚未将其注册为单例类型(每次解析时都会获得一个新实例)。属性/接口只会将其完全从区域中删除,不能保证您获得一个新实例。

于 2012-10-09T18:19:43.577 回答