C# 为什么Mvvm方法不适用于我的计算器?

C# 为什么Mvvm方法不适用于我的计算器?,c#,.net,silverlight,mvvm,data-binding,C#,.net,Silverlight,Mvvm,Data Binding,我正在尝试使用silverlight 5处理MVVM,我的xaml代码如下: <Grid x:Name="LayoutRoot" Background="White" Height="100" Width="350"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> <Co

我正在尝试使用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>
我的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));
            }
        }

    }
我想在模型类(CalculatorModel.cs)中定义这个计算委托。根据我的理解,这就是模型类的含义。(如果我错了,请纠正我

我是这样做的:

 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();
        }
    }
问题是我不知道为什么调试结果的值时,FirstValue和SecondValue为空
在单击按钮时调用委托
calculate()
,但结果中没有更新的输出


有人能纠正我在模型类中定义calculate()的方法有什么错误吗?

您似乎没有更新模型中的值。数据绑定仅在视图和ViewModel之间起作用

    private string _firstValue;
    public string FirstValue
    {
        get { return _firstValue; }
        set
        {
            _firstValue = value;
            cc.FirstValue = _firstValue;     // add this
            OnPropertyChanged("FirstValue");
        }
    }
也为SecondValue这样做。更改命令,以便将结果指定给结果

这表明您的设置不是最有效或最优雅的。 考虑

  • 使模型INotifyPropertyChanged并绑定到Model.FirstValue
  • 或者,把模型的概念放在这里 当您使模型实现INPC时,您可以(应该)完全从VM中删除FirstValue、SecondValue和Result。您可以绑定到模型:
    Text=“{Binding Path=cc.FirstValue,Mode=TwoWay}”
    ,然后将模型设为属性:

       public CalculatorModel cc { get; set; }   // must be a property
    
    否则,“重复”属性的更好模式是:

        //private string _firstValue;
        public string FirstValue
        {
            get { return cc.FirstValue; }
            set
            {
                //_firstValue = value;
                cc.FirstValue = value;           // add this
                OnPropertyChanged("FirstValue");
            }
        }
    

    避免存储数据两次。

    谢谢您的回答,但现在调试时,我可以看到文本框中写入的值是可访问的,但结果尚未更新。知道为什么吗?(我已经做了你建议的所有三个的更改,我的意思也是为了结果)你的意思是这个更改:private ICommand\u calculateCommand;public ICommand CalculateCommand{get{return\u CalculateCommand;}set{cc.Result=\u Result;}}}不,看起来不对。大致:
    newrelaycommand(()=>Result=cc.Calculate())
    我需要更多帮助。您在回答中提到的两点是否意味着:ViewModel类不适合更改InotifyProperty。必须在模型中完成吗?你能在你的答案中详细解释一下哪个类应该包含什么吗?(因为在许多示例中,我没有在model类中看到,它们只是在ViewModel类中执行所有操作,所以我对此有点困惑)这是一个单独的问题,但可以使用
    cc=new CalculatorModel()跟踪逻辑。这叫做相互重复。
    
        //private string _firstValue;
        public string FirstValue
        {
            get { return cc.FirstValue; }
            set
            {
                //_firstValue = value;
                cc.FirstValue = value;           // add this
                OnPropertyChanged("FirstValue");
            }
        }