运行MVVM时,EventHandler为Null

时间:2018-05-06 14:59:41

标签: c# mvvm

所以我试图在我的WPF应用程序中共享变量。在这样做的过程中,我使用ViewModelController实现了eventhandler类来存储影响我应用其余部分的变量。

我的ViewModelController课程是这样的:

    public class ViewModelController
    {

        private DataRowView _SelectedUniverse;

        public MenuViewModel MenuViewModel { get { return new MenuViewModel(this); } }

        public GamePageViewModel GamePageViewModel { get { return new GamePageViewModel(this); } }

        public PlayersViewModel PlayersViewModel { get { return new PlayersViewModel(this); } }

        public event EventHandler UniverseChanged;

        public DataRowView SelectedUniverse
        {
            get { return _SelectedUniverse; }
            set
            {
                _SelectedUniverse = value;
                UniverseChanged.Invoke(this, EventArgs.Empty);
                Debug.Print("Universe Changed in ViewModelController");
            }
        }

    }

我在我的菜单ViewModel上测试它,看看它是否传递到另一个类:

MenuViewModel

    public class MenuViewModel : ObservableObject
    {

        ViewModelController _ViewModelController;
        private DataView _Universes;
        private DataRowView _SelectedUniverse;

        public MenuViewModel(ViewModelController controller)
        {
            _ViewModelController = controller;
            _Universes = Controller.UniverseTableAdapter.GetData().DefaultView;
        }

        public DataView Universes
        {
            get { return _Universes; }
            set
            {
                _Universes = value;
                RaisePropertyChangedEvent("Universes");
            }
        }

        public DataRowView SelectedUniverse
        {
            get { return _SelectedUniverse; }
            set
            {
                _SelectedUniverse = value;
                Debug.Print("Universe Changed from Menu");
                _ViewModelController.SelectedUniverse = _SelectedUniverse;
                RaisePropertyChangedEvent("SelectedUniverse");
            }
        }
    }

PlayerViewModel班级

public class PlayersViewModel : ObservableObject
{

    private ViewModelController _ViewModelController;

    #region Variables
    private ObservableCollection<Player> _Players = Controller.GetPlayers(0);
    private Player _SelectedPlayer;
    private DataView _Colleges = Controller.CollegeTableAdapter.GetData().DefaultView;
    private DataView _Universes = Controller.UniverseTableAdapter.GetData().DefaultView;

    private long _PlayerID;
    private string _PlayerFirstName;
    private string _PlayerLastName;
    private string _PlayerFullName;
    private DataRowView _SelectedCollege;
    private DataRowView _SelectedUniverse;
    private long _PlayerCollegeID;
    private long _UniverseID;
    private int _PlayerEnteredYear;

    private DataView _RegularSeasonStats = Controller.SeasonView.GetData().DefaultView;
    private DataRowView _SelectedSeason;
    private DataView _PlayoffStats = Controller.PlayoffsView.GetData().DefaultView;
    private DataView _PlayerSchedule = Controller.PlayerScheduleView.GetDataByPersonAndYear(0, 0).DefaultView;
    #endregion 

    public PlayersViewModel(ViewModelController controller)
    {
        _ViewModelController = controller;
        _ViewModelController.UniverseChanged += OnUniverseChanged;
        Debug.Print("PlayerViewModel Constructed");
    }

    public DataRowView SelectedUniverse
    {
        get { return _SelectedUniverse; }
        set
        {
            _SelectedUniverse = value;
            if(_SelectedUniverse != null)
            {
                _UniverseID = _SelectedUniverse.Row.Field<long>("UniverseID");
                RaisePropertyChangedEvent("SelectedUniverse");
            }
        }
    }

    private void OnUniverseChanged(object sender, EventArgs e)
    {
        SelectedUniverse = _ViewModelController.SelectedUniverse;
        Debug.Print("Universe was changed");
    }
}

更改SelectedUniverse后,它会将该变量发送到ViewModelController,该UniverseChanged应调用Object Reference Not Set to an Instance of an Object事件。但是,我收到EventHandler

我知道我应该实例化RewriteRule ^topic/([^/]+?-)?([^/-]+)$ index.php?page=topic&server_id=$2&name=$1 ,但我没有看到在没有构造函数的情况下实例化它的方法。我可以解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

这里的问题是你强迫调用UniverseChanged事件但是没有侦听器来处理它,所以事件实际上可以为null。

您应该在调用之前检查是否有侦听器。像这样:

public DataRowView SelectedUniverse
{
    get { return _SelectedUniverse; }
    set
    {
        _SelectedUniverse = value;
        UniverseChanged?.Invoke(this, EventArgs.Empty);
        // Or if you're using a C# version< 6:
        // var evtHandlers = UniverseChanged;
        // if(evtHandlers != null) evtHandlers.Invoke(this, EventArgs.Empty);
        Debug.Print("Universe Changed in ViewModelController");
    }
}

我希望它有所帮助。