2016-11-07 15 views
0

我正在學習catel mvvm框架,並且有一些理解上的麻煩。 嘗試構建簡單的項目,非常簡單,使用catel框架。 有一個模型如何做簡單的catel依賴注入

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

然後讓視圖模型

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> 

和窗

<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依賴注入,即在兩個文本框中啓動應用程序時都是來自First類的默認數據。以及如何通過使用服務按Write按鈕將第一個文本框中輸入的人複製到第二個文本框。我嘗試通過示例做到這一點從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>(); 

註冊您的所有服務的另一個重要的事情是,你必須每每個視圖使用一個視圖模型,所以我在那一部分中並沒有真正地低估你。只是試圖把你xaml放入單個文件中(在你的情況下,我認爲它是窗口)