首页 > 解决方案 > 如何使用 XAML 使用字符串资源设置 AppBarButton 文本

问题描述

有没有办法将字符串资源设置为 AppBarButton 的文本,而不必以编程方式进行?下面使用的方法由于某种原因不起作用。

<Page>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <CommandBar>
            <CommandBar.Content>
                <Button 
                    Style="{StaticResource NavigationBackButtonNormalStyle}" 
                    Name="BackButton" 
                    VerticalAlignment="Top" 
                    Click="Back_Click"/>
            </CommandBar.Content>

            <AppBarButton Name="HelloWorld" x:Uid="HelloWorld">
                <AppBarButton.Icon>
                    <FontIcon Glyph="&#xE110;"/>
                </AppBarButton.Icon>
            </AppBarButton>
        </CommandBar>
        <Frame Name="MyFrame"/>
    </Grid>
</Page>

标签: windowsxamluwpuwp-xaml

解决方案


您的代码不起作用的原因可能与Resources.resw文件有关。首先正如Richard Zhang - MSFT 所说,你需要创建一个Strings文件夹,并创建相应的localize 文件夹。 在此处输入图像描述

Resources.resw中,新建一个名为 的键/值HelloWorld.Label,将值设置为Hello World!

在此处输入图像描述

请注意,我们正在设置AppBarButton's Label。如果您将密钥设置为“HelloWorld”,则不会发生任何事情。 在此处输入图像描述


推荐阅读