c# - 通过代码添加应用栏(Windows 8.1,C#)

标签 c# windows-runtime windows-store-apps windows-8.1 commandbar

在我的场景中,我想为我的所有 Windows 8.1 应用程序页面编写一个 BasePage。 在此BasePage 中应该创建一个TopAppBar

其实我有:

public CommandBar TopCommandBar
{
    get
    {
        // Check if a TopAppBar exists
        if (this.TopAppBar != null) return this.TopAppBar.Content as CommandBar;

        var appBar = new AppBar();
        this.TopAppBar = appBar;
        var top = this.TopAppBar.Content as CommandBar;
        if (top == null)
        {
            topCommandBar = new CommandBar();
            this.TopAppBar.Content = topCommandBar;
        }
        return this.TopAppBar.Content as CommandBar;
    }
}

这段代码运行得很好。但稍后在我的 BaseClass 中我想添加一个 AppBarButton

if (ShowCloseButton)
{
    var closeBtn = new AppBarButton();
    closeBtn.Icon = new SymbolIcon(Symbol.Clear);
    closeBtn.Label = "Close";
    closeBtn.Click += closeBtn_Click;
    this.TopCommandBar.PrimaryCommands.Insert(0, closeBtn);
}

奇怪的行为是,除非我单击鼠标右键两次,否则 closeBtn 不会显示在 TopAppBar 中。

意味着我第一次单击右键-->出现TopAppBar,但内部没有按钮。

然后我再次单击右侧按钮 --> TopAppBar 保持打开状态,并且该按钮显示其全部功能。

最佳答案

是的,我同意这看起来像一个错误。我在代码生成的 route 看到了同样的事情。经过调查,似乎 AppBar.IsOpen 在右键单击或滑动期间切换为 true,但 CommandBar.IsOpen 仍然为 false。这个修复对我有用:

BottomAppBar.Opened += (o, args) => { (this.BottomAppBar.Content as CommandBar).IsOpen = true; };

关于c# - 通过代码添加应用栏(Windows 8.1,C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19561300/

相关文章:

c# - 动态格式化数据表的列(值)

.net - 获取英语异常消息而不是本地语言

c# - 如何调试 WCF 程序

c# - 如何删除段落中的文字?

windows-8 - 给定的System.Uri无法转换为Windows.Foundation.Uri

c# - 在 Windows 8 商店应用程序中输入文本框时停止显示 Windows 8 键盘

c# - TextBlock 上的删除线效果

c# - 将 Windows 应用商店应用程序中的 Canvas 保存为图像文件

c# - .Net Rest Web 服务响应具有默认的数据协定命名空间而不是预期的命名空间

c# - 降低位图质量会产生 OutOfMemoryException