命令执行后 WPF UI 样式部分冻结在错误状态

发布于 2024-11-10 15:20:19 字数 1456 浏览 2 评论 0 原文

另一天,另一个问题。老实说,如果这一切停止的话,那就太无聊了,不是吗?

编辑:似乎所有背景信息都已过时。这是精简版本: 我的 Command 类使用 CommandManagers RequerySuggested 事件来配置 CanExecuteChanged(如下所述:WPF 自定义 ICommand 实现和 CanExecuteChanged 事件)。

public abstract class CommandBase : ICommand
{
    public abstract void Execute(object parameter);
    public abstract bool CanExecute(object parameter);
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
}

就我而言,CommandManager 无法建议重新查询,而这可能很重要。现在我必须绕过这个并强制它重新查询。有人知道该怎么做吗?

另一个编辑:我现在已经尝试了很多事情(更改我的命令逻辑,删除控件模板和样式),但仍然陷入困境。不过,我无法在孤立的沙箱场景中重现该问题。我真的认为我在这里遇到了一个错误,因为按钮的行为与其外观不一致,即使样式归结为以下内容:

<Style TargetType="{x:Type Button}" x:Key="CertificateActionButton">
    <Setter Property="Background" Value="{DynamicResource CertificateActionButtonBackground}"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="LightGray"/>
        </Trigger>
    </Style.Triggers>
</Style>

当我取消焦点然后再次聚焦窗口时,一致性被恢复 - 按钮的外观不活跃,因为它是。

更新:由于我无法重现此问题,因此我关闭了此问题。 它收到的唯一答案是一个很好的答案,但在多次编辑该问题后,q & a 似乎不再相关了。

another day, another problem. Honestly, it would be rather boring if this ever stopped, wouldn't it?

EDIT : Seems all of the background-information is obsolete. Here's the boiled down version:
My Command class rigs up CanExecuteChanged with the CommandManagers RequerySuggested event (as described here : WPF Custom ICommand implementation and the CanExecuteChanged event).

public abstract class CommandBase : ICommand
{
    public abstract void Execute(object parameter);
    public abstract bool CanExecute(object parameter);
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
}

In my case, the CommandManager fails to suggest a requery where it would be of some importance. Now I have to bypass this and force it to requery. Does anybody know how to do this?

Another Edit: I've tried really many things now (changing my command logic, removing control template and style), and still get stuck on this. I can't reproduce the problem in an isolated sandbox scenario, though. I really think I am suffering from a bug here, since the Button's behaviour is inconsistent with its looks, even if the Style is boiled down to the following :

<Style TargetType="{x:Type Button}" x:Key="CertificateActionButton">
    <Setter Property="Background" Value="{DynamicResource CertificateActionButtonBackground}"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="LightGray"/>
        </Trigger>
    </Style.Triggers>
</Style>

When I unfocus and then focus the window again, Coherence is restored - the button the looks as inactive as it is.

Update : Since I haven't been able to reproduce this issue, i close this question.
The only answer it has received was a good one, but after having edited the question so many times, q & a don't really seem related any more.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢蛋碎的人ぎ生 2024-11-17 15:20:19

您可以尝试从 CommandManager.InvalidateRequerySuggested() rel="nofollow">这里

CommandManager 仅关注
在某些条件下确定
当命令目标改变时,
例如键盘焦点的变化。在
CommandManager 的情况
并不能充分确定
条件的变化导致
命令无法执行,
InvalidateRequerySuggested 可以
调用以强制 CommandManager
引发 RequerySuggested 事件。

还来自注释:

应该注意的是,如果您是
使用异步调用,然后
CommandManager.InvalidateRequerySuggested()
应该从主线程调用,
因为任何命令侦听器都会打开
用户界面线程。 (使用调度程序并调用 CheckAccess())

You could try calling CommandManager.InvalidateRequerySuggested() from here.

The CommandManager only pays attention
to certain conditions in determining
when the command target has changed,
such as change in keyboard focus. In
situations where the CommandManager
does not sufficiently determine a
change in conditions that cause a
command to not be able to execute,
InvalidateRequerySuggested can be
called to force the CommandManager to
raise the RequerySuggested event.

also from the notes:

It should be noted, that if you are
using async calls, then the
CommandManager.InvalidateRequerySuggested()
should be called from the main thread,
as any command listeners will be on
the UI thread. (Use a Dispatcher and call CheckAccess())

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文