■ IRegion 인터페이스에서 뷰 주입을 사용해 수동으로 뷰를 추가하거나 제거하는 방법을 보여준다.
▶ MainView.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<UserControl x:Class="TestProject.Views.MainView" 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" mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="450"> <Grid> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="36" Text="Main View" /> </Grid> </UserControl> |
▶ MainView.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System.Windows.Controls; namespace TestProject.Views { /// <summary> /// 메인 뷰 /// </summary> public partial class MainView : UserControl { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainView() /// <summary> /// 생성자 /// </summary> public MainView() { InitializeComponent(); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<Window x:Class="TestProject.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" Width="800" Height="600" Title="Shell" FontFamily="나눔고딕코딩" FontSize="16"> <DockPanel LastChildFill="True"> <Button DockPanel.Dock="Top" Padding="5" Content="뷰 추가하기" Click="button_Click" /> <ContentControl prism:RegionManager.RegionName="ContentRegion" /> </DockPanel> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
using System.Windows; using Prism.Ioc; using Prism.Regions; namespace TestProject.Views { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 컨테이너 확장 인터페이스 /// </summary> private IContainerExtension containerExtension; /// <summary> /// 영역 관리자 인터페이스 /// </summary> private IRegionManager regionManager; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow(containerExtension, regionManager) /// <summary> /// 생성자 /// </summary> /// <param name="containerExtension">컨테이너 확장 인터페이스</param> /// <param name="regionManager">영역 관리자 인터페이스</param> public MainWindow(IContainerExtension containerExtension, IRegionManager regionManager) { InitializeComponent(); this.containerExtension = containerExtension; this.regionManager = regionManager; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 버튼 클릭시 처리하기 - button_Click(sender, e) /// <summary> /// 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void button_Click(object sender, RoutedEventArgs e) { MainView view = containerExtension.Resolve<MainView>(); IRegion region = regionManager.Regions["ContentRegion"]; region.Add(view); } #endregion } } |
▶ MainApplication.xaml
1 2 3 4 5 6 7 |
<prism:PrismApplication x:Class="TestProject.MainApplication" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/"> </prism:PrismApplication> |
▶ MainApplication.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using System.Windows; using Prism.Ioc; using Prism.Unity; using TestProject.Views; namespace TestProject { /// <summary> /// 메인 애플리케이션 /// </summary> public partial class MainApplication : PrismApplication { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 쉘 생성하기 - CreateShell() /// <summary> /// 쉘 생성하기 /// </summary> /// <returns>쉘</returns> protected override Window CreateShell() { return Container.Resolve<MainWindow>(); } #endregion #region 타입 등록하기 - RegisterTypes(containerRegistry) /// <summary> /// 타입 등록하기 /// </summary> /// <param name="containerRegistry">컨테이너 레지스트리 인터페이스</param> protected override void RegisterTypes(IContainerRegistry containerRegistry) { } #endregion } } |