simplify setting the flyout content

This commit is contained in:
Daniel Clark
2021-02-06 16:05:19 +00:00
parent 2be7a1128c
commit 3de9ceee0d
3 changed files with 20 additions and 41 deletions
+1 -3
View File
@@ -6,7 +6,5 @@
<NotificationFlyout.Icon> <NotificationFlyout.Icon>
<NotificationFlyoutIcon IconSource="/Assets/Icon.ico" /> <NotificationFlyoutIcon IconSource="/Assets/Icon.ico" />
</NotificationFlyout.Icon> </NotificationFlyout.Icon>
<NotificationFlyout.FlyoutContent> <sample:MyUserControl1 />
<sample:MyUserControl1 />
</NotificationFlyout.FlyoutContent>
</NotificationFlyout> </NotificationFlyout>
@@ -1,9 +1,11 @@
using NotificationFlyout.Wpf.UI.Extensions; using NotificationFlyout.Wpf.UI.Extensions;
using NotificationFlyout.Wpf.UI.Helpers; using NotificationFlyout.Wpf.UI.Helpers;
using System.Windows; using System.Windows;
using System.Windows.Markup;
namespace NotificationFlyout.Wpf.UI.Controls namespace NotificationFlyout.Wpf.UI.Controls
{ {
[ContentProperty(nameof(Content))]
public class NotificationFlyout : DependencyObject public class NotificationFlyout : DependencyObject
{ {
private const string ShellTrayHandleName = "Shell_TrayWnd"; private const string ShellTrayHandleName = "Shell_TrayWnd";
@@ -13,10 +15,10 @@ namespace NotificationFlyout.Wpf.UI.Controls
typeof(NotificationFlyoutIcon), typeof(NotificationFlyout), typeof(NotificationFlyoutIcon), typeof(NotificationFlyout),
new PropertyMetadata(null, OnIconPropertyChanged)); new PropertyMetadata(null, OnIconPropertyChanged));
public static DependencyProperty FlyoutContentProperty = public static DependencyProperty ContentProperty =
DependencyProperty.Register(nameof(FlyoutContent), DependencyProperty.Register(nameof(Content),
typeof(Windows.UI.Xaml.UIElement), typeof(NotificationFlyout), typeof(Windows.UI.Xaml.UIElement), typeof(NotificationFlyout),
new PropertyMetadata(null, OnFlyoutContentPropertyChanged)); new PropertyMetadata(null, OnContentPropertyChanged));
private NotificationFlyoutXamlHost _xamlHost; private NotificationFlyoutXamlHost _xamlHost;
@@ -26,10 +28,10 @@ namespace NotificationFlyout.Wpf.UI.Controls
_xamlHost.Show(); _xamlHost.Show();
} }
public Windows.UI.Xaml.UIElement FlyoutContent public Windows.UI.Xaml.UIElement Content
{ {
get => (Windows.UI.Xaml.UIElement)GetValue(FlyoutContentProperty); get => (Windows.UI.Xaml.UIElement)GetValue(ContentProperty);
set => SetValue(FlyoutContentProperty, value); set => SetValue(ContentProperty, value);
} }
public NotificationFlyoutIcon Icon public NotificationFlyoutIcon Icon
@@ -48,10 +50,10 @@ namespace NotificationFlyout.Wpf.UI.Controls
_xamlHost.ShowFlyout(); _xamlHost.ShowFlyout();
} }
private static void OnFlyoutContentPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) private static void OnContentPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{ {
var sender = dependencyObject as NotificationFlyout; var sender = dependencyObject as NotificationFlyout;
sender?.OnFlyoutContentPropertyChanged(); sender?.OnContentPropertyChanged();
} }
private static void OnIconPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) private static void OnIconPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
@@ -60,9 +62,9 @@ namespace NotificationFlyout.Wpf.UI.Controls
sender?.OnIconPropertyChanged(); sender?.OnIconPropertyChanged();
} }
private void OnFlyoutContentPropertyChanged() private void OnContentPropertyChanged()
{ {
_xamlHost.FlyoutContent = FlyoutContent; _xamlHost.SetFlyoutContent(Content);
} }
private void OnIconPropertyChanged() private void OnIconPropertyChanged()
@@ -3,7 +3,6 @@ using NotificationFlyout.Uwp.UI.Controls;
using NotificationFlyout.Wpf.UI.Extensions; using NotificationFlyout.Wpf.UI.Extensions;
using NotificationFlyout.Wpf.UI.Helpers; using NotificationFlyout.Wpf.UI.Helpers;
using System; using System;
using System.Drawing;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Controls.Primitives;
@@ -12,11 +11,7 @@ namespace NotificationFlyout.Wpf.UI.Controls
{ {
internal class NotificationFlyoutXamlHost : Window internal class NotificationFlyoutXamlHost : Window
{ {
internal static DependencyProperty FlyoutContentProperty = private const double MaximumOffset = 80;
DependencyProperty.Register(nameof(FlyoutContent),
typeof(Windows.UI.Xaml.UIElement), typeof(NotificationFlyoutXamlHost),
new PropertyMetadata(null, OnFlyoutContentPropertyChanged));
private WindowsXamlHost _host; private WindowsXamlHost _host;
private NotificationIconHelper _notificationIconHelper; private NotificationIconHelper _notificationIconHelper;
@@ -30,10 +25,13 @@ namespace NotificationFlyout.Wpf.UI.Controls
Loaded += OnLoaded; Loaded += OnLoaded;
} }
public Windows.UI.Xaml.UIElement FlyoutContent public void SetFlyoutContent(Windows.UI.Xaml.UIElement content)
{ {
get => (Windows.UI.Xaml.UIElement)GetValue(FlyoutContentProperty); var flyoutPresenter = GetNotificationFlyoutPresenter();
set => SetValue(FlyoutContentProperty, value); if (flyoutPresenter != null)
{
flyoutPresenter.Content = content;
}
} }
internal void HideFlyout() internal void HideFlyout()
@@ -49,9 +47,6 @@ namespace NotificationFlyout.Wpf.UI.Controls
{ {
_notificationIconHelper.SetIcon(handle); _notificationIconHelper.SetIcon(handle);
} }
private const double MaximumOffset = 80;
internal void ShowFlyout() internal void ShowFlyout()
{ {
var flyoutPresenter = GetNotificationFlyoutPresenter(); 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() private NotificationFlyoutPresenter GetNotificationFlyoutPresenter()
{ {
if (_host == null) return null; if (_host == null) return null;
return _host.GetUwpInternalObject() as NotificationFlyoutPresenter; return _host.GetUwpInternalObject() as NotificationFlyoutPresenter;
} }
private void OnFlyoutContentPropertyChanged()
{
var flyoutPresenter = GetNotificationFlyoutPresenter();
if (flyoutPresenter != null)
{
flyoutPresenter.Content = FlyoutContent;
}
}
private void OnIconInvoked(object sender, NotificationIconInvokedEventArgs args) private void OnIconInvoked(object sender, NotificationIconInvokedEventArgs args)
{ {
ShowFlyout(); ShowFlyout();