在ICommand实现、ViewModel和View之间共享数据

本文关键字:View 之间 共享 数据 ViewModel ICommand 实现 | 更新日期: 2023-09-27 17:53:50

我正在做我在。net的第一个项目,我试图使用MVVM模式。我如何在我的ViewModel和我的iccommand类之间共享数据?我的ViewModel看起来像这样:

public class WorkClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private Book mybook;
    private ObservableCollection<Book> bookList;
    public Book Mybook
    {
        get { return mybook; }
        set
        {
            book = value;
            OnPropertyChanged("Mybook");
        }
    }
    public ObservableCollection<Book> BookList
    {
        get { return bookList; }
        private set
        {
            bookList = value;
            OnPropertyChanged("BookList");
        }
    }
    testCommand2 test = new testCommand2();
    public ICommand Test { get { return test; } }
    private class testCommand2 : ICommand
    {
        public void Execute(object parameter)
        {
            // Do stuff here
        }
        public bool CanExecute(object parameter)
        {
            return true;
        }
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

我实现了iccommand作为内部类,但我也尝试了一个"独立"类,但也不能让它工作。我想我的命令"测试"工作,并与WorkClass属性BookList和Mybook共享数据。(Book是类I定义的只包含一些字符串属性)

我的应用程序现在有两个窗口,在主窗口中,我创建WorkClass的实例并将其插入其数据上下文。然后我将它通过数据上下文传递到像这样的其他窗口(这是正确的吗?):

Window1 w = new Window1();
w.DataContext = this.DataContext;
w.ShowDialog();

我希望我的命令从那些窗口来执行数据更改从这个WorkClass实例。我希望你明白我的要求。我知道最好使用RelayCommand或DelegateCommand课程,这些课程可以通过互联网获得,但由于这是学校的项目,我担心它会被视为抄袭别人的作品。

如果你需要我从代码中发布更多或解释任何东西,我将编辑。

谢谢你的帮助。

在ICommand实现、ViewModel和View之间共享数据

您需要通过视图模型实例初始化命令,并将命令执行委托给视图模型类的方法

public class WorkClass : INotifyPropertyChanged
{
    public WorkClass()
    {
        test = new testCommand2(this);
    }
    // Will be called on command execturion
    public Test2Action()
    {
    }
public event PropertyChangedEventHandler PropertyChanged;
private Book mybook;
private ObservableCollection<Book> bookList;
public Book Mybook
{
    get { return mybook; }
    set
    {
        book = value;
        OnPropertyChanged("Mybook");
    }
}
public ObservableCollection<Book> BookList
{
    get { return bookList; }
    private set
    {
        bookList = value;
        OnPropertyChanged("BookList");
    }
}
testCommand2 test;
public ICommand Test { get { return test; } }
private class testCommand2 : ICommand
{
    private readonly WorkClass _wc;
    public testCommand2(WorkClass wc)
    {
       _wc = wc;
    }
    public void Execute(object parameter)
    {
        _wc.Test2Action();
    }
    public bool CanExecute(object parameter)
    {
        return true;
    }
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
}
protected void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }
}

}