Appbar 按钮颜色不会持续到 onclick 函数 returns

Appbar button color not persisting till onclick function returns

单击联系人图标时,它的背景颜色变为蓝色,但立即 returns 变回原来的颜色。我想将背景颜色保持为蓝色,直到 OnClick 函数 returns。请问有什么办法吗?

<Page.BottomAppBar>
        <CommandBar>
            <CommandBar.PrimaryCommands>
                <AppBarButton x:Name="ContactBtn" x:Uid="Contact_id" Icon="Contact" Label="View Contacts" Click="OnClick" />
            </CommandBar.PrimaryCommands> 
       </CommandBar>
</Page.BottomAppBar>

在 OnClick 方法开始时将 AppBarButton 的 属性 IsPressed 设置为 true,最后设置为 false;

编辑

    <interactivity:Interaction.Behaviors>
    <core:DataTriggerBehavior Binding="{Binding Path=IsPressed, RelativeSource={RelativeSource Self}" Value="True">
        <core:ChangePropertyAction PropertyName="Background" Value="Blue"/>
    </core:DataTriggerBehavior>
    <core:DataTriggerBehavior Binding="{Binding Path=IsPressed, RelativeSource={RelativeSource Self}" Value="False">
        <core:ChangePropertyAction PropertyName="Background" Value="Black"/>
    </core:DataTriggerBehavior>
</interactivity:Interaction.Behaviors>

当 IsPressed 改变时,您可以设置您想要的背景。

按钮的样式取决于它的 VisualState。我认为最简单的方法是在执行期间使用 AppBarToggleButton and set it's IsCheckedtrue
Click 会自动将其设置为 true,因此您只需将其重置为 false

但这仍然不像听起来那么容易。因为这只有在您在点击事件中执行一些异步操作或打开 Popup 或类似事件时才会起作用。

如果您只执行同步操作,按钮 UI 将不会更新。所以我认为它已经是异步的了。