티스토리 뷰

반응형

Windows 8 Consumer Preview에서는 기본적으로 MEF를 지원하고 있다. 그런데, 이전과는 약간 다르게 SatisfyImports를 해야해서 그 부분에 대한 설명을 잠깐 하도록 하겠다.

예제는 예전에 사용했던 HelloWorld 예제를 MEF를 사용하도록 수정했다.

1. BlankPage.xaml

<Page
    x:Class="HelloWorld.BlankPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:HelloWorld"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ViewModels="using:HelloWorld.ViewModels"
    mc:Ignorable="d">

    <d:DataContext>
        <ViewModels:BlankPageViewModel/>
    </d:DataContext>
   
    <Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
        <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock x:Name="tbTitle" Text="{Binding HelloWorldText}" FontSize="36"/>
        </Grid>
    </Grid>
</Page>

2. BlankPage.xaml.cs

namespace HelloWorld
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class BlankPage : Page
    {
        [Import]
        public BlankPageViewModel ViewModel
        {
            get { return this.DataContext as BlankPageViewModel; }
            set
            {
                //임포트가 되면 바로 DataContext에 입력

                this.DataContext = value;
                //이벤트 연결 - InvokeCommandAction이 아직 없다.
                tbTitle.Tapped += value.TextBlockTapped;
            }
        }

        public BlankPage()
        {
            this.InitializeComponent();

            if (DesignMode.DesignModeEnabled == true)
            {
            }
            else
            {
                //MEF 컨테이너 구성 - 딱 한번만 할 수 있다.(이 문장을 부르기 전에 Import를 해야하는 녀석들을 모두 모아 놓고 실행하는 것이 좋다
                var assembly = this.GetType().GetTypeInfo().Assembly;
                using (var catalog = new AssemblyCatalog(assembly))
                {
                    using (var service = catalog.CreateCompositionService())
                    {
                        service.SatisfyImportsOnce(this);
                    }
                }

            }
        }


        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
    }
}

3. BlankPageViewModel.cs

    //뷰 모델 익스포트
    [Export]
    public class BlankPageViewModel : BindableBase
    {
        int tabCount;

        private string helloWorldText;

        public string HelloWorldText
        {
            get { return helloWorldText; }
            set
            {
                helloWorldText = value;
                //보너스 OnPropertyChanged와 SetProperty의 차이를 찾아서 알아 놓으면 좋다
                OnPropertyChanged();
            }
        }

        public BlankPageViewModel()
        {
            //SetProperty<string>(ref helloWorldText, "Hello World in Windows 8 Metro style app [MEF]");
            HelloWorldText = "Hello World in Windows 8 Metro style app [MEF]";
            tabCount = 0;
        }

        public void TextBlockTapped(object sender, TappedRoutedEventArgs e)
        {
            tabCount++;
            SetProperty<string>(ref helloWorldText, "User Tabed!! " + tabCount.ToString(), "HelloWorldText");
        }
    }

 

 

 

 4. Source

HelloWorld.zip

반응형
댓글