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文檔入門。但不要工作。