Closed5
WindowsAppSDK1.7以降で追加された、タイトルバーのTheme変更を試す

- AppWindowTitleBar.PreferredTheme to set the light/dark theme of the titlebar

AppSDKを更新しようとMSページに行くと、日本語版だとLatestの安定版が1.6.5と古い情報のまま。。
英語版だと、1.7.1をインストールできる。

下記コードで試す。
MainWindow.xaml.cs
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}
private void darkButton_Click(object sender, RoutedEventArgs e)
{
if (Content is FrameworkElement rootElement)
{
rootElement.RequestedTheme = ElementTheme.Dark;
AppWindow.TitleBar.PreferredTheme = TitleBarTheme.Dark;
}
}
private void lightButton_Click(object sender, RoutedEventArgs e)
{
if (Content is FrameworkElement rootElement)
{
rootElement.RequestedTheme = ElementTheme.Light;
AppWindow.TitleBar.PreferredTheme = TitleBarTheme.Light;
}
}
}
MainWindow.xaml
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel
HorizontalAlignment="Center"
VerticalAlignment="Center"
Orientation="Horizontal">
<Button x:Name="darkButton" Click="darkButton_Click">Dark</Button>
<Button x:Name="lightButton" Click="lightButton_Click">Light</Button>
</StackPanel>
</Grid>

DarkThemeの例
テーマ変更後に、再フォーカスまたはリサイズ等しないと反映が部分的
上記コードで動くことが期待されるけども、テーマ変更後は色味が残ったりする。ウィンドウからフォーカス外したりするとちゃんと反映される。

上記問題の解決をどうするか
- TemplateStudioではここの処理を確かActivate系のSendMessageで対応。
- このAPIは使っていないけど。
- WinUI-Galleryの方はExtendsContentIntoTitleBarだからそもそも無関係。キャプションボタンの設定だけ変更してた。
正直P/Invokeとか必要になってくるぐらいなら従来手法をそのまま流用するので、このPreferredTheme
は、最初のWindow生成時点でテーマが固定(ライトなりダークなり)される場合に選択肢に入るかな。
今後のアップデートで修正されそうだけど。もしかしたらWin10固有の問題だったりして。。。
このスクラップは5ヶ月前にクローズされました