prism 激活去激活视图

1 按照《prism项目搭建》搭建项目

2 在Views文件夹里面创建用户控件ViewA,ViewB

<UserControl x:Class="Prism.ActiveDeactive.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:Prism.ActiveDeactive.Views"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <TextBlock Text="ViewA" HorizontalAlignment="Center" />
    </Grid>
</UserControl>
<UserControl x:Class="Prism.ActiveDeactive.Views.ViewB"
             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:Prism.ActiveDeactive.Views"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <TextBlock Text="ViewB" HorizontalAlignment="Center" />
    </Grid>
</UserControl>

 3 在MainWindow里面添加四个按钮

<Window x:Class="Prism.ActiveDeactive.Views.MainWindow"
        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:Prism.ActiveDeactive"
        xmlns:prism="http://prismlibrary.com/"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
    <DockPanel LastChildFill="True">
        <StackPanel>
            <Button x:Name="ActiveA" Content="Activate ViewA"  Click="ActiveA_Click"/>
            <Button x:Name="DeactiveA" Content="Deactivate ViewA" Click="DeactiveA_Click"/>
            <Button x:Name="ActiveB" Content="Activate ViewB" Click="ActiveB_Click"/>
            <Button x:Name="DeactiveB" Content="Deactivate ViewB" Click="DeactiveB_Click"/>
        </StackPanel>
        <ContentControl prism:RegionManager.RegionName="ContentRegion" />
    </DockPanel>
</Window>

5 在MainWindow里面添加四个局部变量

 IRegionManager _regionManager; 
        IContainerExtension _container;

        ViewA viewA;
        ViewB viewB;

        IRegion region;

6 通过构造函数注入regionManager和container

 public MainWindow(IRegionManager regionManager,IContainerExtension container)
        {
            InitializeComponent();
            _regionManager = regionManager;
            _container = container;

        }

7 在WIndow的加载函数中通过容易得到ViewA ViewB的实例并添加的region里面

 private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            region = _regionManager.Regions["ContentRegion"];
            viewA = _container.Resolve<ViewA>();
            viewB = _container.Resolve<ViewB>();
            region.Add(viewA);
            region.Add(viewB);
        }

8 按钮事件实现如下

 private void ActiveA_Click(object sender, RoutedEventArgs e)
        {
            region.Activate(viewA);
        }

        private void DeactiveA_Click(object sender, RoutedEventArgs e)
        {
            region.Deactivate(viewA);
        }

        private void ActiveB_Click(object sender, RoutedEventArgs e)
        {
            region.Activate(viewB);
        }

        private void DeactiveB_Click(object sender, RoutedEventArgs e)
        {
            region.Deactivate(viewB);
        }

9 全部代码如下

using Prism.Ioc;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Prism.ActiveDeactive.Views
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        IRegionManager _regionManager; 
        IContainerExtension _container;

        ViewA viewA;
        ViewB viewB;

        IRegion region;


        public MainWindow(IRegionManager regionManager,IContainerExtension container)
        {
            InitializeComponent();
            _regionManager = regionManager;
            _container = container;

        }

        private void ActiveA_Click(object sender, RoutedEventArgs e)
        {
            region.Activate(viewA);
        }

        private void DeactiveA_Click(object sender, RoutedEventArgs e)
        {
            region.Deactivate(viewA);
        }

        private void ActiveB_Click(object sender, RoutedEventArgs e)
        {
            region.Activate(viewB);
        }

        private void DeactiveB_Click(object sender, RoutedEventArgs e)
        {
            region.Deactivate(viewB);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            region = _regionManager.Regions["ContentRegion"];
            viewA = _container.Resolve<ViewA>();
            viewB = _container.Resolve<ViewB>();
            region.Add(viewA);
            region.Add(viewB);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值