简体   繁体   English

使用 .NET MAUI Community Toolkit MVVM 时按钮变灰

[英]Button greyed when using .NET MAUI Community Toolkit MVVM

Once clicked, a button turns from blue to grey and will not return to the original blue color.单击后,按钮会从蓝色变为灰色,并且不会恢复到原来的蓝色。

I create a new .NET MAUI app and install CommunityToolkit.MVVM (V8.0.0).我创建了一个新的 .NET MAUI 应用程序并安装了 CommunityToolkit.MVVM (V8.0.0)。 Then create a MainPageViewModel and hook it up to the MainPage View.然后创建一个 MainPageViewModel 并将其连接到 MainPage 视图。

In the MainPageViewModel I have two [RelayCommand] methods.在 MainPageViewModel 中,我有两个 [RelayCommand] 方法。 One is async and the other is not.一个是异步的,另一个不是。

I add two buttons to the MainPage XAML and use the Command attribute to assign each of the 'RelayCommand' handlers to the buttons.我向 MainPage XAML 添加了两个按钮,并使用 Command 属性将每个“RelayCommand”处理程序分配给按钮。 The methods just dump to System.Diagnostics.Debug.这些方法只是转储到 System.Diagnostics.Debug。

When I execute the app on Windows 10 and click the non-async button, I see the diagnostic dump and the button background color remains blue.当我在 Windows 10 上执行应用程序并单击非异步按钮时,我看到诊断转储并且按钮背景颜色保持蓝色。 ie the original color.即原来的颜色。 ie this is the behavior I expect for both buttons.即这是我期望两个按钮的行为。

When I click on the async button, I see the diagnostic dump but the button background color changes from blue to grey.当我单击异步按钮时,我看到了诊断转储,但按钮背景颜色从蓝色变为灰色。 The button is still enabled.该按钮仍处于启用状态。 If I click again, I see the diagnostic dump again.如果再次单击,我将再次看到诊断转储。

How do I get the 'async' button back into the original state.如何将“异步”按钮恢复到原始状态。

Snippet from the MainPageViewModel:来自 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));
        }
    }


Snippet from the MainPage.xaml:来自 MainPage.xaml 的片段:

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

I am having the same problem.我有同样的问题。 I suspect this is due to an open Maui bug reported on Github: Button IsEnabled property in Windows Platform #7377我怀疑这是由于 Github 上报告的一个开放的毛伊岛错误: Button IsEnabled property in Windows Platform #7377

When I set up the Button's "IsEnabled" property, programmatically.当我以编程方式设置按钮的“IsEnabled”属性时。 The expected behavior is working in all platforms.预期行为适用于所有平台。 However, the formatting of the button in the Windows Platform gets lost after a couple of changes.但是,Windows 平台中按钮的格式经过几次更改后丢失了。

And

The IsEnabled = True works as intended if there was no await call between setting it false and true.如果在将其设置为 false 和 true 之间没有等待调用,则 IsEnabled = True 将按预期工作。 But if there is an await call before resetting it to true, the visual element does not reset.但是如果在将其重置为 true 之前有一个 await 调用,则视觉元素不会重置。

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

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