如何修复PropertyChanged null(特定情况)

时间:2019-05-22 08:19:27

标签: c# .net wpf mvvm

由于创建新的Class _Date,我得到了空的PropertyChanged,这对游戏过程有帮助。如何将值发送到XAML并保存Class _Date。需要更新currentMoney和currentPolution。如果我在MainWindow.xaml.cs中更新,则这2个属性都可以。但是从_Date开始,PropertyChanged为NULL。

public class _Date : ViewModelBase {

    private DateTime today;
    private DateTime destinationDay;
    private Polutions.Pollution.Pollution pollution = MainWindow.currentPollution;
    private Money.Money money = MainWindow.currentMoney;

    public DateTime Today {
        get => today;
        set {
            today = value;
            OnPropertyChanged(nameof(Today));
        }
    }

    // Property for count of simulation days
    public int DayCount { get; set; }


    public _Date() {
        today = DateTime.Today;
        destinationDay = new DateTime();
        pollution = new Polutions.Pollution.Pollution();
        money = new Money.Money();
    }

    //Simulation of game process 
    //TODO:
    /*
        Problem: Don't displaying changing of pollution + money

        Possible solution: Delete _Date class, probably because of it PropertyChanged NULL!

    */
    public void startSimulation() {
        destinationDay = today;
        destinationDay = destinationDay.AddDays(DayCount);
        for (; Today.Date < destinationDay.Date; Today = Today.AddDays(1)) {

            pollution.CurrentPollution += 0.1;
            money.CurrentMoney -= 1000;
            DayCount--;

        }
    }

}

MainWindow.xaml.cs

public partial class MainWindow : Window {

        public static Money currentMoney;
        public static Pollution currentPollution;
        public static _Date date;

        public MainWindow () {

            InitializeComponent();

            currentMoney = new Money();
            currentPollution = new Pollution();
            date = new _Date();

            CurrentMoney.DataContext = currentMoney;
            CurrentPollution.DataContext = currentPollution;
            StartDateLabel.DataContext = date;

        }

        private void Initialize() {
            Facktories.Content = Facktories.Content.ToString() + FactoriesList.Quantity;
            FactoriesPollution.Content = FactoriesPollution.Content.ToString() + FactoriesList.FullPolution;
        }

        private void Window_ContentRendered(object sender, EventArgs e) {           
            Initialize();
        }

        private void Button_Click(object sender, RoutedEventArgs e) {

            date.DayCount = Convert.ToInt32(DayCountBox.Text);

            date.startSimulation();

        }
    }

0 个答案:

没有答案