05、ViewInjection

  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 Prism.Unity;
 9 using ViewInjection.Views;
10 
11 namespace ViewInjection
12 {
13     public 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文件夹中添加TextA.xaml。

  

 1 <Window x:Class="ViewInjection.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:ViewInjection"
 7         xmlns:prism="http://prismlibrary.com/"
 8         mc:Ignorable="d"
 9         Title="MainWindow" Height="450" Width="800">
10     <DockPanel>
11         <Button x:Name="btnAddRegion" Click="BtnAddRegion_OnClick" DockPanel.Dock="Top" Content="Add Region" Height="45"/>
12         <ContentControl prism:RegionManager.RegionName="ContentRegion"></ContentControl>
13     </DockPanel>
14 </Window>
 1 using System.Windows.Navigation;
 2 using System.Windows.Shapes;
 3 using Microsoft.Practices.Unity;
 4 using Prism.Regions;
 5 
 6 namespace ViewInjection.Views
 7 {
 8     /// <summary>
 9     /// MainWindow.xaml 的交互逻辑
10     /// </summary>
11     public partial class MainWindow : Window
12     {
13         private IUnityContainer _container;
14         private IRegionManager _regionManager;
15         public MainWindow(IUnityContainer unityContainer, IRegionManager regionManager)
16         {
17             InitializeComponent();
18 
19             _container = unityContainer;
20             _regionManager = regionManager;
21         }
22 
23 
24         private void BtnAddRegion_OnClick(object sender, RoutedEventArgs e)
25         {
26             var view = _container.Resolve<TextA>();
27             IRegion region = _regionManager.Regions["ContentRegion"];
28             region.Add(view);
29         }
30     }
31 }
 1 <UserControl x:Class="ViewInjection.Views.TextA"
 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:ViewInjection.Views"
 7              mc:Ignorable="d" 
 8              d:DesignHeight="200" d:DesignWidth="300">
 9     <Grid>
10         <TextBlock Text="TextA" 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 ViewInjection
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     }
25 }

 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值