wpf Prism中区域的使用
1.首先,我们创建一个区域内容,这里我们创建一个ViewA使用UseControl
这个就是普通的UseControl,只是加了个TextBlock,显示ViewA
ViewA.xaml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <UserControl x:Class="MyTest.ViewA" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 mc:Ignorable="d" 7 d:DesignHeight="450" d:DesignWidth="800"> 8 <Grid> 9 10 <TextBlock Text="ViewA" FontSize="50" Background="Yellow"></TextBlock> 11 </Grid> 12 </UserControl>
ViewA.xaml.cs
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System.Windows.Controls; 2 3 namespace MyTest 4 { 5 /// <summary> 6 /// ViewA.xaml 的交互逻辑 7 /// </summary> 8 public partial class ViewA : UserControl 9 { 10 public ViewA() 11 { 12 InitializeComponent(); 13 } 14 } 15 }
2.我们在MainWindow.xaml文件中加入ContentCtrol用于显示Region
<ContentControl prism:RegionManager.RegionName="contentRegion"/>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <Window x:Class="MyTest.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:prism="http://prismlibrary.com/" 7 prism:ViewModelLocator.AutoWireViewModel="True" 8 xmlns:local="clr-namespace:MyTest" 9 mc:Ignorable="d" 10 Title="MainWindow" Height="450" Width="800"> 11 <Grid> 12 <ContentControl prism:RegionManager.RegionName="contentRegion"/> 13 14 </Grid> 15 </Window>
或者在cs构造中申明区域名称:
MainWindow.xaml文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <Window x:Class="MyTest.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:prism="http://prismlibrary.com/" 7 prism:ViewModelLocator.AutoWireViewModel="True" 8 xmlns:local="clr-namespace:MyTest" 9 mc:Ignorable="d" 10 Title="MainWindow" Height="450" Width="800"> 11 <Grid> 12 <ContentControl prism:RegionManager.RegionName="contentRegion"/> 13 <ContentControl x:Name="ctr"/> 14 </Grid> 15 </Window>
MainWindow.xaml.cs文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using Prism.Regions; 2 using System.Windows; 3 4 namespace iProfile 5 { 6 /// <summary> 7 /// Interaction logic for MainWindow.xaml 8 /// </summary> 9 public partial class MainWindow : Window 10 { 11 12 public MainWindow() 13 { 14 InitializeComponent(); 15 RegionManager.SetRegionName(ctr, "content2"); 16 } 17 18 } 19 }
3.我们在MainWIndow.ViewModel中将区域A注册在contentRegion中显示
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using Prism.Mvvm; 2 using Prism.Regions; 3 4 namespace MyTest 5 { 6 public class MainWindowViewModel:BindableBase 7 { 8 9 private string _title = "hello"; 10 public string Title 11 { 12 get { return _title; } 13 set { SetProperty(ref _title, value); } 14 } 15 16 private readonly IRegionManager regionManager; 17 18 19 public MainWindowViewModel(IRegionManager regionManager) 20 { 21 this.regionManager = regionManager; 22 regionManager.RegisterViewWithRegion("contentRegion",typeof(ViewA)); 23 24 } 25 26 27 28 } 29 }
4.这样我们就在区域contentRegion中显示了区域ViewA视图,也可以通过按按钮动态切换