【问题标题】:Button greyed when using .NET MAUI Community Toolkit MVVM使用 .NET MAUI Community Toolkit MVVM 时按钮变灰
【发布时间】:2022-12-11 08:01:08
【问题描述】:

单击后,按钮会从蓝色变为灰色,并且不会恢复到原来的蓝色。

我创建了一个新的 .NET MAUI 应用程序并安装了 CommunityToolkit.MVVM (V8.0.0)。然后创建一个 MainPageViewModel 并将其连接到 MainPage 视图。

在 MainPageViewModel 中,我有两个 [RelayCommand] 方法。一个是异步的,另一个不是。

我向 MainPage XAML 添加了两个按钮,并使用 Command 属性将每个“RelayCommand”处理程序分配给按钮。这些方法只是转储到 System.Diagnostics.Debug。

当我在 Windows 10 上执行应用程序并单击非异步按钮时,我看到诊断转储并且按钮背景颜色保持蓝色。即原始颜色。即这是我对两个按钮的预期行为。

当我单击异步按钮时,我看到了诊断转储,但按钮背景颜色从蓝色变为灰色。该按钮仍处于启用状态。如果再次单击,我将再次看到诊断转储。

如何将“异步”按钮恢复到原始状态。

来自 MainPageViewModel 的片段:

    public partial class MainPageViewModel : ObservableObject
    {
        [RelayCommand]
        async Task TestSomethingAsync()
        {
            await Task.Delay(1);
            System.Diagnostics.Debug.WriteLine(nameof(TestSomethingAsync));
        }

        [RelayCommand]
        void TestMore()
        {
            System.Diagnostics.Debug.WriteLine(nameof(TestMore));
        }
    }


来自 MainPage.xaml 的片段:

            <Button
                Text="T1 Async"
                Command="{Binding TestSomethingCommand}"
                HorizontalOptions="Center" />
            
            <Button
                Text="T2"
                Command="{Binding TestMoreCommand}"
                HorizontalOptions="Center" />

【问题讨论】:

  • 我测试了您提供的代码,但无法重现您描述的问题 (a button turns from blue to grey and will not return to the original blue color)。我在 Windows 11 上执行了该应用程序。

标签: c# .net-maui


【解决方案1】:

我有同样的问题。我单击命令的按钮,它变灰了,但在命令执行完毕后它再也不会恢复启用状态。我正在使用 Windows 11。 您找到解决方案了吗?

【讨论】:

    猜你喜欢
    • 2022-08-04
    • 2023-01-16
    • 2021-12-28
    • 2023-01-20
    • 2012-02-03
    • 2022-11-18
    • 2011-06-19
    • 2023-02-06
    • 2016-07-18
    相关资源
    最近更新 更多