简体   繁体   English

多个AppBar / CommandBar

[英]Multiple AppBar/CommandBar's

Back in Windows Phone 8, I was able to to use multiple AppBar, swapping them on certain pivot pages but In Windows Phone 8.1, I'm not sure how to do this or is this even possible. 回到Windows Phone 8,我能够使用多个AppBar,在某些数据透视页上交换它们,但在Windows Phone 8.1中,我不知道如何做到这一点,或者这是否可能。

Basically for my scenario, I've got 3 Pivot Pages. 基本上对于我的场景,我有3个Pivot页面。 Each page needs to have a different CommandBar because it needs to have different controls. 每个页面都需要具有不同的CommandBar,因为它需要具有不同的控件。

Is someone able to show me how I can do this? 有人能告诉我如何做到这一点吗?

Edit: Code Which I used for Windows Phone 8 to execute this: 编辑:代码我用于Windows Phone 8执行此操作:

XAML: XAML:

<phone:PhoneApplicationPage.Resources>
<shell:ApplicationBar x:Key="AppBar1" IsVisible="True" IsMenuEnabled="True">
    <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
    <shell:ApplicationBar.MenuItems>
        <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
    </shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>

<shell:ApplicationBar x:Key="AppBar2" IsVisible="True" IsMenuEnabled="True">
    <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1" />
    <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2" />
    <shell:ApplicationBar.MenuItems>
        <shell:ApplicationBarMenuItem Text="MenuItem 1" />
        <shell:ApplicationBarMenuItem Text="MenuItem 2" />
    </shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>

C#: C#:

private void MainPivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        switch (MainPivot.SelectedIndex)
        {
            case 0:
                ApplicationBar = this.Resources["AppBar1"] as ApplicationBar;
                break;
            case 1:
                ApplicationBar = this.Resources["AppBar2"] as ApplicationBar;
                break;
        }
    }

Basically switches the the AppBar when the PivotPage is changed. 更改数据透视表时,基本上会切换AppBar。

A simple solution is to use XAML to define your buttons and ViewModel (MVVM pattern) to control the visibilities of these buttons, and this can avoid creating buttons in code and complicated logic to control which button to display. 一个简单的解决方案是使用XAML定义按钮和ViewModel(MVVM模式)来控制这些按钮的可见性,这可以避免在代码中创建按钮和复杂的逻辑来控制显示哪个按钮。

First, define all the buttons might be used in CommandBar: 首先,定义CommandBar中可能使用的所有按钮:

<Page.BottomAppBar>
    <CommandBar>
        <!--buttons of group1-->
        <AppBarButton Icon="Icon1" Label="button1"/>
        ...
        <!--buttons of group2-->
        <AppBarButton Icon="Icon2" Label="button2"/>
        ...
        <!--buttons of group3-->
        <AppBarButton Icon="Icon3" Label="button3"/>
    </CommandBar>
</Page.BottomAppBar>

Then define a property in the ViewModel, such as: 然后在ViewModel中定义一个属性,例如:

public class PageViewModel : INotifyPropertyChanged
{
    ...
    public int CommandGroup
    {
        get { return _commandGroup; }
        set { _commandGroup = value; NotifyPropertyChanged("CommandGroup"); }
    }
}

This CommandGroup property is used to control the show/hide of buttons, for example, setting CommandGroup = 1 to show buttons in group1 and hide buttons in other groups, and setting CommandGroup = 2 to show buttons in group2 and hide buttons in other groups, which group1 and group2 here are just logical groups. CommandGroup属性用于控制按钮的显示/隐藏,例如,设置CommandGroup = 1以显示group1中的按钮和隐藏其他组中的按钮,设置CommandGroup = 2以显示group2中的按钮和隐藏其他组中的按钮,这里的group1和group2只是逻辑组。

Then define a converter to convert the value of CommandGroup property to Visibility: 然后定义一个转换器,将CommandGroup属性的值转换为Visibility:

public class CommandGroupToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language) 
    {
        return (System.Convert.ToInt32(value) == System.Convert.ToInt32(parameter)) ? Visibility.Visible : Visibility.Collapsed;
    }
}

