将 CommandBar 添加到 xaml UWP 中当前歌曲列表的顶部。多次设置“内容”属性

Posted

技术标签:

【中文标题】将 CommandBar 添加到 xaml UWP 中当前歌曲列表的顶部。多次设置“内容”属性【英文标题】:Adding CommandBar to top of current song list in xaml UWP. The property 'Content' is set more than once 【发布时间】:2021-03-23 22:24:06 【问题描述】:

我有两个部分我正在尝试将 xaml 代码合并在一起,它们都独立工作,但是当合并时我无法让它工作。什么是正确的方法。我已将 CommandBar xmal 放在

CommandBar XAML:显示在页面顶部 enter image description here

<CommandBar Margin="-10,46,10,0" IsOpen="True" Name="xCommand"  >
    <AppBarToggleButton x:Name="apbartg1" IsChecked="False" Icon="Mute" Label="Mute"  Click="AppBarMuteButton_Click" />
    <AppBarToggleButton Icon="RepeatAll" Label="Refresh" Click="AppBarRefreshButton_Click"/>
    <AppBarSeparator/>
    <AppBarButton Icon="Stop" Label="Stop" Click="AppBarStopButton_Click"/>
    <AppBarButton Icon="Play" Label="Play" Click="AppBarPlayButton_Click"/>

    <!--<CommandBar.SecondaryCommands>
        <AppBarButton Icon="Like" Label="Like" Click="AppBarButton_Click"/>
        <AppBarButton Icon="Dislike" Label="Dislike" Click="AppBarButton_Click"/>
    </CommandBar.SecondaryCommands>-->

    <CommandBar.Content>
        <TextBlock x:Name="contentNameText" Text="Now playing..." Margin="12,8"/>
    </CommandBar.Content>
</CommandBar>

歌曲列表 XAML: enter image description here

<Page
    x:Class="mInc.WUP.Audio.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:mInc.WUP.Audio"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:models="using:mInc.WUP.Audio.Models"
    mc:Ignorable="d">



    
    <Page.Resources>
       
        <x:String x:Key="ChevronGlyph">&#xE26B;</x:String>

        <CollectionViewSource
            x:Name="GroupedMusicListSource"
            Source="x:Bind MainModel.GroupedMusicList"
            IsSourceGrouped="True"
            ItemsPath="Items"
            />

        <CollectionViewSource
            x:Name="UngroupedMusicListSource"
            Source="x:Bind MainModel.UngroupedMusicList"
            IsSourceGrouped="False"
            />
    </Page.Resources>

将 CommandBar 代码放在这里并得到错误。我也尝试了其他地方,但无法找到解决方案。

    <GridView ItemsSource="Binding Source=StaticResource GroupedMusicListSource"
              SelectionMode="None"
              Name="GuestGridView"
              IsItemClickEnabled="True"
              ItemClick="GuestGridView_ItemClick"
              >

        <GridView.GroupStyle >
            <GroupStyle>
                <GroupStyle.HeaderTemplate >
                    <DataTemplate x:DataType="models:SongGroupModel" >
                        <Grid Margin="0,0,0,2" >
                            <Button
                                Foreground="ThemeResource ApplicationHeaderForegroundThemeBrush"
                                IsEnabled="x:Bind HasGroupDetails"
                                Click="Header_Click"
                                Style="StaticResource TextBlockButtonStyle" >
                                <RelativePanel >
                                    <TextBlock
                                        Name="TitleBlock"
                                        RelativePanel.AlignLeftWithPanel="True"
                                        RelativePanel.AlignVerticalCenterWithPanel="True"
                                        Text="x:Bind Title" Margin="0,0,10,0"
                                        Style="StaticResource TitleTextBlockStyle" />
                                    <TextBlock
                                        RelativePanel.RightOf="TitleBlock"
                                        RelativePanel.AlignVerticalCenterWithPanel="True"
                                        Text="StaticResource ChevronGlyph"
                                        FontFamily="Segoe MDL2 Assets"
                                        FontWeight="Normal"
                                        Style="StaticResource TitleTextBlockStyle" />
                                </RelativePanel>
                            </Button>
                        </Grid>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </GridView.GroupStyle>

        <GridView.ItemTemplate>
            <DataTemplate x:DataType="models:SongModel" >
                <StackPanel
                    Orientation="Horizontal"
                    HorizontalAlignment="Center"
                    Width="300"
                    BorderThickness="1" BorderBrush="DarkBlue"
                    Background="AliceBlue"
                    >
                    <StackPanel HorizontalAlignment="Center" Padding="5" >
                        <StackPanel Orientation="Horizontal" >
                            <TextBlock Text="Title:" FontWeight="Bold" />
                            <TextBlock Name="AlbumBlock" Margin="5,0,0,0"
                               Text="x:Bind Title" />
                        </StackPanel>

                        <StackPanel Orientation="Horizontal" >
                            <TextBlock Text="Album:" FontWeight="Bold" />
                            <TextBlock Name="ArtistBlock" Margin="5,0,0,0"
                               Text="x:Bind Album" />
                        </StackPanel>

                        <StackPanel Orientation="Horizontal" >
                            <TextBlock Text="Artist:" FontWeight="Bold" />
                            <TextBlock Name="TitleBlock" Margin="5,0,0,0"
                               Text="x:Bind Artist" />
                        </StackPanel>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </GridView.ItemTemplate>
        
    </GridView>
</Page>

【问题讨论】:

"Getting Content error" 错误信息在哪里?请张贴。 当我把 schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= 之后的第一个代码"schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:AviymInc.WUP.AudioFetch" xmlns:d="schemas.microsoft.com/expression/blend/2008" xmlns:mc="schemas.openxmlformats.org/markup-compatibility/2006" xmlns:models="using:AviymInc.WUP.AudioFetch.Models " mc:Ignorable="d"> 然后我的“属性'内容”设置了不止一次错误。 谢谢@John Wiese,请编辑您的原始帖子并将其添加到那里。 我尝试将它放在其他地方,但我得到了同样的错误或没有显示任何内容。我应该把命令栏放在页面顶部的哪里?代码 XLS0501 不用担心,没关系。审核问题的人也会检查 cmets。 【参考方案1】:

<!--<CommandBar.SecondaryCommands>
    <AppBarButton Icon="Like" Label="Like" Click="AppBarButton_Click"/>
    <AppBarButton Icon="Dislike" Label="Dislike" Click="AppBarButton_Click"/>
</CommandBar.SecondaryCommands>-->

<CommandBar.Content>
    <TextBlock x:Name="contentNameText" Text="Now playing..." Margin="12,8"/>
</CommandBar.Content>

【讨论】:

以上是关于将 CommandBar 添加到 xaml UWP 中当前歌曲列表的顶部。多次设置“内容”属性的主要内容,如果未能解决你的问题,请参考以下文章

UWP CommandBar SecondaryCommand 与 AppBarButton Flyout 冲突

MasterDetailsView中的CommandBar放置

如何将功能按钮添加到 UWP XAML 中的列表框项

CommandBar

UWP Xaml将属性绑定到splitview中的其他页面

如何在 Xamarin UWP 中更改 CommandBar Title 字体大小