如何在 windows phone 8.1 运行时应用程序中的 xaml 页面本身添加多个 AppBar/CommandBar?
How to add Multiple AppBar/CommandBar's in xaml page itself in windows phone 8.1 runtime app?
我已经在我的应用程序 (Prism) 中实现了 mvvm,所以每个逻辑都会出现在视图模型中,所以我不想从后面的代码创建 appbar/commandbar,在 xaml 页面中它不支持更多比一个 appbar/commandbar,我浏览了很多博客,但找不到满足我需要的任何解决方案,但我得到的建议是通过在基页中使用依赖项 属性 可以完成,但我不知道如何实施它,所以任何人都可以帮助我解决这个问题。提前致谢:)
我的代码低于我在代码隐藏中所做的。(第一个应用栏在 xaml 中,第二个在代码隐藏中)
Xaml:
<storeApps:VisualStateAwarePage.BottomAppBar>
<CommandBar x:Name="firstBar">
<CommandBar.PrimaryCommands>
<AppBarButton Label="Proceed">
<AppBarButton.Icon>
<BitmapIcon UriSource="/Assets/next.png"/>
</AppBarButton.Icon>
</AppBarButton>
</CommandBar.PrimaryCommands>
</CommandBar>
</storeApps:VisualStateAwarePage.BottomAppBar>
CS 代码:
CommandBar secondBar;
public HomePage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
PrepareAppBars();
}
private void PrepareAppBars()
{
secondBar= new CommandBar();
secondBar.IsOpen = true;
AppBarButton btnHome = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/home.png") } };
btnHome.Label = "Home";
btnHome.IsEnabled = true;
AppBarButton btnWallet = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/wallet.png") } };
btnWallet.Label = "Wallet";
btnWallet.IsEnabled = true;
AppBarButton btnAccount = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/account.png") } };
btnAccount.Label = "Account";
btnAccount.IsEnabled = true;
AppBarButton btnUpdate = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/update.png") } };
btnUpdate.Label = "Update";
btnUpdate.IsEnabled = true;
secondBar.PrimaryCommands.Add(btnHome);
secondBar.PrimaryCommands.Add(btnWallet);
secondBar.PrimaryCommands.Add(btnAccount);
secondBar.PrimaryCommands.Add(btnUpdate);
BottomAppBar = secondBar;
}
private void HomePivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (HomePivot.SelectedIndex)
{
case 0:
BottomAppBar = secondBar;
break;
case 1:
BottomAppBar = firstBar;
break;
default:
BottomAppBar = null;
break;
}
}
你可以添加这样的东西来使用底部应用栏,你最多可以有 4 个带图标的按钮
<Page.BottomAppBar>
<CommandBar x:Name="btmAppbar">
<AppBarButton Icon="Refresh"
Label="Refresh"
x:Name="RefreshButton"/>
<AppBarButton Icon="Help"
x:Name="HelpButton"
Label="Help"/>
</CommandBar>
</Page.BottomAppBar>
要添加更多按钮,您必须使用辅助命令
<Page.BottomAppBar>
<CommandBar x:Name="btmAppbar">
<AppBarButton Icon="Refresh"
Label="Refresh"
x:Name="RefreshButton"/>
<AppBarButton Icon="Help"
x:Name="HelpButton"
Label="Help"/>
<CommandBar.SecondaryCommands>
<AppBarButton Label="FirstSecondary"/>
</CommandBar.SecondaryCommands>
</CommandBar>
</Page.BottomAppBar>
希望这对您有所帮助。
我有同样的要求,我打算尝试这个解决方案:您可以使用 XAML 定义按钮和 ViewModel(MVVM 模式)来控制这些按钮的可见性。将按钮分成不同的组。在视图模型中定义一个 属性,这个 属性 用于显示不同的按钮组,因此您需要在 XAML 中绑定那个 属性。
检查这个 Multiple AppBar/CommandBars
我已经在我的应用程序 (Prism) 中实现了 mvvm,所以每个逻辑都会出现在视图模型中,所以我不想从后面的代码创建 appbar/commandbar,在 xaml 页面中它不支持更多比一个 appbar/commandbar,我浏览了很多博客,但找不到满足我需要的任何解决方案,但我得到的建议是通过在基页中使用依赖项 属性 可以完成,但我不知道如何实施它,所以任何人都可以帮助我解决这个问题。提前致谢:)
我的代码低于我在代码隐藏中所做的。(第一个应用栏在 xaml 中,第二个在代码隐藏中)
Xaml:
<storeApps:VisualStateAwarePage.BottomAppBar>
<CommandBar x:Name="firstBar">
<CommandBar.PrimaryCommands>
<AppBarButton Label="Proceed">
<AppBarButton.Icon>
<BitmapIcon UriSource="/Assets/next.png"/>
</AppBarButton.Icon>
</AppBarButton>
</CommandBar.PrimaryCommands>
</CommandBar>
</storeApps:VisualStateAwarePage.BottomAppBar>
CS 代码:
CommandBar secondBar;
public HomePage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
PrepareAppBars();
}
private void PrepareAppBars()
{
secondBar= new CommandBar();
secondBar.IsOpen = true;
AppBarButton btnHome = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/home.png") } };
btnHome.Label = "Home";
btnHome.IsEnabled = true;
AppBarButton btnWallet = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/wallet.png") } };
btnWallet.Label = "Wallet";
btnWallet.IsEnabled = true;
AppBarButton btnAccount = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/account.png") } };
btnAccount.Label = "Account";
btnAccount.IsEnabled = true;
AppBarButton btnUpdate = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/update.png") } };
btnUpdate.Label = "Update";
btnUpdate.IsEnabled = true;
secondBar.PrimaryCommands.Add(btnHome);
secondBar.PrimaryCommands.Add(btnWallet);
secondBar.PrimaryCommands.Add(btnAccount);
secondBar.PrimaryCommands.Add(btnUpdate);
BottomAppBar = secondBar;
}
private void HomePivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (HomePivot.SelectedIndex)
{
case 0:
BottomAppBar = secondBar;
break;
case 1:
BottomAppBar = firstBar;
break;
default:
BottomAppBar = null;
break;
}
}
你可以添加这样的东西来使用底部应用栏,你最多可以有 4 个带图标的按钮
<Page.BottomAppBar>
<CommandBar x:Name="btmAppbar">
<AppBarButton Icon="Refresh"
Label="Refresh"
x:Name="RefreshButton"/>
<AppBarButton Icon="Help"
x:Name="HelpButton"
Label="Help"/>
</CommandBar>
</Page.BottomAppBar>
要添加更多按钮,您必须使用辅助命令
<Page.BottomAppBar>
<CommandBar x:Name="btmAppbar">
<AppBarButton Icon="Refresh"
Label="Refresh"
x:Name="RefreshButton"/>
<AppBarButton Icon="Help"
x:Name="HelpButton"
Label="Help"/>
<CommandBar.SecondaryCommands>
<AppBarButton Label="FirstSecondary"/>
</CommandBar.SecondaryCommands>
</CommandBar>
</Page.BottomAppBar>
希望这对您有所帮助。
我有同样的要求,我打算尝试这个解决方案:您可以使用 XAML 定义按钮和 ViewModel(MVVM 模式)来控制这些按钮的可见性。将按钮分成不同的组。在视图模型中定义一个 属性,这个 属性 用于显示不同的按钮组,因此您需要在 XAML 中绑定那个 属性。 检查这个 Multiple AppBar/CommandBars