首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在BottomAppBars中添加不同的App.xaml

在BottomAppBars中添加不同的App.xaml
EN

Stack Overflow用户
提问于 2015-05-11 20:01:34
回答 2查看 299关注 0票数 2

如何在BottomAppBar中添加不同的App.xaml?我需要在相同或不同的页面中动态加载不同的BottomAppBar,所以我想将它们添加到App.xaml中。

例如,在xaml中有两个BottomAppBars:

PageBottomAppBar1

代码语言:javascript
复制
<Page.BottomAppBar>
    <CommandBar Name="PageBottomAppBar1">
        <CommandBar.PrimaryCommands>
            <AppBarButton Label="new" 
                          Icon="Page" 
                          Command="{Binding AddCommand, Mode=OneWay}"/>
            <AppBarButton Label="search" 
                          Icon="Find" />
        </CommandBar.PrimaryCommands>

        <CommandBar.SecondaryCommands>
            <AppBarButton Name="Logout" 
                          Label="Logout" 
                          Command="{Binding LogoutCommand, Mode=OneWay}" />
        </CommandBar.SecondaryCommands>
    </CommandBar>
</Page.BottomAppBar>

PageBottomAppBar2

代码语言:javascript
复制
<Page.BottomAppBar>
    <CommandBar Name="PageBottomAppBar2">
        <CommandBar.PrimaryCommands>
            <AppBarButton Label="sync" 
                          Icon="Sync" 
                          Command="{Binding SyncCommand, Mode=OneWay}"/>
            <AppBarButton Label="search" 
                          Icon="Find" />
        </CommandBar.PrimaryCommands>

        <CommandBar.SecondaryCommands>
            <AppBarButton Name="About" 
                          Label="About" 
                          Command="{Binding AboutCommand, Mode=OneWay}" />
        </CommandBar.SecondaryCommands>
    </CommandBar>
</Page.BottomAppBar>

如何将它们添加到App.xaml中以便将它们作为资源使用?

下面是App.xaml:

代码语言:javascript
复制
<Application x:Class="Test.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Test">
<Application.Resources>
  <vm:ViewModelLocator x:Key="Locator" xmlns:vm="using:Test.ViewModel" />

</Application.Resources>
</Application>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-12 14:22:02

只是一个想法,但是如何定义从IObservableVector继承的类:

代码语言:javascript
复制
public class CommandBarContent : IObservableVector<ICommandBarElement> {}

然后在XAML

代码语言:javascript
复制
<cb:CommandBarContent x:Key="FirstPrimaryBar">
    <AppBarButton Label="new" 
                      Icon="Page" 
                      Command="{Binding AddCommand, Mode=OneWay}"/>
    <AppBarButton Label="search" 
                      Icon="Find" />
</cb:CommandBarContent>

添加一个选择器/转换器(以http://tech.pro/tutorial/807/wpf-tutorial-how-to-use-a-datatemplateselector为例)

代码语言:javascript
复制
<cb:MyCommandSelector x:Key="CommandSelector" Case1="{StaticResource FirstPrimaryBar}" Case2="{StaticResource SecondPrimaryBar}" />

然后绑定命令栏:

代码语言:javascript
复制
<CommandBar Name="PageBottomAppBar1" PrimaryCommands="{Binding ScenarioParameter,Converter={StaticResource CommandSelector}}">

我还没有试过,但取决于可能的方案的数量,这可能是我将使用的解决方案。

另一个选项是绑定按钮的可见性属性,对于delete按钮,我已经这样做了,但是该按钮在更改场景时具有“跳转”行为,这有点奇怪。

票数 1
EN

Stack Overflow用户

发布于 2015-05-11 22:11:31

您可以创建一个从AppBar继承的类

代码语言:javascript
复制
public class MyAppBar : AppBar
{
    public MyAppBar() 
    { 
    }

    //...
}

然后你就可以像这样使用它:

代码语言:javascript
复制
<Page.BottomAppBar>
    <local:MyAppBar />
</Page.BottomAppBar>

不幸的是,在这种情况下,XAML将不可用。

为了能够使用XAML,您可以创建一个UserControl

MyCustomAppBar.xaml

代码语言:javascript
复制
<ctrls:AppBar
    x:Class="App1.MyCustomAppBar"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ctrls="using:Windows.UI.Xaml.Controls"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <Grid>

    </Grid>
</ctrls:AppBar>

MyCustomAppBar.xaml.cs

代码语言:javascript
复制
using Windows.UI.Xaml.Controls;

namespace App1
{
    public sealed partial class MyCustomAppBar : AppBar
    {
        public MyCustomAppBar()
        {
            this.InitializeComponent();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30176622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档