然而,当我实现此功能时,我意识到当使用RegionManager的RequestNavigate切换到另一个视图时,以前的视图模型仍然处于活动状态。当我为最近的视图触发公共事件时,它也会为前一个视图触发。
-
从视图1开始
-
-
-
请求导航到视图2
-
-
响应:来自视图2的消息,然后来自视图1的消息
-
请求导航到视图3
-
-
响应:来自视图3、视图2和视图1的消息
-
我在视图1、视图2和视图3的公共事件上放置了一个断点,每次从视图中获得消息时,它的断点都会被击中。
我想要的很简单:我不希望以前的ViewModel(也可能是View)在切换视图时仍然处于活动状态。最好是对它们进行垃圾收集,因为我还遇到了一些奇怪的情况,通过再次导航到视图1、视图2和视图1,视图1的消息被发送了两次(其断点也被击中了两次),因此我甚至不确定是否创建了ViewModels的多个引用,这可能会导致内存泄漏。
壳xaml
<Window x:Class="PrismTest.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prsm="http://prismlibrary.com/"
mc:Ignorable="d">
<Grid>
<ContentControl Name="MainRegion" prsm:RegionManager.RegionName="MainRegion" />
</Grid>
</Window>
NinjectPrismBootstrapper。反恐精英
public class NinjectPrismBootstrapper : NinjectBootstrapper
{
protected override void InitializeModules()
{
base.InitializeModules();
// Text to speech
Kernel.Bind<SpeechSynthesizer>().ToSelf().InSingletonScope();
Kernel.Bind<INarrator>().To<StandardNarrator>().InSingletonScope();
Kernel.Bind<INarratorEventManager>().To<NarratorEventManager>().InSingletonScope();
// View models
Kernel.Bind<MainPageViewModel>().ToSelf();
Kernel.Bind<SecondPageViewModel>().ToSelf();
// Views
Kernel.Bind<object>().To<MainPageView>().InTransientScope().Named(typeof(MainPageView).Name);
Kernel.Bind<object>().To<SecondPageView>().InTransientScope().Named(typeof(SecondPageView).Name);
Kernel.Bind<Shell>().ToSelf();
var narratorEventManager = Kernel.Get<INarratorEventManager>();
var regionManager = Kernel.Get<IRegionManager>();
regionManager.RegisterViewWithRegion("MainRegion", typeof(MainPageView));
}
protected override DependencyObject CreateShell()
{
return (Shell)Kernel.GetService(typeof(Shell));
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Shell)this.Shell;
Application.Current.MainWindow.Show();
}
}
主页面视图。xaml(我的起始页)
<UserControl x:Class="PrismTest.Views.MainPageView"
namespaces...>
<StackPanel>
<TextBlock Text="Main page"/>
<Button Content="Narrator speaks" Command="{Binding Path=NarratorSpeaksCommand}" />
<Button Content="Next page" Command="{Binding Path=GoToNextPageCommand}"/>
</StackPanel>
</UserControl>
主页面视图。xaml。反恐精英
public partial class MainPageView : UserControl
{
public MainPageView(MainPageViewModel dataContext)
{
InitializeComponent();
this.DataContext = dataContext;
}
}
public class MainPageViewModel : BindableBase, IRegionMemberLifetime, INavigationAware
{
private readonly IEventAggregator _eventAggregator;
private readonly IRegionManager _regionManager;
public DelegateCommand GoToNextPageCommand { get; private set; }
public DelegateCommand NarratorSpeaksCommand { get; private set; }
public MainPageViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
{
_eventAggregator = eventAggregator;
_regionManager = regionManager;
ConfigureCommands();
//The original common event triggered by a vocal command is simulated in this project by simply clicking on a button
_eventAggregator.GetEvent<CommonEventToAllViews>().Subscribe(NarratorSpeaks);
}
private void ConfigureCommands()
{
GoToNextPageCommand = new DelegateCommand(GoToNextPage);
NarratorSpeaksCommand = new DelegateCommand(ClickPressed);
}
private void GoToNextPage()
{
_regionManager.RequestNavigate("MainRegion", new Uri("SecondPageView", UriKind.Relative));
}
private void ClickPressed()
{
_eventAggregator.GetEvent<CommonEventToAllViews>().Publish();
}
private void NarratorSpeaks()
{
_eventAggregator.GetEvent<NarratorSpeaksEvent>().Publish("Main page");
}
}
我不需要输入SecondPageViewModel和SecondPageView的代码,因为除了RequestNavigate将用户发送回MainPageView,其叙述者峰值方法发送不同的字符串之外,这两个代码完全相同。
我尝试的内容:
2) 在IsNavigationTarget方法中继承INavigationAware并返回false
public void OnNavigatedFrom(NavigationContext navigationContext)
{
var region = _regionManager.Regions["MainRegion"];
var view = region.Views.Single(v => v.GetType().Name == "MainPageView");
region.Deactivate(view);
}
值得注意的是:即使没有deactivate部分,如果我在var region=\u regionManager之后放置一个断点。区域[“MainRegion”];并检查区域。视图,无论我切换多少视图,只有一个结果。
什么都不起作用,当我来回切换视图时,在以前的视图中不断触发事件。
所以,我有点不知所措。我不确定是我在Ninject中注册视图和视图模型的方式触发了这一点,还是其他什么,但如果有人有建议,我很乐意接受。
谢谢