命令从viewmodel调用方法 [英] Command to call method from viewmodel

查看:262
本文介绍了命令从viewmodel调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我倾向于避免使用命令,因为他们总是设法混淆我的地狱,但我正在一个新的项目,我试图正确建设它,没有代码在我的观点。基本上所有我现在想做的现在是连接一个按钮,发射一个命令,做一些事情在我的视图模型,不知何故,这么简单的东西仍然给我麻烦。我想我很近,但不能到那里。这是我现在的情况。

OK, I tend to avoid using commands because they always manage to confuse the hell out of me, but I'm on a new project and am trying to architect it correctly with no code behind on my view. Basically All I am trying to do right now is wire up a button that fires a command that does some things on my view model, and somehow something so simple is still giving me trouble. I think I'm close but can't quite get there. Here is what I have right now.

<Window.Resources>
    <RoutedUICommand x:Key="GetMusic" />
</Window.Resources>
<Window.DataContext>
    <core:ViewMain />
</Window.DataContext>
<Window.CommandBindings>
    <CommandBinding Command="{StaticResource GetMusic}" Executed="GetMusicExecuted"/>
</Window.CommandBindings>

而且视图模型现在几乎不存在了。

And the view model is pretty much nothing right now

public class ViewMain
{
    public MusicCollection Music { get; set; }

    private void GetMusicExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        //Logic
    }
}

现在我想做的是连接这个命令我设置在我的命令绑定,只是调用我的执行的方法在我的视图模型,但是它试图在视图本身内找到该方法。有没有办法,我可以将它指向我的视图模型中的方法,或者更好的方式来设置完成同样的事情?希望能够保持简单,所以我不会过早的打击我的心。

Now what I'm trying to do is wire up this command I set up in my command bindings to just call my executed method in my view model, however it tries to find that method within view itself. Is there a way I can direct it to the method in my view model instead, or a better way to set this up to accomplish the same thing? Hoping to keep it simple at first so I don't blow my mind too early.

推荐答案

我倾向于使用我自己的命令类,实现ICommand。然后我将Button Command属性绑定到我的视图模型中的命令属性。当按钮被点击时,它执行Execute方法,而不管什么是绑定到Command属性。

I tend to use my own command class, implementing ICommand. Then I bind the Button Command property to the command property in my view model. When the button is clicked, it executes the Execute method whatever is bound to the Command property.

这是丑陋的两分钟版本,但它显示了如何使命令类,然后分配它委托指向你想要的视图模型上的任何方法。

This is the ugly two minute version, but it shows how you can make a Command class and then assign it delegates that point back to whatever method you like on your view model.

ViewModel:

The ViewModel:

public class MyViewModel
{
    public MyCommand ActionCommand
    {
        get;
        set;
    }

    public MyViewModel()
    {
        ActionCommand = new MyCommand();
        ActionCommand.CanExecuteFunc = obj => true;
        ActionCommand.ExecuteFunc = MyActionFunc;
    }

    public void MyActionFunc(object parameter)
    {
        // Do stuff here 
    }

}

public class MyCommand : ICommand 
{
    public Predicate<object> CanExecuteFunc
    {
        get;
        set;
    }

    public Action<object> ExecuteFunc
    {
        get;
        set;
    }

    public bool CanExecute(object parameter)
    {
        return CanExecuteFunc(parameter);
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        ExecuteFunc(parameter);
    }
}

View将绑定到它(假设DataContext设置为视图模型的实例):

The View would bind to it thusly (assuming DataContext is set to an instance of the view model):

<Window x:Class="exp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Command="{Binding Path=ActionCommand}">Action</Button>
    </Grid>
</Window>

这篇关于命令从viewmodel调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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