1

我正在尝试将命令栏添加到 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" />
4

1 回答 1

1

您可以像这样设置 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" />
于 2017-11-07T15:23:56.057 回答