Switching between RequestedTheme and System theme is RequetedTheme is Default should be fully working

This commit is contained in:
Daniel Clark
2021-02-08 14:02:24 +00:00
parent 6f3fff7351
commit 94bb6351fe
@@ -1,4 +1,5 @@
using Windows.UI.Xaml; using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
namespace NotificationFlyout.Uwp.UI.Controls namespace NotificationFlyout.Uwp.UI.Controls
@@ -7,10 +8,32 @@ namespace NotificationFlyout.Uwp.UI.Controls
{ {
private NotificationFlyoutContentPresenter _contentPresenter; private NotificationFlyoutContentPresenter _contentPresenter;
private ElementTheme _systemTheme;
public NotificationFlyoutPresenter() public NotificationFlyoutPresenter()
{ {
DefaultStyleKey = typeof(NotificationFlyoutPresenter); DefaultStyleKey = typeof(NotificationFlyoutPresenter);
ActualThemeChanged += OnActualThemeChanged; RegisterPropertyChangedCallback(RequestedThemeProperty, RequestedThemePropertyChanged);
}
public void SetBackground(string theme)
{
if (_contentPresenter == null) return;
switch (theme)
{
case "Dark":
_systemTheme = ElementTheme.Dark;
break;
case "Light":
_systemTheme = ElementTheme.Light;
break;
}
if (RequestedTheme == ElementTheme.Default)
{
_contentPresenter.SetValue(RequestedThemeProperty, _systemTheme);
}
} }
protected override void OnApplyTemplate() protected override void OnApplyTemplate()
@@ -18,30 +41,16 @@ namespace NotificationFlyout.Uwp.UI.Controls
_contentPresenter = GetTemplateChild("ContentPresenter") as NotificationFlyoutContentPresenter; _contentPresenter = GetTemplateChild("ContentPresenter") as NotificationFlyoutContentPresenter;
} }
public void SetBackground(string theme) private void RequestedThemePropertyChanged(DependencyObject sender, DependencyProperty dp)
{ {
if (_contentPresenter == null) return;
if (RequestedTheme == ElementTheme.Default) if (RequestedTheme == ElementTheme.Default)
{ {
ActualThemeChanged -= OnActualThemeChanged; _contentPresenter.SetValue(RequestedThemeProperty, _systemTheme);
switch (theme)
{
case "Dark":
_contentPresenter.SetValue(RequestedThemeProperty, ElementTheme.Dark);
break;
case "Light":
_contentPresenter.SetValue(RequestedThemeProperty, ElementTheme.Light);
break;
} }
else
ActualThemeChanged += OnActualThemeChanged;
}
}
private void OnActualThemeChanged(FrameworkElement sender, object args)
{ {
_contentPresenter.SetValue(RequestedThemeProperty, RequestedTheme); _contentPresenter.SetValue(RequestedThemeProperty, RequestedTheme);
} }
} }
} }
}