[英]Why Mvvm approach is not working for my Calculator?
I am trying to work on MVVM using silverlight 5 and my xaml code is this: 我正在尝试使用silverlight 5在MVVM上工作,而我的xaml代码是这样的:
<Grid x:Name="LayoutRoot" Background="White" Height="100" Width="350">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox Grid.Column="0" Text="{Binding FirstValue, Mode=TwoWay}" Height="25" TextAlignment="Right"/>
<TextBlock Grid.Column="1" Text="+" Height="25" TextAlignment="Center"/>
<TextBox Grid.Column="2" Text="{Binding SecondValue, Mode=TwoWay}" Height="25" TextAlignment="Right"/>
<TextBlock Grid.Column="3" Text="=" Height="25" TextAlignment="Center"/>
<TextBlock Grid.Column="4" Text="{Binding Result, Mode=OneWay}" Height="25" TextAlignment="Left"/>
<Button Grid.Row="1" Grid.ColumnSpan="5" Margin="0,5,0,0" Content="Calculate" Command="{Binding CalculateCommand}" />
</Grid>
And 和
public MainPage()
{
InitializeComponent();
this.DataContext = new CalculatorViewModel();
}
My CalculatorViewModel.cs class is : 我的CalculatorViewModel.cs类是:
public class CalculatorViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public CalculatorModel cc;
private string _firstValue;
public string FirstValue
{
get { return _firstValue; }
set
{
_firstValue = value;
OnPropertyChanged("FirstValue");
}
}
private string _secondValue;
public string SecondValue
{
get { return _secondValue; }
set
{
_secondValue = value;
OnPropertyChanged("SecondValue");
}
}
private string _result;
public string Result
{
get { return _result; }
private set
{
_result = value;
OnPropertyChanged("Result");
}
}
private ICommand _calculateCommand;
public ICommand CalculateCommand
{
get { return _calculateCommand; }
}
public CalculatorViewModel()
{
cc = new CalculatorModel();
cc.FirstValue = _firstValue;
cc.SecondValue = _secondValue;
cc.Result = _result;
_calculateCommand = new RelayCommand(cc.Calculate) { IsEnabled = true };
//This Calculate() is defined in Model class (CalculatorModel.cs).
}
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
}
}
}
And i wanted to define this calculate delegate in Model class(CalculatorModel.cs).According to what i understand this is what for Model class are meant. 我想在Model类(CalculatorModel.cs)中定义此计算委托。根据我的理解,这是Model类的含义。 ( please correct me if i am wrong ).
( 如果我错了,请纠正我 )。
I done like this: 我这样做是这样的:
public class CalculatorModel
{
#region PROPERTIES
public string FirstValue;
public string SecondValue;
public string Result;
#endregion
//CalculatorViewModel cv = new CalculatorViewModel();
public CalculatorModel()
{
this.Result = "";
this.FirstValue = "";
this.SecondValue = "";
}
public void Calculate()
{
MessageBox.Show("check : " +FirstValue);
Result = (Convert.ToInt32(FirstValue) + Convert.ToInt32(SecondValue)).ToString();
}
}
The problem is i dont know why on debugging the value of Result, FirstValue and SecondValue are null the delegate calculate()
is called on button click but there is no output updated in Result. 问题是我不知道为什么在调试Result,FirstValue和SecondValue的值为null时 ,单击按钮时会调用委托
calculate()
但是Result中没有更新的输出。
Could some one please correct me whats wrong in my approach of defining calculate() in Model class ? 有人可以纠正我在Model类中定义calculate()方法的问题吗?
You don't seem to be updating the values in the Model. 您似乎没有更新模型中的值。 Databinding only works between View and ViewModel.
数据绑定仅在View和ViewModel之间起作用。
private string _firstValue;
public string FirstValue
{
get { return _firstValue; }
set
{
_firstValue = value;
cc.FirstValue = _firstValue; // add this
OnPropertyChanged("FirstValue");
}
}
and do that for SecondValue as well. 并为SecondValue做到这一点。 Change the command so that the result is assigned to Result.
更改命令,以便将结果分配给Result。
This tells you that your setup is not the most efficient or elegant. 这告诉您设置不是最有效或最优雅的。 Consider
考虑
When you make the Model implement INPC you can (should) remove the FirstValue, SecondValue and Result entirely from the VM. 使模型实现INPC时,您可以(应该)从VM中完全删除FirstValue,SecondValue和Result。 You can bind to the Model:
Text="{Binding Path=cc.FirstValue, Mode=TwoWay}"
and then make the Model a property: 您可以绑定到模型:
Text="{Binding Path=cc.FirstValue, Mode=TwoWay}"
,然后将Model设置为属性:
public CalculatorModel cc { get; set; } // must be a property
Otherwise, the better pattern for 'repeating' a property is: 否则,“重复”属性的更好模式是:
//private string _firstValue;
public string FirstValue
{
get { return cc.FirstValue; }
set
{
//_firstValue = value;
cc.FirstValue = value; // add this
OnPropertyChanged("FirstValue");
}
}
avoid storing data twice. 避免两次存储数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.