CanExecute()返回true,按钮仍然被禁用 [英] CanExecute() returns true and button is still disabled

查看:1296
本文介绍了CanExecute()返回true,按钮仍然被禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个Windows Phone特定页面中有一个 BottomAppBar.AppBarButton ,它绑定了一个中继命令。代码,绑定和viewmodel实现在项目的其他页面上基本上以相同的方式使用,并按照预期工作。

I have a BottomAppBar.AppBarButton in a Windows Phone specific page, that is bound to a relay command. The code, binding and viewmodel implementation have all been used in basically the same way on other pages in the project and works exactly as expected there.

在这个特定场景中的问题即使在提高 .RaiseCanExecuteChanged()方法后, CanExecute()返回 true

The issue in this particular scenario is that the button remains disabled even after raising the .RaiseCanExecuteChanged() method, and the CanExecute() returns true.

我原以为这可能是由于额外的调用手动提高通知的属性更改,收紧我的代码的一部分,以便该方法只根据需要提出,以及何时需要更改按钮的状态。即使如此, CanExecute()返回 true ,按钮仍保持禁用状态。
如果我注释掉在 CanExecute()中的所有检查并且默认为true,按钮按预期启用,当点击启动时,预期的 Execute()函数,因此看起来 RelayCommand 的初始化是确定的。如果我然后让检查回来,并运行步骤每次 CanExecute()被触发,当它返回true,该按钮不会启用。

I originally thought that it might be due to excess calls to manually raising the notification with property changes, so have tightened that part of my code so that the method is only raised as needed, and when it is needed to change the button's status. Even still, the button remains disabled despite CanExecute() returning true. If I comment out all the checks in CanExecute() and default to true, the button is enabled as expected, and when tapped fires the expected Execute() function, so it appears that the initialization of the RelayCommand is ok. If I then let the checks back in, and run step through each time CanExecute() is fired, when it returns true, the button doesn't become enabled.

有什么想法吗?对于它的价值,我在下面添加了代码,但我不认为这是原因。

Any ideas? For what its worth, I've added code below, but I don't think that is the cause.

RelayCommand class是VS中的HubApp附带的标准类,因此我将忽略该代码。

RelayCommand class is the standard class that comes with the HubApp in VS, so I will omit that code.

viewmodel构造函数的最后一行是RelayCommand;

last line of the viewmodel constructor is the RelayCommand;

AddStrikeTeamCommand = new RelayCommand(async() => await AddStrikeTeam(), CanAddStrikeTeam);

可以添加:

private bool CanAddStrikeTeam()
{
    //if (NameWorking == string.Empty) return false;
    //if (FactionWorking == string.Empty) return false;
    //if (PointsLimitWorking < 1) return false;
    //if (!IsValidTeamWorking) return false;
    return true;
}

最后,按钮绑定

<AppBarButton x:Name="accept" Icon="Accept" Label="accept"
              Command="{Binding AddStrikeTeamCommand}"/>


推荐答案

我可能会打赌你的问题RaiseCanExecuteChanged()。这是特别真实的,如果你习惯了WPF和它如何自动刷新CanExe4cute为你。查看此委派命令实施:

I'd probably bet your problem has to do with RaiseCanExecuteChanged(). This is especially true if you are used to WPF and how it automatically refreshes CanExe4cute for you. Check out this Delegate Command implementation:


http://codepaste.net/ho9s5a

ICommand interface定义了指示按钮(或UI元素)刷新其启用状态的事件 CanExecuteChanged 在WPF中,这是由静态的,命令经理不断提出的。这在WinRT中不存在。在WPF中,因为它被频繁引发,WPF开发人员必须小心, CanExecute()不是一个昂贵的操作。 WinRT提供了昂贵的测试,但是因此需要开发人员手动提高事件。我希望这是有道理的。

The ICommand interface defines the event CanExecuteChanged which instructs the button (or UI Element) to refresh its Enabled status. In WPF, this was raised constantly by the static, command manager. This does not exist in WinRT. In WPF, because it was raised so frequently, WPF developers had to be careful that CanExecute() was not an expensive operation. WinRT provides for expensive tests, but consequently requires the developer to raise the event manually. I hope this makes sense.

我处理的一种方法是:

DelegateCommand _SaveCommand = null;
public DelegateCommand SaveCommand
{
    get
    {
        if (_SaveCommand != null)
            return _SaveCommand;
        _SaveCommand = new DelegateCommand
        (
            () =>
            {
                // TODO
            }, 
            () => true
        );
        this.PropertyChanged += (s, e) => _SaveCommand.RaiseCanExecuteChanged();
        return _SaveCommand;
    }
}

这基本上刷新CanExecute基于任何属性(通常在我的视图模型)。这是不够的,如果你有可能改变你在一个ObservableCollection中的模型,但它是一个很好的开始,整个事情。

This basically refreshes the CanExecute based on the change of any property in (usually in my View Model). This is not sufficient if you have potential changes in models that you have in an ObservableCollection, but it's a nice start to the whole thing.

有可能你根本没有这个问题。而且你要调用来提高事件,它返回true,并且仍然不工作。如果这是发生了什么,它只需要是你的代码,因为命令正在为成千上万的应用程序工作。但是,如果你想给我你的代码,我来看看。

There's a possibility that you don't have this problem at all. And that you are calling to raise the event, it is returning true, and is still not working. If that is what is happening, it just has to be your code because Commands are working for thousands of apps. But, if you want to send me your code, I'll take a look.

最好的运气!

这篇关于CanExecute()返回true,按钮仍然被禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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