简体   繁体   English

单击绑定到功能的UWP MVVM

[英]UWP MVVM on click bind to function

This is probably the easiest thing in the world to do but I'm struggling a bit. 这可能是世界上最简单的事情,但是我有点挣扎。

I have this in XAML: 我在XAML中有这个:

<Button Name="browseButton" Content="Browse" Grid.Column="1" />

I've got everything binding correctly from the view to the viewmodel, like radio buttons and input text boxes etc... but what I want is to bind this button to a function, such that when the user clicks it some operation occurs. 我已经从视图到视图模型正确地绑定了所有内容,例如单选按钮和输入文本框等。但是我想要的是将此按钮绑定到一个函数,以便当用户单击它时发生一些操作。

But I'm really having a hard time figuring out how to bind clicking this button to a function in the viewmodel. 但是我真的很难弄清楚如何将单击此按钮绑定到视图模型中的函数。 I've played a bit with ICommand and didn't get very far, and I don't want to do the hack-ish thing of just sticking it in the code behind. 我在ICommand上玩了一点,但距离还不够远,我也不想做一些将其粘贴在后面的代码中的事。

I'm using MVVMLight (Galasoft) if that helps. 如果有帮助,我正在使用MVVMLight(Galasoft)。

Any guidance appreciated. 任何指导表示赞赏。

Edit 编辑

Following the example from https://msdn.microsoft.com/en-us/magazine/dn237302.aspx I have, but where does canExecuteMyCommand come into it? 遵循来自https://msdn.microsoft.com/zh-cn/magazine/dn237302.aspx的示例,但是canExecuteMyCommand可以输入哪里呢? And how do I bind it in the XAML? 以及如何在XAML中绑定它?

    public RelayCommand BrowseCommand
    {
        get;
        private set;
    }

    public LoadFilesViewModel()
    {
        BrowseCommand = new RelayCommand(executeBrowse, () => _canExecuteMyCommand);
    }

    private void executeBrowse()
    {
        // Do something 
    }

Solution

<Button Name="browseButton" Content="Browse" Grid.Column="1" Command="{Binding BrowseCommand}" />

And

    public RelayCommand BrowseCommand
    {
        get;
        private set;
    }

    public LoadFilesViewModel()
    {
        BrowseCommand = new RelayCommand(executeBrowse, () => true);
    }

    private void executeBrowse()
    {
        // Do something 
    }

If you look at the code you provided, the RelayCommand constructor comes with 2 parameters. 如果您查看提供的代码,那么RelayCommand构造函数带有2个参数。

public RelayCommand BrowseCommand
{
    get;
    private set;
}

public LoadFilesViewModel()
{
    BrowseCommand = new RelayCommand(executeBrowse, () => _canExecuteMyCommand);
}

private void executeBrowse()
{
    // Do something 
}

Checking the source code (that's the learning a code base and open-source makes this possible) or Visual Studio IntelliSense, you'll see this signature: 检查源代码 (通过学习代码库和开放源代码,可以实现此目的)或Visual Studio IntelliSense,您将看到以下签名:

public RelayCommand(Action execute, Func<bool> canExecute)

So the first parameter is an action to be executed, and the second parameter is a check if it can execute. 因此,第一个参数是要执行的动作,第二个参数是检查它是否可以执行。 You've correctly identified the executeBrowse as a method to "do something". 您已经正确地将executeBrowse标识为“执行某些操作”的方法。 The _canExecuteMyCommand parameter is a class variable of the type bool that can be either true or false (set somewhere else). _canExecuteMyCommand参数是bool类型的类变量,可以为true或false(在其他位置设置)。

In your own solution (posted in question), you replaced this by true (hardcoded). 在您自己的解决方案(有问题的发布)中,您将其替换为true(硬编码)。 Note that you can also drop the second parameter in this case: 请注意,在这种情况下,您也可以删除第二个参数:

public LoadFilesViewModel()
{
    BrowseCommand = new RelayCommand(executeBrowse); // will always execute
}

Bonus 奖金

Note that instead of using a local variable, you can also use a method to defined whether the method can execute (or write the check logic inline with the delegate syntax). 请注意,除了使用局部变量之外,您还可以使用一种方法来定义该方法是否可以执行(或使用委托语法内联编写检查逻辑)。

public LoadFilesViewModel()
{
    BrowseCommand = new RelayCommand(ExecuteBrowse, CanExecuteBrowse);
}

private void ExecuteBrowse()
{
    // Do something 
}

private bool CanExecuteBrowse()
{
    // Check if we are allowed to browser
    return true; // or false :)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM