【问题标题】:Preventing commandbar from going into landscape mode防止命令栏进入横向模式
【发布时间】:2017-01-25 11:52:31
【问题描述】:

我正在制作一个应用程序,我的第一个 UWP 应用程序,我已经花了几个小时寻找它。我需要命令栏保持纵向模式,而应用程序的所有其余部分都进入横向模式。我需要这样的东西:

in portrait mode

And in landscape mode, see the bar didn't moved.

到目前为止,这是我的代码:

<Page.BottomAppBar>
    <CommandBar IsSticky="True" Name="cmdBar_ed">
        <CommandBar.PrimaryCommands>
            <AppBarButton Name="newBtn" Label="" Width="30" Click="newFile">
                <AppBarButton.Icon>
                    <BitmapIcon UriSource="ms-appx:///Assets/test.png"/>
                </AppBarButton.Icon>
            </AppBarButton>
            <AppBarButton Name="newBtn1" Label="" Width="30" Click="newFile">
                <AppBarButton.Icon>
                    <BitmapIcon UriSource="ms-appx:///Assets/test.png"/>
                </AppBarButton.Icon>
            </AppBarButton>
            <AppBarButton Name="newBtn2" Label="" Width="30" Click="newFile">
                <AppBarButton.Icon>
                    <BitmapIcon UriSource="ms-appx:///Assets/test.png"/>
                </AppBarButton.Icon>
            </AppBarButton>
            <AppBarButton Name="newBtn3" Label="" Width="30" Click="newFile">
                <AppBarButton.Icon>
                    <BitmapIcon UriSource="ms-appx:///Assets/test.png"/>
                </AppBarButton.Icon>
            </AppBarButton>
            <AppBarButton Name="newBtn4" Label="" Width="30" Click="newFile">
                <AppBarButton.Icon>
                    <BitmapIcon UriSource="ms-appx:///Assets/test.png"/>
                </AppBarButton.Icon>
            </AppBarButton>
            <AppBarButton Name="btnTab" Label="" Width="30" Click="insertTab">
                <AppBarButton.Icon>
                    <BitmapIcon UriSource="ms-appx:///Assets/btnIcon/Tab.png"/>
                </AppBarButton.Icon>
            </AppBarButton>
        </CommandBar.PrimaryCommands>
        <CommandBar.SecondaryCommands>
            <AppBarButton Name="btn_save" Icon="Save" Label="Save" Click="save"/>
            <AppBarButton Name="btn_saveAs" Icon="Save" Label="Save As" Click="saveAs"/>
            <AppBarButton Name="btnClose" Icon="Cancel" Label="Close File" Click="close" />
        </CommandBar.SecondaryCommands>
    </CommandBar>
</Page.BottomAppBar>

另外,另一个奇怪的事情是当屏幕键盘被激活时,这个栏会随之移动以保持在屏幕键盘的顶部,而不是在一个固定的位置。我该如何解决?

【问题讨论】:

  • 您确定要开发 UWP 吗?对我来说,这看起来像是一个 Windows Phone 8.1 应用程序,因为在 UWP 应用程序中,AppBar 图标周围没有圆角字形。
  • 是的,我确定它是一个 UWP 应用程序,我正在使用我的 Lumia 435(升级到 windows 10 移动版)来运行它。到目前为止,除了这些问题之外,它仍然有效。

标签: c# mobile uwp


【解决方案1】:

如果你把commandbar放在&lt;Page.BottomAppBar/&gt;,它总是根据当前ApplicationViewOrientation改变它的位置,你无法控制它。

但是如果你把它放到一个面板中(例如,网格):

<Page
x:Class="AppCommandBar.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppCommandBar"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid.RowDefinitions>
        <RowDefinition Height="9*"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <CommandBar Name="cmdBar_ed" Grid.Row="1" Grid.ColumnSpan="2">
        <CommandBar.PrimaryCommands>
            <AppBarButton Name="newBtn" Label="test" Width="30">
            </AppBarButton>
        </CommandBar.PrimaryCommands>
        <CommandBar.SecondaryCommands>
            <AppBarButton Name="btn_save" Icon="Save" Label="Save"/>
            <AppBarButton Name="btn_saveAs" Icon="Save" Label="Save As"/>
            <AppBarButton Name="btnClose" Icon="Cancel" Label="Close File"/>
        </CommandBar.SecondaryCommands>
    </CommandBar>
</Grid>

您可以通过当前视图的方向手动设置其位置。关于如何检测当前视图的方向,请查看此线程How to detect orientation changes and change layout

【讨论】:

  • 我调查过了,但我仍然不明白如何将网格锁定在那个位置,不知何故,这家伙 here 看到了图片吗?
  • @William 我的意思是更改命令栏在网格中的位置(例如行、列),而不是锁定网格的位置。您可以使用Grid.SetColumnGrid.SetRow 来改变Comamndbar 在不同方向的位置。您需要自定义Commandbar's style 使其垂直显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多