Prism中Region的动态使用

1、完成Region的定义

新建一个Dmeo2的wpf窗体,完成区域定义

复制代码
<Window x:Class="WpfProject_Study.Views.Demo2"
        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:local="clr-namespace:WpfProject_Study.Views"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        mc:Ignorable="d"
        Title="Demo2" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <StackPanel>
            <Button Margin="10" Content="用户管理" Command="{Binding OpenCommand}" CommandParameter="UserManage"></Button>
            <Button Margin="10" Content="部门管理" Command="{Binding OpenCommand}" CommandParameter="DeptManage"></Button>
            <Button Margin="10" Content="角色管理"></Button>
        </StackPanel>
        <ContentControl Grid.Column="1" prism:RegionManager.RegionName="UserRegion"></ContentControl>
    </Grid>
</Window>
复制代码

2、创建3个用于切换的用户控件,其他雷同。

复制代码
<UserControl x:Class="WpfProject_Study.Views.SystemManage.DeptManage"
             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:WpfProject_Study.Views.SystemManage"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <TextBlock Text="部门管理"></TextBlock>
    </Grid>
</UserControl>
复制代码

3、在App.xml文件中,完成对页面导航的注入

复制代码
public partial class App : PrismApplication
{
    /// <summary>
    /// 创建一个启动页
    /// </summary>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    protected override Window CreateShell()
    {
        return Container.Resolve<Demo2>();
    }

    /// <summary>
    /// 区域视图的注入
    /// </summary>
    /// <param name="containerRegistry">区域容器</param>
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        //向容器中注入一个导航
     containerRegistry.RegisterForNavigation<Views.SystemManage.UserManage>();
        containerRegistry.RegisterForNavigation<Views.SystemManage.DeptManage>();
    }

}
复制代码

4、在ViewModel中,创建Demo2ViewModel,继承BindableBase完成MVVM的框架使用规范。实现命令与区域管理功能的实现。

复制代码
public class Demo2ViewModel:BindableBase
{
    private IRegionManager _regionManager;

    public MyComand OpenCommand { get; set; }
    public Demo2ViewModel(IRegionManager regionManager) { 
        _regionManager = regionManager;
        //初次加载注入一个页面
        _regionManager.RegisterViewWithRegion("UserRegion", typeof(UserManage));
        OpenCommand = new MyComand(load, IsExecuted);
    }

    /// <summary>
    /// 实现点击命令
    /// 根据不同的按钮所传递的参数,去请求不同的页面导航
    /// </summary>
    /// <param name="obj"></param>
    public void load(object obj) 
    {
        //找到UserRegion的区域,请求一个导航
        _regionManager.Regions["UserRegion"].RequestNavigate(obj.ToString());

    }

    private bool isExecuted =true;
    public bool IsExecuted(object obj) { return isExecuted; }
}
复制代码

点击不同按钮,设定区域会加载不同的控件

 

posted @   行稳致远ac  阅读(93)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示