2Bbear's knowledge workshop

https://github.com/PrismLibrary/Prism-Samples-Wpf/tree/master/08-ViewModelLocator/ViewModelLocator

코드 참조


https://github.com/2Bbear/WindowsProgrmaDevelop/tree/master/WPF/UsingMvvmPrismLibrary/08-ViewModelLocator

내가 만든 코드참조


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using Prism.Ioc;
using Prism.Unity;
using System.Windows;
using ViewModelLocator.Views;
 
namespace ViewModelLocator
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }
 
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
 
        }
    }
}
 
cs


시작 윈도우는 역시 Mainwindow


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using Prism.Mvvm;
 
namespace ViewModelLocator.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "Prism Unity Application";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
 
        public MainWindowViewModel()
        {
 
        }
    }
}
 
cs


ViewModelLocator라는 폴더 안에 MainWindowViewModel이라는 파일을 보니.

BindableBase라는 상속을 받은 형태의 클래스가 존재하고 있었다.


단순히 프로퍼티가 존재 할 뿐이고, 특이하게 set에 SetProperty라는 함수가 있다

SetProperty메소드는 bindalbleBase의 메소드로. ref 형태의 매개변수로 들어가는 값을 참조자를 넘겨주고 있다. 즉 실제값을 넘기고 있다는 말이다.


1
2
3
4
5
6
7
8
9
10
11
<Window x:Class="ViewModelLocator.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/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="350" Width="525">
    <Grid>
        <ContentControl prism:RegionManager.RegionName="ContentRegion" />
    </Grid>
</Window>
 
cs


MainWindow의 XAML은 title에 바인딩이 걸려잇는데. 

하단의 ContentControl은 딱히 불러오는 RegionManager에서 ContentRegion이라는 등록명으로 등록한 View가 없어 출력이 되지 않을 것이다.


실행을 시키면


이렇게 실행 제목창에 만든 MainwindowViewModel의 title변수에 있는 값이 나타나게 된다.


이렇게 바인딩이 가능하게 되는 코드는


XAML에 있는 prism:ViewModelLocator.AutoWireViewModel="True" 이 있기 때문이다.

이 말은 prism 라이브러리 안에 있는 ViewModellocator의 AutoWireViewModel의 값을 True로 변경하라는 의미이다.


그럼 어떻게 Prism은 View와 ViewModel을 연관지었을까?

예상은 아무래도 이름의 형식에 맞추어서 찾은 것으로 생각된다. 간단하게 새로운 윈도우를 만들어 시험해보자


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
using System.Windows;
 
 
namespace ViewModelLocator.Views
{
    /// <summary>
    /// MyWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MyWindow : Window
    {
        public MyWindow()
        {
            InitializeComponent();
        }
    }
}
 
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<Window x:Class="ViewModelLocator.Views.MyWindow"
        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:ViewModelLocator.Views"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
      
        Title="{Binding Title}" Height="350" Width="525">
    <Grid>
        
    </Grid>
</Window>
 
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
using Prism.Ioc;
using Prism.Unity;
using System.Windows;
using ViewModelLocator.Views;
 
namespace ViewModelLocator
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MyWindow>();
        }
 
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
 
        }
    }
}
 
cs



새롭게 MyWindow라는 것을 추가하고 


MyWindowViewModel 이라는 것을 만들어

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
using Prism.Mvvm;
namespace ViewModelLocator.ViewModels
{
    class MyWindowViewModel: BindableBase
    {
        private string _title = "Prism Unity Applicationddd";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
 
        public MyWindowViewModel()
        {
 
        }
    }
}
 
cs



다르게 출력할 수 있게 만들어놓았다.




실행 해보면



간단하게 값이 바뀐 것을 확인 할 수 있다.


이로서 ViewLocator를 이용하여 Prism은 이름만으로 뷰와 뷰 모델을 연관지을 수 있다는 것을 알았다.