From 3de9ceee0dd968cafbcdd7f15b04c5edf45a1099 Mon Sep 17 00:00:00 2001 From: Daniel Clark Date: Sat, 6 Feb 2021 16:05:19 +0000 Subject: [PATCH] simplify setting the flyout content --- NotificationFlyout.Tray/Views/Shell.xaml | 4 +-- .../NotificationFlyout/NotificationFlyout.cs | 22 ++++++------ .../NotificationFlyoutXamlHost.cs | 35 ++++--------------- 3 files changed, 20 insertions(+), 41 deletions(-) diff --git a/NotificationFlyout.Tray/Views/Shell.xaml b/NotificationFlyout.Tray/Views/Shell.xaml index 8d177be..5bba08c 100644 --- a/NotificationFlyout.Tray/Views/Shell.xaml +++ b/NotificationFlyout.Tray/Views/Shell.xaml @@ -6,7 +6,5 @@ - - - + diff --git a/NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyout.cs b/NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyout.cs index a8400b8..38b6be0 100644 --- a/NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyout.cs +++ b/NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyout.cs @@ -1,9 +1,11 @@ using NotificationFlyout.Wpf.UI.Extensions; using NotificationFlyout.Wpf.UI.Helpers; using System.Windows; +using System.Windows.Markup; namespace NotificationFlyout.Wpf.UI.Controls { + [ContentProperty(nameof(Content))] public class NotificationFlyout : DependencyObject { private const string ShellTrayHandleName = "Shell_TrayWnd"; @@ -13,10 +15,10 @@ namespace NotificationFlyout.Wpf.UI.Controls typeof(NotificationFlyoutIcon), typeof(NotificationFlyout), new PropertyMetadata(null, OnIconPropertyChanged)); - public static DependencyProperty FlyoutContentProperty = - DependencyProperty.Register(nameof(FlyoutContent), + public static DependencyProperty ContentProperty = + DependencyProperty.Register(nameof(Content), typeof(Windows.UI.Xaml.UIElement), typeof(NotificationFlyout), - new PropertyMetadata(null, OnFlyoutContentPropertyChanged)); + new PropertyMetadata(null, OnContentPropertyChanged)); private NotificationFlyoutXamlHost _xamlHost; @@ -26,10 +28,10 @@ namespace NotificationFlyout.Wpf.UI.Controls _xamlHost.Show(); } - public Windows.UI.Xaml.UIElement FlyoutContent + public Windows.UI.Xaml.UIElement Content { - get => (Windows.UI.Xaml.UIElement)GetValue(FlyoutContentProperty); - set => SetValue(FlyoutContentProperty, value); + get => (Windows.UI.Xaml.UIElement)GetValue(ContentProperty); + set => SetValue(ContentProperty, value); } public NotificationFlyoutIcon Icon @@ -48,10 +50,10 @@ namespace NotificationFlyout.Wpf.UI.Controls _xamlHost.ShowFlyout(); } - private static void OnFlyoutContentPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) + private static void OnContentPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) { var sender = dependencyObject as NotificationFlyout; - sender?.OnFlyoutContentPropertyChanged(); + sender?.OnContentPropertyChanged(); } private static void OnIconPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) @@ -60,9 +62,9 @@ namespace NotificationFlyout.Wpf.UI.Controls sender?.OnIconPropertyChanged(); } - private void OnFlyoutContentPropertyChanged() + private void OnContentPropertyChanged() { - _xamlHost.FlyoutContent = FlyoutContent; + _xamlHost.SetFlyoutContent(Content); } private void OnIconPropertyChanged() diff --git a/NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyoutXamlHost.cs b/NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyoutXamlHost.cs index bc613eb..34e30de 100644 --- a/NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyoutXamlHost.cs +++ b/NotificationFlyout.Wpf.UI.Controls/NotificationFlyout/NotificationFlyoutXamlHost.cs @@ -3,7 +3,6 @@ using NotificationFlyout.Uwp.UI.Controls; using NotificationFlyout.Wpf.UI.Extensions; using NotificationFlyout.Wpf.UI.Helpers; using System; -using System.Drawing; using System.Windows; using System.Windows.Media; using Windows.UI.Xaml.Controls.Primitives; @@ -12,11 +11,7 @@ namespace NotificationFlyout.Wpf.UI.Controls { internal class NotificationFlyoutXamlHost : Window { - internal static DependencyProperty FlyoutContentProperty = - DependencyProperty.Register(nameof(FlyoutContent), - typeof(Windows.UI.Xaml.UIElement), typeof(NotificationFlyoutXamlHost), - new PropertyMetadata(null, OnFlyoutContentPropertyChanged)); - + private const double MaximumOffset = 80; private WindowsXamlHost _host; private NotificationIconHelper _notificationIconHelper; @@ -30,10 +25,13 @@ namespace NotificationFlyout.Wpf.UI.Controls Loaded += OnLoaded; } - public Windows.UI.Xaml.UIElement FlyoutContent + public void SetFlyoutContent(Windows.UI.Xaml.UIElement content) { - get => (Windows.UI.Xaml.UIElement)GetValue(FlyoutContentProperty); - set => SetValue(FlyoutContentProperty, value); + var flyoutPresenter = GetNotificationFlyoutPresenter(); + if (flyoutPresenter != null) + { + flyoutPresenter.Content = content; + } } internal void HideFlyout() @@ -49,9 +47,6 @@ namespace NotificationFlyout.Wpf.UI.Controls { _notificationIconHelper.SetIcon(handle); } - - private const double MaximumOffset = 80; - internal void ShowFlyout() { var flyoutPresenter = GetNotificationFlyoutPresenter(); @@ -72,27 +67,11 @@ namespace NotificationFlyout.Wpf.UI.Controls } } - private static void OnFlyoutContentPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) - { - var sender = dependencyObject as NotificationFlyoutXamlHost; - sender?.OnFlyoutContentPropertyChanged(); - } - private NotificationFlyoutPresenter GetNotificationFlyoutPresenter() { if (_host == null) return null; return _host.GetUwpInternalObject() as NotificationFlyoutPresenter; } - - private void OnFlyoutContentPropertyChanged() - { - var flyoutPresenter = GetNotificationFlyoutPresenter(); - if (flyoutPresenter != null) - { - flyoutPresenter.Content = FlyoutContent; - } - } - private void OnIconInvoked(object sender, NotificationIconInvokedEventArgs args) { ShowFlyout();