And finally, bind this CommandGroup property to all the buttons in CommandBar (copy and paste things): 最后,将此CommandGroup属性绑定到CommandBar中的所有按钮(复制和粘贴内容):

<Page.Resources>
    <c:CommandGroupToVisibilityConverter x:Key="MyConverter"/>
</Page.Resources>
<Page.BottomAppBar>
    <CommandBar>
        <!--buttons of group1-->
        <AppBarButton 
            Icon="Icon1" Label="button1" 
            Visibility="{Binding CommandGroup, Converter={StaticResource MyConverter}, ConverterParameter=1}"/>

        <!--buttons of group2-->
        <AppBarButton 
            Icon="Icon2" Label="button2" 
            Visibility="{Binding CommandGroup, Converter={StaticResource MyConverter}, ConverterParameter=2}"/>

        <!--buttons of group3-->
        <AppBarButton 
            Icon="Icon3" Label="button3" 
            Visibility="{Binding CommandGroup, Converter={StaticResource MyConverter}, ConverterParameter=3}"/>
    </CommandBar>
</Page.BottomAppBar>

Note that when CommandGroup == 2, then all buttons with ConverterParameter=2 will display and others are gone. 请注意,当CommandGroup == 2时,将显示所有具有ConverterParameter = 2的按钮,其他按钮将消失。

This might be very useful in where there are several views in one page (like a Pivot) and each of them has its different group of command buttons. 在一个页面中有多个视图(如Pivot)并且每个视图都有不同的命令按钮组时,这可能非常有用。

In WP8.1 RT, you have a property BottomAppBar of your Page . 在WP8.1 RT中,您有一个页面属性BottomAppBar It works pretty much the same (apart it's extended) as old ApplicationBar - you can set it with CommandBar . 它与旧的ApplicationBar几乎相同(除了扩展) - 您可以使用CommandBar进行设置。 I've created my command bars in code and it works, you can try like this: 我已经在代码中创建了命令栏并且它可以工作,你可以尝试这样:

// prepare your CommandBars - run method somewhere in the constructor of the page:
CommandBar firstBar;
CommandBar secondBar;

private void PrepareAppBars()
{
    firstBar = new CommandBar();
    firstBar.IsOpen = true;
    AppBarButton FirstBtn = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/first.png") } };
    FirstBtn.Label = "First";
    FirstBtn.Click += FirstBtn_Click;
    FirstBtn.IsEnabled = true;
    // Similar for second button
    AppBarButton SecondBtn = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/second.png") } };

    firstBar.PrimaryCommands.Add(FirstBtn);
    firstBar.PrimaryCommands.Add(SecondBtn);

    // define also SecondaryCommands

    // simlar secondBar
    secondBar = new CommandBar();
    secondBar.IsOpen = true;
    // ...
}

// then you can surely switch them like this:

private void MainPivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    switch (MainPivot.SelectedIndex)
    {
        case 0:
            BottomAppBar = firstBar ;
            break;
        case 1:
            BottomAppBar = secondBar ;
            break;
    }
}

I ended with creating my base page class (actually I already had I for passing navigation parameter to ViewModel) that extends original Page. 我最后创建了我的基页类(实际上我已经将导航参数传递给ViewModel),它扩展了原始页面。 In Base Page I added Dependency Property AppBarCollection so that I could use it in my real pages in Xaml. 在Base Page中我添加了Dependency Property AppBarCollection,以便我可以在Xaml的真实页面中使用它。 And there I define all necessary AppBars without necessity of creating them in code behind. 在那里,我定义了所有必要的AppBars,而无需在后面的代码中创建它们。 The only thing I do there is choosing which one to show. 我在那里做的唯一一件事是选择展示哪一个。 Even this can be done from Xaml, but I didn't want to make things more complex. 即使这可以从Xaml完成,但我不想让事情变得更复杂。 The option is basically the first suggestion, with the difference that you can define all AppBars in you page in xaml. 该选项基本上是第一个建议,区别在于您可以在xaml中定义您页面中的所有AppBars。

<views:BindablePage.AppBarCollection>
    <views:AppBarCollection>
        <CommandBar/>
        <CommandBar/>
        <CommandBar/>
    </views:AppBarCollection>
</views:BindablePage.AppBarCollection>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM