有没有干净的方式,使CanExecute总是返回true具有属性? [英] Is there a clean way to make CanExecute always return true with an attribute?

查看:362
本文介绍了有没有干净的方式,使CanExecute总是返回true具有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我已经知道答案,我的问题,但我希望把它放在那里呢。我有一吨的命令处理程序的应用程序,每一个在其CanExecute方法特别的逻辑来适当地启用绑定按钮。

I think I already know the answer to my question, but I want to put it out there anyway. I have an app with a ton of command handlers, each with special logic in their CanExecute methods to enable the bound Buttons appropriately.

现在我在一个情况下,我不希望任何执行逻辑,因为执行结果在调用库,我不希望发生的只是界面更新的。我不能存根出库调用,因为它们是应用程序的其他部分的功能​​非常重要。

Now I'm in a situation where I don't want any of the logic to execute, because execution results in calls to a library that I don't want to occur just for GUI updates. I can't stub out the library calls because they are important for the functionality of the rest of the app.

我看着有条件属性在.NET和可悲的是,这是行不通的,因为他们只能在返回void的方法。我可以使用#if和#定义要么用我的逻辑或者只是返回true。我还可以查询在视图模型属性,并允许它决定是否只返回true。

I looked into the Conditional attributes in .NET, and sadly, this won't work because they only work on methods that return void. I could use #if and #define to either use my logic or just return true. I could also query a property in the viewmodel and allow this to determine whether or not to just return true.

现在的问题是,我不懒,但我也不想做了一堆繁琐的工作,使我猜是不可避免的修改。但是,如果有人知道的一种方式来使用的东西在.NET自动有我的按钮启用,而无需调用CanExecute或至少避免使用的基本逻辑,请张贴的答案! :)

The problem is, I'm not lazy but I also don't want to do a bunch of grunt work to make the modifications that I am guessing are unavoidable. However, if anyone knows of a way to use something in .NET to automatically have my buttons enabled without needing to call CanExecute or at least avoid using the underlying logic, please post the answer! :)

推荐答案

我问为什么这是不好的调用到这个库的GUI逻辑。你不希望启用按钮,不会做任何事情,所以被禁用的命令时,他们实际上并不能工作,可以是一个很好的事情。

I question why it is not good to call into this library for GUI logic. You don't want to enable buttons that won't do anything, so commands that are disabled when they can't actually work can be a good thing.

在使用MVVM模式,我的ViewModels通常直接暴露个ICommand。如果你使用棱镜,有一个DelegateCommand实现,可以很容易地在你的ViewModels执行这些命令。如果不是,或者如果CanExecute很可能会改变,我将实现的ICommand在单独类

When using the MVVM pattern, my ViewModels normally expose the ICommands directly. If you use Prism, there is a DelegateCommand implementation that makes it easy to implement these commands in your ViewModels. If not, or if the CanExecute is likely to change, I would implement the ICommand in a separate class.

如果你有一个长期运行的/昂贵的操作,你可以随时缓存库调用的结果。我假设你会得到一个事件或回调如果状态变化,这将启用/禁用命令。在这种情况下,在你的ICommand实现中,你将提高CanExecuteChanged事件,以便在GUI,以反映改变该命令。下面是使用后备服务,它知道当一个操作就可以完成一个命令的例子:

If you have a long running / expensive operation, you can always cache the result of the library call. I'm assuming you will get an event or callback if the state changes which would enable/disable the command. In that case, in your ICommand implementation, you would raise the CanExecuteChanged event in order for the GUI to reflect the changes to the command. Here is an example of a command that uses a backing service that is aware of when an operation can be completed:

public class ExpensiveCommand : ICommand
{
    private readonly IExpensiveService service;
    private bool canExecute;

    public ExpensiveCommand (IExpensiveService service)
    {
        this.service = service;
        canExecute = service.CanExecute();
        service.CanExecuteChanged += OnCanExecuteChanged;
    }

    public void Execute(object parameter)
    {
        service.Execute();
    }

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

    public event EventHandler CanExecuteChanged;
    private void OnCanExecuteChanged(object sender, EventArgs e)
    {
        canExecute = service.CanExecute();

        if (CanExecuteChanged != null)
            CanExecuteChanged(this, EventArgs.Empty);
    }
}

另外,如果你总是希望该命令是可执行的,你可以返回true从CanExecute方法。

Alternately, if you always want the command to be executable, you can just return true from the CanExecute method.

这篇关于有没有干净的方式,使CanExecute总是返回true具有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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