小智 9
默认情况下,UWP 无法向标题栏添加按钮。但uwp支持自定义标题栏布局。
\n\n用于启动隐藏标题栏视图
\n\nCoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;\n
Run Code Online (Sandbox Code Playgroud)\n\n创建新的网格布局并将其附加到标题栏后
\n\nWindow.Current.SetTitleBar(UserLayout);\n
Run Code Online (Sandbox Code Playgroud)\n\n创建TitleBar
并订阅LayoutMetricsChanged
用于动态创建 Margin 的事件,因为系统按钮数量不同,情况也会有所不同。
var tBar = CoreApplication.GetCurrentView().TitleBar;\ntBar.LayoutMetricsChanged += OnTitleBarLayoutMetricsChanged;\n
Run Code Online (Sandbox Code Playgroud)\n\n并添加功能
\n\npublic void OnTitleBarLayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)\n{\n var bar = sender as CoreApplicationViewTitleBar;\n RightPanel.Margin = new Thickness(0, 0, bar.SystemOverlayRightInset, 0);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n将页面框架导航到主页
\n\nContent.Navigate(typeof(Home), null, new SuppressNavigationTransitionInfo()); // Navigate to Home page with null args and null animation\n
Run Code Online (Sandbox Code Playgroud)\n\n结束于 app.xaml.cs 将标准导航框架设置为此页面
\n\nif (e.PrelaunchActivated == false) {\n if (rootFrame.Content == null) {\n rootFrame.Navigate(typeof(AniMiru.Windows10.Views.AppCustomWindow), e.Arguments);\n }\n Window.Current.Activate();\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n页面xaml:
\n\n<Grid>\n <Grid.RowDefinitions>\n <RowDefinition Height="32"/>\n <RowDefinition />\n </Grid.RowDefinitions>\n <Grid x:Name="TopBar" >\n <Grid x:Name="UserLayout" Background="#00000000" />\n <Grid Canvas.ZIndex="1">\n <StackPanel x:Name="LeftPanel" Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Left">\n <AutoSuggestBox QueryIcon="Find" PlaceholderText="Search" Width="300" />\n </StackPanel>\n <StackPanel x:Name="RightPanel" Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Right">\n <Button Content="\xee\x84\x9d" FontFamily="Segoe MDL2 Assets" FontSize="13" VerticalAlignment="Stretch" />\n <Button Content="\xee\x85\xa1" FontFamily="Segoe MDL2 Assets" FontSize="13" VerticalAlignment="Stretch" />\n </StackPanel>\n </Grid>\n </Grid>\n <Grid Grid.Row="1">\n <Frame x:Name="Content" />\n </Grid>\n </Grid>\n
Run Code Online (Sandbox Code Playgroud)\n\nC#页面:
\n\npublic AppCustomWindow()\n{\n this.InitializeComponent();\n\n // Hide titlebar panel and add new layout to title bar\n CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;\n Window.Current.SetTitleBar(UserLayout);\n\n // Add LayoutMetricsChanged Event to TitleBar\n var tBar = CoreApplication.GetCurrentView().TitleBar;\n tBar.LayoutMetricsChanged += OnTitleBarLayoutMetricsChanged;\n\n // Navigate\n Content.Navigate(typeof(Home), null, new SuppressNavigationTransitionInfo()); // Navigate to Home page with null args and null animation\n}\n\npublic void OnTitleBarLayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)\n{\n var bar = sender as CoreApplicationViewTitleBar;\n RightPanel.Margin = new Thickness(0, 0, bar.SystemOverlayRightInset, 0);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n截图:
\n\n\n\n网址:
\n\n\n\n\n\n\n\n对不起我的英语。
\n\n此致。
\n