使用MVVM创建保存/加载功能

时间:2016-10-21 23:14:11

标签: c# wpf mvvm

我正在尝试创建一个可以以XML格式保存/加载的函数。问题是我在MainViewModel上做错了导致一些错误。我这样做了,并评论了我得到错误的地方:

namespace Editor
{
    public class MainViewModel : INotifyPropertyChanged
    {

        private double _x;
        private object _content;
        public object Content
        {
            get { return _content; }
            set
            {
                _content = value;
                OnPropertyChanged(nameof(Content));
            }
        }

        public double X
        {
            get { return _x; }
            set
            {
                _x = value;
                OnPropertyChanged(nameof(X));
            }
        }

        public ICommand BtnLoadCommand { get; }

        public ICommand BtnGemCommand { get; }

        public double Y { get; set; }

        public MainViewModel()
        {
            Content = new Save_Load(); //the type or namespace could not be found are you missing an assembly 
            BtnLoadCommand = new RelayCommand(Load); //cannot convert from method group to action
            BtnGemCommand = new RelayCommand(Save); //cannot convert from method group to action
        }

        private void Load(object sender, EventArgs e)
        {
            OpenFileDialog loadfiledialog = new OpenFileDialog();
            if (loadfildialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

            }
        }

        private void Save(object sender, EventArgs e)
        {
            SaveFileDialog gemfiledialog = new SaveFileDialog();
            if (gemfildialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

            }

        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

尝试以下

BtnLoadCommand = new RelayCommand<object>(Load); 
BtnGemCommand = new RelayCommand<object>(Save);