1 按照《prism basic navigation》搭建项目和库
2 修改ModuleA中ViewA,ViewB
<UserControl x:Class="ModuleA.Views.ViewA"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ModuleA.Views"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<StackPanel>
<TextBlock Text="{Binding Title}" FontSize="48" HorizontalAlignment="Center" VerticalAlignment="Center" />
<TextBlock Text="{Binding PageViews}" FontSize="24" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
</UserControl>
<UserControl x:Class="ModuleA.Views.ViewB"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True">
<StackPanel>
<TextBlock Text="{Binding Title}" FontSize="48" HorizontalAlignment="Center" VerticalAlignment="Center" />
<TextBlock Text="{Binding PageViews}" FontSize="24" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
</UserControl>
3 在ModuleA中新建ViewModels文件夹,并创建ViewAViewModel和ViewBViewModel,让其再实现INavigationAware接口,这个可以设置当前视图是不是可以导航,已经导航到该视图以后,要做的事情
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModuleA.ViewModels
{
public class ViewAViewModel : BindableBase, INavigationAware
{
private string _title = "ViewA";
public string Title
{
get { return _title; }
set { SetProperty(ref _title, value); }
}
private int _pageViews;
public int PageViews
{
get { return _pageViews; }
set { SetProperty(ref _pageViews, value); }
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
PageViews++;
}
}
}
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModuleA.ViewModels
{
public class ViewBViewModel : BindableBase, INavigationAware
{
private string _title="ViewB";
public string Title
{
get { return _title; }
set { SetProperty(ref _title, value); }
}
private int _pageViews;
public int PageViews
{
get { return _pageViews; }
set { SetProperty(ref _pageViews, value); }
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
PageViews++;
}
}
}
4 运行
5 如果修改ViewModel里面的IsNavigationTarget方法如下,当该方法返回true时候,将导航到该视图,如果返回false,将新建一个TabItem来显示,计数从0开始
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return PageViews / 3 != 1;
}