c# - 如何使 CommandBar 打开到屏幕底部?

标签 c# xaml visual-studio-2015 windows-10 uwp

我有一个可能是愚蠢的疑问,但我真的不知道如何解决这个问题。

我有一个复杂的用户界面,里面有很多项目(一个 SplitView 里面有其他东西,然后是一个包含页面的框架,我有一个网格,最后是我的 CommandBar)。

当我点击“...”按钮时,CommandBar 向窗口顶部打开,它实际上还覆盖了其父 Page 所在的 Frame 的部分 UI 外部位于(我不知道这怎么可能)。

我尝试将 CommandBar、父网格、页面等的 VerticalAlignment 属性设置为 Top,但 CommandBar 仍然向屏幕的顶部打开。

As you can see, the CommandBar opens towards the top of the screen and covers other UI elements

有没有办法让它向底部打开,就像内置天气或照片应用程序中的 CommandBar 一样?我在这里缺少设置/属性吗?

感谢您的帮助!

塞尔吉奥

编辑:为了清楚起见,我页面的基本结构是这样的:

RootContent > ... > Frame > Page > Grid > CommandBar

不能将 CommandBar 放在 Page.TopAppBar 中,就好像我这样做一样,CommandBar 被放置在我的 Page 的 外面 并覆盖了我的 UI 的顶部。我需要将 CommandBar 放置在页面内部

最佳答案

CommandBar 依靠 VisualStates 来控制这部分尺寸和动画,这使得使用自定义视觉状态管理器拦截状态更改调用并将所有 OpenUp 调用替换为 OpenDown 调用变得容易。

public class OpenDownCommandBarVisualStateManager : VisualStateManager
{
    protected override bool GoToStateCore(Control control, FrameworkElement templateRoot, string stateName, VisualStateGroup group, VisualState state, bool useTransitions)
    {
        //replace OpenUp state change with OpenDown one and continue as normal
        if(!string.IsNullOrWhiteSpace(stateName) && stateName.EndsWith("OpenUp"))
        {
            stateName = stateName.Substring(0, stateName.Length - 6) + "OpenDown";
        }
        return base.GoToStateCore(control, templateRoot, stateName, group, state, useTransitions);
    }
}

public class OpenDownCommandBar : CommandBar
{
    public OpenDownCommandBar()
    {
    }

    protected override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        var layoutRoot = GetTemplateChild("LayoutRoot") as Grid;
        if(layoutRoot != null)
        {
            VisualStateManager.SetCustomVisualStateManager(layoutRoot, new OpenDownCommandBarVisualStateManager());
        }
    }
}

然后只需使用新的 OpenDownCommandBar 而不是普通的。

<myControls:OpenDownCommandBar>...</myControls:OpenDownCommandBar>

关于c# - 如何使 CommandBar 打开到屏幕底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33290361/

相关文章:

c# - 如何创建可选的 DateTime 参数?

c# - 多列组合框中的选定值

c++ - 运行时抛出的 std::regex_error 异常

c# - VS 2015 : C# NuGet Package restore failed. 文件或目录损坏无法读取

typescript - VS 2015 中是否会有通用应用程序的 TypeScript 模板?

c# - 将字符串在字符串数组中的特定位置分组

C# 如何将对象属性列表显示为逗号分隔的字符串

xaml - UWP 绑定(bind)到属性

c# - 强类型数据集会提高性能吗?

c# - `FallbackValue` 未在 WPF XAML 中使用空 `Path` 进行绑定(bind)评估?