【问题标题】:UWP XAML Custom Command Bar Toggle ButtonUWP XAML 自定义命令栏切换按钮
【发布时间】:2020-05-02 15:45:03
【问题描述】:

我正在尝试为 CommandBar 创建一个自定义 AppBarToggleButton,它将仅使用 XAML ThemeResource 作为最喜欢/不喜欢按钮作出反应。我希望它将图标从OutlineStart 更改为SolidStar,并将按钮标签从Favorite 更改为Unfavorite,如下面的屏幕截图所示。 Exact result that I want to achieve is here.

这是其背后的代码:Github Gist

我的问题是,虽然此代码在 XAML Studio 应用程序中运行良好,但它会在 Visual Studio 2017 社区 中抛出 Uncaught exception。它在 Visual Studio 设计器中也能正常工作。

我知道我可以通过类似的代码实现同样的目的

private void ToggleFavorite_Checked(object sender, RoutedEventArgs e) {
    ToggleFavorite.Text = "Unfavorite";
    ToggleFavorite.Icon = new SymbolIcon(Symbol.SolidStar);
}
private void ToggleFavorite_Unchecked(object sender, RoutedEventArgs e) {
    ToggleFavorite.Text = "Favorite";
    ToggleFavorite.Icon = new SymbolIcon(Symbol.OutlineStar);
}

但这不是我想要的,我对 XAML 自定义很感兴趣,我想完全按照 XAML 风格来做。

我该怎么做才能让它在 Visual Studio 2017 中工作?

EDIT (05.03.2020):修改我的代码后,我注意到 Visual Studio 将此行突出显示为错误

<Setter Target="Content.Foreground" Value="{ThemeResource AppBarToggleButtonForegroundPointerOver}"/>

发生这种情况是因为我在这里将 ContentPresenter 的名称从 Content 更改为 ViewboxContentPresenter

<Viewbox x:Name="ContentViewbox" AutomationProperties.AccessibilityView="Raw" HorizontalAlignment="Stretch" Height="{ThemeResource AppBarButtonContentHeight}" Margin="{ThemeResource AppBarButtonContentViewboxCollapsedMargin}">
    <ContentPresenter x:Name="ViewboxContentPresenter" Foreground="{TemplateBinding Foreground}">
        <ContentPresenter.Content>
            <SymbolIcon Symbol="OutlineStar" />
        </ContentPresenter.Content>
    </ContentPresenter>
</Viewbox>

所以,这就是Unhandled Exception 被抛出的原因。重命名所有这些设置器的目标后,一切都开始按预期工作。

【问题讨论】:

    标签: c# xaml uwp customization resourcedictionary


    【解决方案1】:

    您可以使用VisualStateManager 来实现您的要求。

    使用StateTrigger 绑定AppBarToggleButtonIsChecked 属性。 选中按钮后使用Setter设置属性。

    <Grid>
            <CommandBar VerticalAlignment="Bottom">
                <AppBarToggleButton x:Name="toggleBtn" Icon="UnFavorite" Label="UnFavorite"/>
            </CommandBar>
    
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup>
                    <VisualState x:Name="checkedbtn">
                        <VisualState.StateTriggers>
                            <StateTrigger IsActive="{Binding ElementName=toggleBtn, Path=IsChecked}"/>
                        </VisualState.StateTriggers>
                        <VisualState.Setters>
                            <Setter Target="toggleBtn.Icon" Value="Favorite"/>
                            <Setter Target="toggleBtn.Label" Value="Favorite"/>
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        </Grid>
    

    【讨论】:

    • 我也可以使用这个更改按钮颜色吗?
    • 对于其他Buttons,你可以使用这种方式,但是对于AppBarToggleButton,我们不能更改它为Background,它不起作用。即使你这样设置。 。但是您可以创建自定义AppBarToggleButton.xaml,在选中和取消选中时更改其颜色并将样式应用于此按钮。
    • 这正是我所做的,我覆盖了 AppBarRevealToggleButton 样式,然后对其进行了更改,但是在我按下此按钮后,应用程序崩溃并出现 Unhandled Exception 错误。但是我的所有更改在 XAML Studio 中都可以正常工作。我只是不明白为什么它们不能在 Visual Studio 中工作。
    • 你把FavoriteButtonStyle.xaml加到App.xaml了吗,··
    猜你喜欢
    • 1970-01-01
    • 2018-03-07
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 2018-08-05
    • 2011-03-31
    相关资源
    最近更新 更多