从代码隐藏操作Windows Phone 8.1 XAML中的应用程序栏

本文关键字:XAML 应用程序 隐藏 代码 操作 Windows Phone | 更新日期: 2023-09-27 18:25:44

这可能是一个微不足道的初学者问题,我已经找到了很多关于Windows和Silverlight应用程序的相关信息,但没有任何直接帮助。我正在用C#和XAML编写一个WindowsPhone8.1/WinRT应用程序,我想以编程方式修改在XAML中创建的应用程序栏。例如,有一个按钮我只想包含在调试构建中,使用代码背后的预处理器指令。

在下面的XAML代码中,我将创建一个带有两个按钮的BottomAppBar。如何创建第二个按钮(AppBarAddSampleItemsButton),包括代码隐藏中的所有属性?

<prism:VisualStateAwarePage.BottomAppBar>
    <CommandBar >
        <CommandBar.PrimaryCommands>
            <AppBarButton x:Uid="AppBarNewItemButton"
                          Label="New item"
                          Icon="Add" Command="{Binding GoToAddItemPageCommand}" />
        </CommandBar.PrimaryCommands>
        <CommandBar.SecondaryCommands>
            <AppBarButton x:Uid="AppBarAddSampleItemsButton"
                          Label="Add sample items"
                          Command="{Binding GoToAddSampleItemssPageCommand}" />
        </CommandBar.SecondaryCommands>
    </CommandBar>
</prism:VisualStateAwarePage.BottomAppBar>

从代码隐藏操作Windows Phone 8.1 XAML中的应用程序栏

下面是一个示例代码,在代码后面创建一个AppBarButton,并将其添加到当前页面的BottomAppBar:

private void AddButtonToAppBar()
{
    AppBarButton buttonToAdd = new AppBarButton { Label = "Label", Icon = new SymbolIcon(Symbol.Help) };
    buttonToAdd.Click += async (sender, e) =>  await new MessageDialog("Button clicked").ShowAsync();
    // add button to Page's BottoAppBar
    (BottomAppBar as CommandBar).PrimaryCommands.Add(buttonToAdd);
}

编辑-对于绑定(再次从我的头顶,所以你必须检查一下),这可能会起作用:

Binding myBind = new Binding();
myBind.Path = new PropertyPath("GoToAddSampleItemssPageCommand");
myBind.Source = DataContext;
buttonToAdd.SetBinding(AppBarButton.CommandProperty, myBind);

MSDN上有关DataBinding的详细信息。

在Windows Phone开发中心进行了一些挖掘后,我发现了以下页面:如何为Windows Phone动态更改应用程序栏图标按钮和菜单项。希望它能有所帮助!