prism 激活去激活视图

阅读: 评论:0

prism 激活去激活视图

prism 激活去激活视图

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

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

<UserControl x:Class="Prism.ActiveDeactive.Views.ViewA"xmlns=""xmlns:x=""xmlns:mc="" xmlns:d="" 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=""xmlns:x=""xmlns:mc="" xmlns:d="" 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=""xmlns:x=""xmlns:d=""xmlns:mc=""xmlns:local="clr-namespace:Prism.ActiveDeactive"xmlns:prism="/"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);}}
}

本文发布于:2024-02-05 00:20:55,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170719733561175.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:视图   prism
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23