2016-11-07 21 views
0

私は学習catel mvvm frameworkですが、なんらかの理解上の問題があります。 catelフレームワークを使用して、シンプルなプロジェクトを簡単に構築しようとしています。 は、その後のviewmodelシンプルなcatel dependency injectionのやり方

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)); 
} 

は、最後のビュー

<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 FirstService : IFirstService 
{ 
    public First Read() 
    { 
     return new First(); 
    } 

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

を持っているモデル

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を実行するかは、両方のテキストボックスの開始アプリケーションでは、ファーストクラスのデフォルトデータでした。また、Writeボタンを押すと、誰かが最初のテキストボックスに入力した人物を、サービスを使用して2番目のテキストボックスにコピーすることができます。私は例でこれをやろうとしていますcatelのドキュメントから始めましょう。しかし、働かないでください。

答えて

1

あなたがあなたのコードを簡素化するようになるCatel.Fody nugetパッケージを使用することができ、すべての最初:

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() 
    } 
} 

あなたが依存性注入を使用したい場合は、あなたが最初の注射の前にどこかのコードで、あなたにサービスを登録する必要があります。たとえば、あなたは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>(); 

にすべてのサービスを登録することを行うことができますもう一つの重要なことは、各ビューごとに1つのビューモデルを使用する必要があるので、私はその部分であなたを本当に下回らないようにしてください。あなたにxamlを1つのファイルに入れてみてください(あなたの場合はウィンドウだと思います)

関連する問題