Prism入門之區域(Region)管理


一、區域管理器

首先看一下官方給的模型圖

img

現在我們可以知道的是,大致一個區域管理器RegionMannager對一個控件創建區域的要點:

  • 創建Region的控件必須包含一個RegionAdapter適配器
  • region是依賴在具有RegionAdapter控件身上的

其實后來我去看了下官方的介紹和源碼,默認RegionAdapter是有三個,且還支持自定義RegionAdapter,因此在官方的模型圖之間我做了點補充:

img

二、區域創建與視圖的注入

1、創建前端頁面,放置3個按鈕用於向區域中注入不同的視圖

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <StackPanel>
            <Button
                Margin="5"
                Command="{Binding OpenCommand}"
                CommandParameter="ViewA"
                Content="模塊A" />
            <Button
                Margin="5"
                Command="{Binding OpenCommand}"
                CommandParameter="ViewB"
                Content="模塊B" />
            <Button
                Margin="5"
                Command="{Binding OpenCommand}"
                CommandParameter="ViewC"
                Content="模塊C" />
        </StackPanel>

        <ContentControl Grid.Column="1" prism:RegionManager.RegionName="ModuleContent" />
    </Grid>

通過Prism框架的區域管理器RegionManager的RegionName注冊一個區域,方便后續向里面填充視圖頁面等。

2、再創建幾個用戶控件,這里要注意一下,只能是只能具有Window或Frame父級才可以。

<UserControl
    x:Class="BlankCoreApp1.Views.ViewA"
    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:local="clr-namespace:BlankCoreApp1.Views"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d">
    <Grid Background="Red">
        <TextBlock
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            FontSize="60"
            Foreground="White"
            Text="我是模塊A" />
    </Grid>
</UserControl>

另外兩個一樣的省略代碼...

3、接着則定義一個Command以及Command最終執行的方法,並綁定到前端的按鈕中

//CS:
public DelegateCommand<string> OpenCommand { get; private set; }

//XAML:
<Button
                Margin="5"
                Command="{Binding OpenCommand}"
                CommandParameter="ViewA"
                Content="模塊A" />

另外,如果我們想使用區域管理器向其ContentControl注入視圖則還需在VM的構造函數中獲取區域管理器

public MainWindowViewModel(IRegionManager regionManager)
        {
            _regionManage = regionManager;
            OpenCommand = new DelegateCommand<string>(OpenMethod);
        }

在Command的執行方法中使用區域管理器查找全局已定義的可用區域,通過注冊的區域名稱找到區域動態的去設置內容

private void OpenMethod(string obj)
        {
        _regionManage.Regions["ModuleContent"].RequestNavigate(obj);
        }

4、還有一個重要的點,需要在App.cs中通過重寫RegisterTypes方法注冊區域視圖

protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<ViewA>();
            containerRegistry.RegisterForNavigation<ViewB>();
            containerRegistry.RegisterForNavigation<ViewC>();
        }

最終運行效果:

1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM