I want to add a new button in the title bar of a UWP application, something that looks like this :
I have already saw some "similar" posts but the answers aren't really clear and it lacks details, it's hard for me to understand what they have done.
by default UWP does not have the ability to add buttons to the titlebar. But uwp support custom titlebar layout.
For starting hide title bar view
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
After Create new grid layout and attach it to title bar
Window.Current.SetTitleBar(UserLayout);
Create TitleBar
and subscribe LayoutMetricsChanged
event that uses to dynamically create Margin, because with a different number of system buttons it will be different.
var tBar = CoreApplication.GetCurrentView().TitleBar;
tBar.LayoutMetricsChanged += OnTitleBarLayoutMetricsChanged;
And add function
public void OnTitleBarLayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)
{
var bar = sender as CoreApplicationViewTitleBar;
RightPanel.Margin = new Thickness(0, 0, bar.SystemOverlayRightInset, 0);
}
Navigate page frame to home page
Content.Navigate(typeof(Home), null, new SuppressNavigationTransitionInfo()); // Navigate to Home page with null args and null animation
End in app.xaml.cs set standart navigation frame to this page
if (e.PrelaunchActivated == false) {
if (rootFrame.Content == null) {
rootFrame.Navigate(typeof(AniMiru.Windows10.Views.AppCustomWindow), e.Arguments);
}
Window.Current.Activate();
}
Page xaml:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="32"/>
<RowDefinition />
</Grid.RowDefinitions>
<Grid x:Name="TopBar" >
<Grid x:Name="UserLayout" Background="#00000000" />
<Grid Canvas.ZIndex="1">
<StackPanel x:Name="LeftPanel" Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Left">
<AutoSuggestBox QueryIcon="Find" PlaceholderText="Search" Width="300" />
</StackPanel>
<StackPanel x:Name="RightPanel" Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Right">
<Button Content="" FontFamily="Segoe MDL2 Assets" FontSize="13" VerticalAlignment="Stretch" />
<Button Content="" FontFamily="Segoe MDL2 Assets" FontSize="13" VerticalAlignment="Stretch" />
</StackPanel>
</Grid>
</Grid>
<Grid Grid.Row="1">
<Frame x:Name="Content" />
</Grid>
</Grid>
Page C#:
public AppCustomWindow()
{
this.InitializeComponent();
// Hide titlebar panel and add new layout to title bar
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
Window.Current.SetTitleBar(UserLayout);
// Add LayoutMetricsChanged Event to TitleBar
var tBar = CoreApplication.GetCurrentView().TitleBar;
tBar.LayoutMetricsChanged += OnTitleBarLayoutMetricsChanged;
// Navigate
Content.Navigate(typeof(Home), null, new SuppressNavigationTransitionInfo()); // Navigate to Home page with null args and null animation
}
public void OnTitleBarLayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)
{
var bar = sender as CoreApplicationViewTitleBar;
RightPanel.Margin = new Thickness(0, 0, bar.SystemOverlayRightInset, 0);
}
Screenshots:
Urls:
Sory for my English.
Best regards.