在UserControl中使用ViewModel库 [英] Using a ViewModel base in a UserControl

查看:115
本文介绍了在UserControl中使用ViewModel库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个ViewModel基础,我在其他所有ViewModel和Models中都使用过,它运行得很好。它支持INotifyPropertyChanged,ICommand等...


但是,我现在正在创建一些UserControls,我需要创建一些ICommands,但我宁愿不必重新创建每个UserControl中的ICommand实现。


在这些UserControls中使用我的ViewModel基础的最佳方法是什么?


谢谢!

解决方案

嗨T Gregory,


根据您的描述,您希望为许多UserControl实现ICommand,但是你不要我想为每个UserControl重新创建ICommand实现,对吗?


如果是,我建议您创建一个实现ICommand接口的新类,如下所示:

 public class Relaycommand:ICommand 
{
readonly Action _execute;

public Relaycommand(Action execute)
{
if(execute == null)
throw new ArgumentNullException(" execute");
_execute = execute;
}

公共事件EventHandler CanExecuteChanged;

public bool CanExecute(object parameter)
{
return true;
}
public void执行(对象参数)
{
_execute();
}
}

然后为UserControl创建viewmodel,引用RelayCommand类, 为此viewmodel创建许多RelayCommand属性,如下所示:

 public class viewmodel1 
{
public RelayCommand command1 {get;组; }

public viewmodel1()
{
command1 = new RelayCommand(fun1);
}

public void fun1()
{
MessageBox.Show(" this is test!");
}
}

现在,您可以将此viewmodel用于当前的UserControl。

<网格和GT; 
< Button
Name =" btn1"
Width =" 200"
高度="30"
Command =" {Binding command1}"
Content =" btn1" />
< / Grid>


 public partial class UserControl9:UserControl 
{
public UserControl9()
{
InitializeComponent();
this.DataContext = new viewmodel1();
}
}

最好的问候,


Cherry


I've created a ViewModel base that I've used in all of my other ViewModels and Models and it works great. It supports INotifyPropertyChanged, ICommand, etc...

However, I'm now creating some UserControls and I'm needing to create some ICommands, but I'd rather not have to recreate the ICommand implementation in each UserControl.

What's the best way to use my ViewModel base in these UserControls?

Thanks!

解决方案

Hi T Gregory,

According to your description, you want to implement ICommand for many UserControls, but you don't want to recreate the ICommand implementation for each UserControl, am I right?

If yes, I suggest you can create new class that implementing ICommand interface, like this:

public class Relaycommand: ICommand
    {
        readonly Action _execute;

        public Relaycommand(Action execute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
            _execute = execute;
        }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }
        public void Execute(object parameter)
        {
            _execute();
        }
    }

Then you create viewmodel for UserControl, reference RelayCommand class,  creating many RelayCommand property for this viewmodel, like this:

public class viewmodel1
    {
        public RelayCommand command1 { get; set; }

        public viewmodel1()
        {
            command1 = new RelayCommand(fun1);
        }

        public void fun1()
        {
            MessageBox.Show("this is test!");
        }
    }

Now, you can use this viewmodel for current UserControl.

<Grid>
        <Button
            Name="btn1"
            Width="200"
            Height="30"
            Command="{Binding command1}"
            Content="btn1" />
    </Grid>

public partial class UserControl9 : UserControl
    {
        public UserControl9()
        {
            InitializeComponent();
            this.DataContext = new viewmodel1();
        }
    }

Best Regards,

Cherry


这篇关于在UserControl中使用ViewModel库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