C# 多个AppBar/CommandBar';s
回到WindowsPhone8,我可以使用多个AppBar,在某些pivot页面上交换它们,但在WindowsPhone8.1中,我不确定如何做到这一点,或者这是否可能 基本上,对于我的场景,我有3个透视页面。每个页面都需要有不同的命令栏,因为它需要有不同的控件 有人能告诉我怎么做吗 编辑: 我用于Windows Phone 8执行此操作的代码: XAML:C# 多个AppBar/CommandBar';s,c#,xaml,windows-phone-8.1,C#,Xaml,Windows Phone 8.1,回到WindowsPhone8,我可以使用多个AppBar,在某些pivot页面上交换它们,但在WindowsPhone8.1中,我不确定如何做到这一点,或者这是否可能 基本上,对于我的场景,我有3个透视页面。每个页面都需要有不同的命令栏,因为它需要有不同的控件 有人能告诉我怎么做吗 编辑: 我用于Windows Phone 8执行此操作的代码: XAML: 当数据透视页更改时,基本上会切换AppBar中的属性。在WP8.1 RT中,您有一个属性。它的工作原理与旧的ApplicationBar几
当数据透视页更改时,基本上会切换AppBar中的属性。在WP8.1 RT中,您有一个属性。它的工作原理与旧的ApplicationBar几乎相同(只是扩展了),您可以使用。我已经用代码创建了我的命令栏,它可以工作,您可以这样尝试:
// 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;
}
}
一个简单的解决方案是使用XAML定义按钮和ViewModel(MVVM模式)来控制这些按钮的可视性,这可以避免在代码中创建按钮和复杂的逻辑来控制显示哪个按钮 首先,定义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>
此CommandGroup属性用于控制按钮的显示/隐藏,例如,将CommandGroup=1设置为在组1中显示按钮,并在其他组中隐藏按钮,将CommandGroup=2设置为在组2中显示按钮并在其他组中隐藏按钮,此处的组1和组2只是逻辑组
然后定义一个转换器,将CommandGroup属性的值转换为可见性:
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;
}
}
最后,将此CommandGroup属性绑定到CommandBar中的所有按钮(复制并粘贴内容):
请注意,当CommandGroup==2时,将显示ConverterParameter=2的所有按钮,其他按钮将消失
如果一个页面中有多个视图(如枢轴),并且每个视图都有其不同的命令按钮组,这可能非常有用。我最后创建了扩展原始页面的基本页面类(实际上我已经有了向ViewModel传递导航参数的I)。在基本页面中,我添加了依赖属性AppBarCollection,以便在Xaml中的实际页面中使用它。在那里,我定义了所有必要的AppBar,而无需在代码隐藏中创建它们。我唯一要做的就是选择要展示的。即使这可以从Xaml完成,但我不想让事情变得更复杂。 该选项基本上是第一个建议,不同的是您可以在xaml中定义页面中的所有AppBar
<views:BindablePage.AppBarCollection>
<views:AppBarCollection>
<CommandBar/>
<CommandBar/>
<CommandBar/>
</views:AppBarCollection>
</views:BindablePage.AppBarCollection>
是的。我更新了我的问题。你不应该切换整个应用程序栏,而是切换它的内容。你知道我在哪里可以找到这个例子吗?如果不是的话,你能给我提供更多关于如何做的细节吗。i、 e.切换内容。使用C#而不是XAML来创建应用程序栏,我相信您会发现这一点非常明显。我不知道,因为这与我制作应用程序栏时的做法不同。刚刚尝试并删除了代码..WP8.1 RT的问题是它不允许您初始化多个命令栏。这是WP8中以前的问题,可以创建多个appbar,然后选择要使用的appbar,但在WP8中没有。你觉得我该怎么办?你知道我能做什么吗?@Ahmed.C事实上,我在参考资料中定义commandbar时也发现了一些问题(虽然我在这方面没有花太多时间-我在代码中声明我的命令栏)。我已经编辑了我的答案,它在我的案例中是如何工作的。我想现在应该清楚该怎么做了。你又一次救了我。我对代码进行了修改,并使其正常工作。谢谢稍微修改了页面构造函数中的{Binding Path=CommandGroup,…}和“DataContext=pageViewModelObject”赋值,对我有效。:-)创意加1;)
public class PageViewModel : INotifyPropertyChanged
{
...
public int CommandGroup
{
get { return _commandGroup; }
set { _commandGroup = value; NotifyPropertyChanged("CommandGroup"); }
}
}
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;
}
}
<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>
<views:BindablePage.AppBarCollection>
<views:AppBarCollection>
<CommandBar/>
<CommandBar/>
<CommandBar/>
</views:AppBarCollection>
</views:BindablePage.AppBarCollection>