06、ActivationDeactivation

  1、将App.xaml中的StartupUri="MainWindow.xaml"删除。

  2、使用NuGet安装Prism.Wpf、Prism.Core、Prism.Unity。

  3、添加类“Bootstrapper”,编辑如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows;
 7 using Microsoft.Practices.Unity;
 8 using ActivationDeactivation.Views;
 9 using Prism.Unity;
10 
11 namespace ActivationDeactivation
12 {
13     class Bootstrapper:UnityBootstrapper
14     {
15         protected override DependencyObject CreateShell()
16         {
17             return Container.Resolve<MainWindow>();
18         }
19 
20         protected override void InitializeShell()
21         {
22             Application.Current.MainWindow.Show();
23         }
24     }
25 }

  4、创建文件夹Views,将MainWindow.xaml移动到此文件夹中。向Views文件夹中添加ViewA.xaml,ViewB.xaml。

  

 1 <Window x:Class="ActivationDeactivation.Views.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:local="clr-namespace:ActivationDeactivation"
 7         xmlns:prism="http://prismlibrary.com/"
 8         mc:Ignorable="d"
 9         Title="MainWindow" Height="450" Width="800">
10     <DockPanel LastChildFill="True">
11         <StackPanel>
12             <Button Content="Activate ViewA" x:Name="btnViewA" Click="BtnViewA_OnClick"/>
13             <Button Content="Deactivate ViewA" x:Name="btnDeactiveViewA" Click="BtnDeactiveViewA_OnClick"/>
14             <Button Content="Activate ViewB" x:Name="btnViewB"  Click="BtnViewB_OnClick"/>
15             <Button Content="Deactivate ViewB" x:Name="btnDeactiveViewB" Click="BtnDeactiveViewB_OnClick"/>
16         </StackPanel>
17         <ContentControl prism:RegionManager.RegionName="ContentRegion" HorizontalAlignment="Center" VerticalAlignment="Center" />
18     </DockPanel>
19 </Window>
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows;
 7 using System.Windows.Controls;
 8 using System.Windows.Data;
 9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15 using Microsoft.Practices.Unity;
16 using Prism.Regions;
17 
18 namespace ActivationDeactivation.Views
19 {
20     /// <summary>
21     /// MainWindow.xaml 的交互逻辑
22     /// </summary>
23     public partial class MainWindow : Window
24     {
25         private IUnityContainer _container;
26         private IRegionManager _regionManager;
27         private IRegion _region;
28 
29         private ViewA _viewA;
30         private ViewB _viewB;
31         public MainWindow(IUnityContainer unityContainer,IRegionManager regionManager)
32         {
33             InitializeComponent();
34             _container = unityContainer;
35             _regionManager = regionManager;
36             
37 
38             this.Loaded += MainWindow_Loaded;
39         }
40 
41         private void MainWindow_Loaded(object sender, RoutedEventArgs e)
42         {
43             _viewA = _container.Resolve<ViewA>();
44             _viewB = _container.Resolve<ViewB>();
45 
46             _region = _regionManager.Regions["ContentRegion"];
47 
48             _region.Add(_viewA);
49             _region.Add(_viewB);
50         }
51 
52         private void BtnViewA_OnClick(object sender, RoutedEventArgs e)
53         {
54             _region.Activate(_viewA);
55         }
56 
57         private void BtnDeactiveViewA_OnClick(object sender, RoutedEventArgs e)
58         {
59             _region.Deactivate(_viewA);
60         }
61 
62         private void BtnViewB_OnClick(object sender, RoutedEventArgs e)
63         {
64             _region.Activate(_viewB);
65         }
66 
67         private void BtnDeactiveViewB_OnClick(object sender, RoutedEventArgs e)
68         {
69             _region.Deactivate(_viewB);
70         }
71     }
72 }
 1 <UserControl x:Class="ActivationDeactivation.Views.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              xmlns:local="clr-namespace:ActivationDeactivation.Views"
 7              mc:Ignorable="d" 
 8              d:DesignHeight="450" d:DesignWidth="800">
 9     <Grid>
10             <TextBlock Text="View A" FontSize="38"/>
11     </Grid>
12 </UserControl>
 1 <UserControl x:Class="ActivationDeactivation.Views.ViewB"
 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              xmlns:local="clr-namespace:ActivationDeactivation.Views"
 7              mc:Ignorable="d" 
 8              d:DesignHeight="450" d:DesignWidth="800">
 9     <Grid>
10             <TextBlock Text="View B" FontSize="38"/>
11     </Grid>
12 </UserControl>

  5、修改App.xaml

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Configuration;
 4 using System.Data;
 5 using System.Linq;
 6 using System.Threading.Tasks;
 7 using System.Windows;
 8 
 9 namespace ActivationDeactivation
10 {
11     /// <summary>
12     /// App.xaml 的交互逻辑
13     /// </summary>
14     public partial class App : Application
15     {
16         protected override void OnStartup(StartupEventArgs e)
17         {
18             base.OnStartup(e);
19 
20             var bootstrapper = new Bootstrapper();
21             bootstrapper.Run();
22         }
23     }
24 }

 

转载于:https://www.cnblogs.com/bjxingch/articles/9547741.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值