簡體   English   中英

為什么Mvvm方法對我的計算器不起作用?

[英]Why Mvvm approach is not working for my Calculator?

我正在嘗試使用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>

public MainPage()
        {
            InitializeComponent();
            this.DataContext = new CalculatorViewModel();
        }

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

    }

我想在Model類(CalculatorModel.cs)中定義此計算委托。根據我的理解,這是Model類的含義。 如果我錯了,請糾正我 )。

我這樣做是這樣的:

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

問題是我不知道為什么在調試Result,FirstValue和SecondValue的值為null時 ,單擊按鈕時會調用委托calculate()但是Result中沒有更新的輸出。

有人可以糾正我在Model類中定義calculate()方法的問題嗎?

您似乎沒有更新模型中的值。 數據綁定僅在View和ViewModel之間起作用。

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

並為SecondValue做到這一點。 更改命令,以便將結果分配給Result。

這告訴您設置不是最有效或最優雅的。 考慮

  1. 使模型成為INotifyPropertyChanged並綁定到Model.FirstValue
  2. 或者,在這里放下模型的概念。

使模型實現INPC時,您可以(應該)從VM中完全刪除FirstValue,SecondValue和Result。 您可以綁定到模型: Text="{Binding Path=cc.FirstValue, Mode=TwoWay}" ,然后將Model設置為屬性:

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

避免兩次存儲數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM