2016-11-07 4 views
0

나는 catel mvvm 프레임 워크를 배우고 있고 약간의 이해 문제가있다. Catel 프레임 워크를 사용하여 간단한 프로젝트를 매우 간단하게 빌드하려고 시도합니다. 그런 다음 뷰 모델간단한 catel 의존성 주사를하는 방법

public class FirstViewModel : ViewModelBase 
{ 
    public FirstViewModel(First first) 
    { 
     Argument.IsNotNull(() => first); 

     First = first; 
    } 

    [Model] 
    public First First 
    { 
     get { return GetValue<First>(TestModelProperty); } 
     private set { SetValue(TestModelProperty, value); } 
    } 
    public static readonly PropertyData TestModelProperty = RegisterProperty("TestModel", typeof(First)); 

    [ViewModelToModel("First")] 
    public String FirstValue 
    { 
     get { return GetValue<String>(FirstValueProperty); } 
     set { SetValue(FirstValueProperty, value); } 
    } 
    public static readonly PropertyData FirstValueProperty = RegisterProperty("FirstValue", typeof(String)); 

    [ViewModelToModel("First")] 
    public String SecondValue 
    { 
     get { return GetValue<String>(SecondValueProperty); } 
     set { SetValue(SecondValueProperty, value); } 
    } 
    public static readonly PropertyData SecondValueProperty = RegisterProperty("SecondValue", typeof(String)); 
} 

에서 마지막이 서비스

public class FirstService : IFirstService 
{ 
    public First Read() 
    { 
     return new First(); 
    } 

    public First Write(First first) 
    { 
     first.SecondValue = first.FirstValue; 
     return first; 
    } 
} 

사용하여 볼

<catel:StackGrid> 
     <catel:StackGrid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
     </catel:StackGrid.RowDefinitions> 
     <catel:StackGrid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="Auto" /> 
     </catel:StackGrid.ColumnDefinitions> 
     <Label Content="First value" /> 
     <TextBox Text="{Binding FirstValue}" TextAlignment="Center" Width="100"/> 
     <Label Content="Second value" /> 
     <TextBox Text="{Binding FirstSecond}" TextAlignment="Center" Width="100"/> 
    </catel:StackGrid> 

그리고 창

012이 모델

public class First : ModelBase 
{ 
    public String FirstValue 
    { 
     get { return GetValue<String>(FirstValueProperty); } 
     set { SetValue(FirstValueProperty, value); } 
    } 

    public static readonly PropertyData FirstValueProperty = RegisterProperty("FirstValue", typeof(String), "First Text"); 

    public String SecondValue 
    { 
     get { return GetValue<String>(SecondValueProperty); } 
     set { SetValue(SecondValueProperty, value); } 
    } 

    public static readonly PropertyData SecondValueProperty = RegisterProperty("SecondValue", typeof(String), "Second text"); 
} 

되세요

<catel:StackGrid x:Name="LayoutRoot"> 
    <catel:StackGrid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
    </catel:StackGrid.RowDefinitions> 
    <Label Content="{Binding Title}" /> 
    <views:FirstView /> 
    <Button Content="Write" Command="{Binding WriteFirst}" Width="70" /> 
</catel:StackGrid> 

sipmle catel dependency injection을 수행하는 방법, 두 텍스트 상자의 시작 응용 프로그램에서 First class의 기본 데이터였습니다. 그리고 쓰기 단추를 눌러 누군가가 서비스를 사용하여 두 번째 텍스트 상자에 첫 번째 텍스트 상자에 입력 복사하십시오. 나는 catel 문서에서 시작하는 예제를 통해 이것을 시도했다. 그러나 일하지 마십시오. 당신이 의존성 주입을 사용하려는 경우

public class FirstModel : ModelBase 
{ 
    public String FirstValue { get; set; } 
    public String SecondValue { get; set; } 
} 

public class FirstViewModel : ViewModelBase 
{ 
    private readonly IFirstService _firstService; 

    public FirstViewModel(IFirstService firstService) 
     : this(new First(), firstService) 
    { 

    } 

    public FirstViewModel(First first, IFirstService firstService) 
    { 
     Argument.IsNotNull(() => first); 
     Argument.IsNotNull(() => firstService); 

     _firstService = firstService; 
     First = first; 

     WriteFirst = new Command(OnWriteFirstExecute); 
    } 

    [Model] 
    // you can use ExposeAttribute if you don't want to use 'FirstValue' 
    // property inside of ViewModel and only want to use it for binding 
    [Expose(nameof(FirstModel.FirstValue))] 
    public FirstModel First { get; set; } 

    [ViewModelToModel(nameof(First)] 
    public String SecondValue { get; set; } 

    public Command WriteFirst { get; } 

    private void OnWriteFirstExecute() 
    { 
     // here you can put you logic (not sure what you want to achieve) 
     // _firstService.Write() 
    } 
} 

, 당신이 먼저 주입하기 전에 어디 코드에서 서비스를 등록해야 : 우선

답변

1

당신은 당신이 당신의 코드를 단순화 할 수있게된다 Catel.Fody nuget 패키지를 사용할 수 있습니다. 예를 들어, 당신은 App.xaml.cs를 또는 당신이 ModileInit.Fody nuget 패키지를 사용할 수 있으며 ModuleInitializer.cs

var serviceLocator = this.GetServiseLocator(); 
// or instatic context you can use: 
// var serviceLocator = ServiceLocator.Default; 
serviseLocator.RegisterType<IFirstService, FirstService>(); 

의 모든 서비스를 등록하는 것이 할 수있는 또 다른 중요한 것은 각보기 당 하나의 뷰 모델을 사용할 필요가있다 그래서 나는 당신을 그 부분에서 정말로 강조하지 마십시오. 그냥 xaml을 하나의 파일에 넣으려고하십시오 (귀하의 경우 윈도우라고 생각합니다)