如何向 MasterDetailsView 添加命令栏?

How do I add a commandbar to the MasterDetailsView?

我正在尝试向 MasterDetailsView 添加命令栏,但我不知道如何直接使用 XAML 添加控件。

这是我的代码:

      <CommandBar Grid.Row="0" Name="CommandBar" >
        <AppBarToggleButton Icon="Shuffle" Label="Shuffle" />
        <AppBarToggleButton Icon="RepeatAll" Label="Repeat" />
        <AppBarSeparator/>
        <AppBarButton Icon="Back" Label="Back" />
        <AppBarButton Icon="Stop" Label="Stop" />
        <AppBarButton Icon="Play" Label="Play" />
        <AppBarButton Icon="Forward" Label="Forward" />

        <CommandBar.SecondaryCommands>
            <AppBarButton Icon="Like" Label="Like" />
            <AppBarButton Icon="Dislike" Label="Dislike" />
        </CommandBar.SecondaryCommands>

        <CommandBar.Content>
            <TextBlock Text="Now playing..." Margin="12,14"/>
        </CommandBar.Content>
    </CommandBar>

    <controls:MasterDetailsView MasterCommandBar="{x:Bind CommandBar}"
        Grid.Row="1"
        x:Name="MasterDetailsViewControl"

        ItemsSource="{x:Bind ViewModel.SampleItems, Mode=OneWay}"
        SelectedItem="{x:Bind ViewModel.Selected, Mode=OneWay}"
        ItemTemplate="{StaticResource ItemTemplate}"
        DetailsTemplate="{StaticResource DetailsTemplate}"
        NoSelectionContentTemplate="{StaticResource NoSelectionContentTemplate}"
        BorderBrush="Transparent" />

您可以这样设置 CommandBar:

<controls:MasterDetailsView MasterCommandBar="{x:Bind CommandBar}"
    Grid.Row="1"
    x:Name="MasterDetailsViewControl"

    ItemsSource="{x:Bind ViewModel.SampleItems, Mode=OneWay}"
    SelectedItem="{x:Bind ViewModel.Selected, Mode=OneWay}"
    ItemTemplate="{StaticResource ItemTemplate}"
    DetailsTemplate="{StaticResource DetailsTemplate}"
    NoSelectionContentTemplate="{StaticResource NoSelectionContentTemplate}"
    BorderBrush="Transparent">
    <controls:MasterDetailsView.MasterCommandBar>
        <CommandBar>
            <AppBarToggleButton Icon="Shuffle" Label="Shuffle" />
        </CommandBar>
    </controls:MasterDetailsView.MasterCommandBar>
</controls:MasterDetailsView>

您还应该能够将 CommandBar 放入您的页面资源中并使用该资源进行应用

<Page.Resources>
    <CommandBar x:Key="MasterCommandBar" >
        <AppBarToggleButton Icon="Shuffle" Label="Shuffle" />
        <AppBarToggleButton Icon="RepeatAll" Label="Repeat" />
        <AppBarSeparator/>
        <AppBarButton Icon="Back" Label="Back" />
        <AppBarButton Icon="Stop" Label="Stop" />
        <AppBarButton Icon="Play" Label="Play" />
        <AppBarButton Icon="Forward" Label="Forward" />

        <CommandBar.SecondaryCommands>
            <AppBarButton Icon="Like" Label="Like" />
            <AppBarButton Icon="Dislike" Label="Dislike" />
        </CommandBar.SecondaryCommands>

        <CommandBar.Content>
            <TextBlock Text="Now playing..." Margin="12,14"/>
        </CommandBar.Content>
    </CommandBar>
</Page.Resources>

<controls:MasterDetailsView MasterCommandBar="{StaticResource MasterCommandBar}"
    Grid.Row="1"
    x:Name="MasterDetailsViewControl"

    ItemsSource="{x:Bind ViewModel.SampleItems, Mode=OneWay}"
    SelectedItem="{x:Bind ViewModel.Selected, Mode=OneWay}"
    ItemTemplate="{StaticResource ItemTemplate}"
    DetailsTemplate="{StaticResource DetailsTemplate}"
    NoSelectionContentTemplate="{StaticResource NoSelectionContentTemplate}"
    BorderBrush="Transparent" />