为什么我的AppBar在页面加载时显示为ClosedDisplayMode.Compact,无论实际设置如何?(Why does my AppBar appear as ClosedDisplayMo

编程入门 行业动态 更新时间:2024-10-28 21:17:08
为什么我的AppBar在页面加载时显示为ClosedDisplayMode.Compact,无论实际设置如何?(Why does my AppBar appear as ClosedDisplayMode.Compact on Page load regardless of actual setting?)

我将一个Win8.1应用移植到Win10的UWP上,并遇到AppBar一个奇怪问题。 我们尝试使用CommandBar而不是AppBar ,但问题仍然存在。 我们正在使用最新版本的MyToolkit(截至撰写本文时为2.5.16)。 我们的观点是这样得出的:

SomeView派生自MtPage的BaseView服务(派生自Page )

因此,对于特定的视图(在这种情况下, HomeView ),XAML看起来像:

<views:BaseView x:Name="pageRoot" x:Class="OurApp.HomeView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:controls="using:OurApp.Controls" xmlns:views="using:OurApp.Views" xmlns:viewModels="using:ViewModels" xmlns:fake="using:ViewModels.Fake" mc:Ignorable="d" > <views:BaseView.TopAppBar> <AppBar> <controls:NavigationView x:Name="NavigationView"> <controls:NavigationView.Context> <viewModels:NavigationViewModel /> </controls:NavigationView.Context> </controls:NavigationView> </AppBar> </views:BaseView.TopAppBar> <!-- other stuff not relevant to AppBars, etc --> </views:BaseView>

其中NavigationView是具有一些按钮的UserControl , NavigationViewContext和NavigationViewModel描述哪些按钮应在哪个页面上处于活动状态,等等。

问题在于,这导致了一种半开半关AppBar外观(几乎不完全像ClosedDisplayMode设置为Compact一样),如下所示:

在向此问题提及的<AppBar>控件中添加了ClosedDisplayMode="Minimal"之后,活动可视化树确认AppBar具有IsOpen = 0和AppBarClosedDisplayMode.Minimal ...但它仍顽固地显示为半开放,如上面的截图。

奇怪的是,如果用户从HomeView导航到某个其他视图然后再回到它, AppBar将正确呈现与AppBarClosedDisplayMode.Minimal (!):

我们尝试处理视图的NavigatedTo事件并手动强制将ClosedDisplayMode强制为Minimal ,但这不会影响呈现的输出(并且在任何情况下,实时可视化树都会确认它已被正确设置为Minimal )。

任何想法为什么会发生这种情况,以及/或者如何使AppBar以ClosedDisplayMode = Minimal呈现, ClosedDisplayMode = Minimal导航? 我敢肯定,我可能会以某种方式蛮横,但我觉得可能有更好的方法,或者我错过了很简单的事情。

I'm porting a Win8.1 app to UWP for Win10 and experiencing a strange issue with the AppBar. We've tried using CommandBar instead of AppBar, but the issue still occurs for us. We're on the latest version of MyToolkit (2.5.16 as of this writing). Our views are derived like so:

SomeView derives from BaseView dervices from MtPage (derives from Page)

So, for a particular view (in this case, HomeView), the XAML looks like:

<views:BaseView x:Name="pageRoot" x:Class="OurApp.HomeView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:controls="using:OurApp.Controls" xmlns:views="using:OurApp.Views" xmlns:viewModels="using:ViewModels" xmlns:fake="using:ViewModels.Fake" mc:Ignorable="d" > <views:BaseView.TopAppBar> <AppBar> <controls:NavigationView x:Name="NavigationView"> <controls:NavigationView.Context> <viewModels:NavigationViewModel /> </controls:NavigationView.Context> </controls:NavigationView> </AppBar> </views:BaseView.TopAppBar> <!-- other stuff not relevant to AppBars, etc --> </views:BaseView>

Where NavigationView is a UserControl that has some buttons, and NavigationViewContext and NavigationViewModel describe which buttons should be active on which page, and so forth.

The problem is that this results in a sort of half-open, half-closed AppBar appearance (almost but not quite exactly as if ClosedDisplayMode were set to Compact) like so:

After adding ClosedDisplayMode="Minimal" to the <AppBar> control, as alluded to in this question, the live visual tree confirms that the AppBar has IsOpen = 0 and AppBarClosedDisplayMode.Minimal... but it still stubbornly appears half-open as in the screenshot above.

Strangely, if the user navigates away from HomeView to some other view and then back to it, the AppBar is correctly rendered with AppBarClosedDisplayMode.Minimal (!):

We've tried handling the view's NavigatedTo event and manually forcing ClosedDisplayMode to Minimal, but that doesn't affect the rendered output (and in any case, the live visual tree confirms that that is already being correctly set to Minimal).

Any ideas why this is happening, and/or how to cause the AppBar to be rendered with ClosedDisplayMode = Minimal without having to navigate first? I'm sure I could probably brute force this somehow, but I feel like there's probably a better way or I'm missing something pretty simple.

最满意答案

只需切换到CommandBar 。 CommandBar非常适用于Minimal和Compact模式。 推荐使用CommandBar来控制AppBar 。 显然,保持AppBar的唯一原因是尽量减少更改。

AppBar - MSDN 。

重要仅当您升级使用AppBar的通用Windows 8应用程序并且需要最小化更改时,才应该使用AppBar。 对于Windows 10中的新应用程序,我们建议使用CommandBar控件。

在这里输入图像描述

页:

<paging:MtPage x:Class="App3.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App3" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:paging="using:MyToolkit.Paging" mc:Ignorable="d"> <paging:MtPage.Resources> </paging:MtPage.Resources> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> </Grid> <paging:MtPage.TopAppBar> <CommandBar x:Name="appbar1" ClosedDisplayMode="Minimal" > <CommandBar.Content> <local:MyUserControl1></local:MyUserControl1> </CommandBar.Content> </CommandBar> </paging:MtPage.TopAppBar>

用户控制:

<UserControl x:Class="App3.MyUserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App3" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid> <StackPanel Orientation="Horizontal"> <AppBarButton Icon="Home" Label="Home"></AppBarButton> <AppBarButton Icon="Back" Label="Back"></AppBarButton> <AppBarButton Icon="Rotate" Label="Rotate"></AppBarButton> </StackPanel> </Grid>

Just switch to CommandBar. CommandBar works out-of-the-box perfectly well, for both, Minimal and Compact modes. CommandBar is the recommended preferred control over AppBar. Apparently, the only reason for keeping AppBar is to minimize changes.

AppBar - MSDN.

Important You should use the AppBar only when you are upgrading a Universal Windows 8 app that uses the AppBar, and need to minimize changes. For new apps in Windows 10, we recommend using the CommandBar control instead.

enter image description here

Page:

<paging:MtPage x:Class="App3.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App3" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:paging="using:MyToolkit.Paging" mc:Ignorable="d"> <paging:MtPage.Resources> </paging:MtPage.Resources> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> </Grid> <paging:MtPage.TopAppBar> <CommandBar x:Name="appbar1" ClosedDisplayMode="Minimal" > <CommandBar.Content> <local:MyUserControl1></local:MyUserControl1> </CommandBar.Content> </CommandBar> </paging:MtPage.TopAppBar>

User Control:

<UserControl x:Class="App3.MyUserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App3" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid> <StackPanel Orientation="Horizontal"> <AppBarButton Icon="Home" Label="Home"></AppBarButton> <AppBarButton Icon="Back" Label="Back"></AppBarButton> <AppBarButton Icon="Rotate" Label="Rotate"></AppBarButton> </StackPanel> </Grid>

更多推荐

本文发布于:2023-08-05 04:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427056.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   页面   ClosedDisplayMode   AppBar   Compact

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!