Tunnel flyout events to top level notificationflyout so users can handle events accordingly.

This commit is contained in:
Daniel Clark
2021-02-13 11:47:55 +00:00
parent c7b80cc997
commit 20441d5749
8 changed files with 65 additions and 19 deletions
+1 -1
View File
@@ -58,7 +58,7 @@
<Slider Margin="0,0,0,8" /> <Slider Margin="0,0,0,8" />
<TextBox Margin="0,0,0,8" /> <TextBox Margin="0,0,0,8" />
<CalendarView Margin="0,0,0,8" /> <CalendarView Margin="0,0,0,8" />
<TimePicker Margin="0,0,0,8" /> <TimePicker x:Name="foo" Margin="0,0,0,8" />
<DatePicker /> <DatePicker />
<muxc:NumberBox <muxc:NumberBox
x:Name="NumberBoxSpinButtonPlacementExample" x:Name="NumberBoxSpinButtonPlacementExample"
@@ -1,4 +1,6 @@
namespace NotificationFlyoutSample using Windows.UI.Xaml.Controls;
namespace NotificationFlyoutSample
{ {
public sealed partial class Shell public sealed partial class Shell
{ {
@@ -1,4 +1,5 @@
using System; using System;
using Windows.Foundation;
using Windows.UI.Xaml; using Windows.UI.Xaml;
using Windows.UI.Xaml.Markup; using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media;
@@ -40,8 +41,12 @@ namespace NotificationFlyout.Uwp.UI.Controls
private static INotificationFlyoutApplication _applicationInstance; private static INotificationFlyoutApplication _applicationInstance;
internal event EventHandler ContextMenuChanged; public event EventHandler<object> Closed;
public event TypedEventHandler<NotificationFlyout, NotificationFlyoutClosingEventArgs> Closing;
public event EventHandler<object> Opened;
public event EventHandler<object> Opening;
internal event EventHandler ContextMenuChanged;
internal event EventHandler IconSourceChanged; internal event EventHandler IconSourceChanged;
public UIElement Content public UIElement Content
@@ -84,6 +89,14 @@ namespace NotificationFlyout.Uwp.UI.Controls
internal static void SetApplication(INotificationFlyoutApplication application) => _applicationInstance = application; internal static void SetApplication(INotificationFlyoutApplication application) => _applicationInstance = application;
internal void InvokeClosedEvent(object obj) => Closed?.Invoke(this, obj);
internal void InvokeClosingEvent(NotificationFlyoutClosingEventArgs eventArgs) => Closing?.Invoke(this, eventArgs);
internal void InvokeOpenedEvent(object obj) => Opened?.Invoke(this, obj);
internal void InvokeOpeningEvent(object obj) => Opening?.Invoke(this, obj);
private static void OnContextMenuPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) private static void OnContextMenuPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{ {
var sender = dependencyObject as NotificationFlyout; var sender = dependencyObject as NotificationFlyout;
@@ -96,14 +109,8 @@ namespace NotificationFlyout.Uwp.UI.Controls
sender?.OnIconPropertyChanged(); sender?.OnIconPropertyChanged();
} }
private void OnContextMenuPropertyChanged() private void OnContextMenuPropertyChanged() => ContextMenuChanged?.Invoke(this, EventArgs.Empty);
{
ContextMenuChanged?.Invoke(this, EventArgs.Empty);
}
private void OnIconPropertyChanged() private void OnIconPropertyChanged() => IconSourceChanged?.Invoke(this, EventArgs.Empty);
{
IconSourceChanged?.Invoke(this, EventArgs.Empty);
}
} }
} }
@@ -0,0 +1,9 @@
using System;
namespace NotificationFlyout.Uwp.UI.Controls
{
public sealed class NotificationFlyoutClosingEventArgs : EventArgs
{
public bool Cancel { get; set; }
}
}
@@ -19,10 +19,10 @@ namespace NotificationFlyout.Uwp.UI.Controls
typeof(Style), typeof(NotificationFlyoutHost), typeof(Style), typeof(NotificationFlyoutHost),
new PropertyMetadata(null)); new PropertyMetadata(null));
private Flyout _flyout;
private bool _isLoaded; private bool _isLoaded;
private NotificationFlyout _notificationFlyout;
private string _placement; private string _placement;
private Grid _root; private Grid _root;
public NotificationFlyoutHost() => DefaultStyleKey = typeof(NotificationFlyoutHost); public NotificationFlyoutHost() => DefaultStyleKey = typeof(NotificationFlyoutHost);
@@ -38,6 +38,7 @@ namespace NotificationFlyout.Uwp.UI.Controls
get => (Style)GetValue(FlyoutPresenterStyleProperty); get => (Style)GetValue(FlyoutPresenterStyleProperty);
set => SetValue(FlyoutPresenterStyleProperty, value); set => SetValue(FlyoutPresenterStyleProperty, value);
} }
public void HideFlyout() public void HideFlyout()
{ {
if (_root == null) return; if (_root == null) return;
@@ -63,16 +64,18 @@ namespace NotificationFlyout.Uwp.UI.Controls
flyout.ShowAt(_root, new FlyoutShowOptions flyout.ShowAt(_root, new FlyoutShowOptions
{ {
Placement = placementMode, Placement = placementMode,
ShowMode = FlyoutShowMode.Standard, ShowMode = FlyoutShowMode.Transient,
}); });
} }
internal void SetOwningFlyout(NotificationFlyout flyout) internal void SetOwningFlyout(NotificationFlyout flyout)
{ {
_notificationFlyout = flyout;
BindingOperations.SetBinding(this, ContentProperty, BindingOperations.SetBinding(this, ContentProperty,
new Binding new Binding
{ {
Source = flyout, Source = _notificationFlyout,
Path = Path =
new PropertyPath(nameof(Content)), new PropertyPath(nameof(Content)),
Mode = BindingMode.TwoWay Mode = BindingMode.TwoWay
@@ -81,7 +84,7 @@ namespace NotificationFlyout.Uwp.UI.Controls
BindingOperations.SetBinding(this, RequestedThemeProperty, BindingOperations.SetBinding(this, RequestedThemeProperty,
new Binding new Binding
{ {
Source = flyout, Source = _notificationFlyout,
Path = new PropertyPath(nameof(RequestedTheme)), Path = new PropertyPath(nameof(RequestedTheme)),
Mode = BindingMode.TwoWay Mode = BindingMode.TwoWay
}); });
@@ -89,13 +92,28 @@ namespace NotificationFlyout.Uwp.UI.Controls
BindingOperations.SetBinding(this, FlyoutPresenterStyleProperty, BindingOperations.SetBinding(this, FlyoutPresenterStyleProperty,
new Binding new Binding
{ {
Source = flyout, Source = _notificationFlyout,
Path = new PropertyPath(nameof(FlyoutPresenterStyle)), Path = new PropertyPath(nameof(FlyoutPresenterStyle)),
Mode = BindingMode.TwoWay Mode = BindingMode.TwoWay
}); });
} }
protected override void OnApplyTemplate() protected override void OnApplyTemplate()
{ {
_flyout = GetTemplateChild("Flyout") as Flyout;
if (_flyout != null)
{
_flyout.Closing -= OnFlyoutClosing;
_flyout.Closed -= OnFlyoutClosed;
_flyout.Opening -= OnFlyoutOpening;
_flyout.Opened -= OnFlyoutOpened;
_flyout.Closing += OnFlyoutClosing;
_flyout.Closed += OnFlyoutClosed;
_flyout.Opening += OnFlyoutOpening;
_flyout.Opened += OnFlyoutOpened;
}
_root = GetTemplateChild("Root") as Grid; _root = GetTemplateChild("Root") as Grid;
if (GetTemplateChild("ContentRoot") is Grid contentRoot) if (GetTemplateChild("ContentRoot") is Grid contentRoot)
{ {
@@ -109,5 +127,13 @@ namespace NotificationFlyout.Uwp.UI.Controls
_isLoaded = true; _isLoaded = true;
SetFlyoutPlacement(_placement); SetFlyoutPlacement(_placement);
} }
private void OnFlyoutClosed(object sender, object args) => _notificationFlyout?.InvokeClosedEvent(args);
private void OnFlyoutClosing(FlyoutBase sender, FlyoutBaseClosingEventArgs args) => _notificationFlyout?.InvokeClosingEvent(new NotificationFlyoutClosingEventArgs());
private void OnFlyoutOpened(object sender, object args) => _notificationFlyout?.InvokeOpenedEvent(args);
private void OnFlyoutOpening(object sender, object args) => _notificationFlyout?.InvokeOpeningEvent(args);
} }
} }
@@ -32,7 +32,7 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="System.Drawing.Common" Version="5.0.0" /> <PackageReference Include="System.Drawing.Common" Version="5.0.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>
@@ -128,6 +128,7 @@ namespace NotificationFlyout.Wpf.UI.Controls
_contextMenuXamlHost.SetOwningFlyout(_flyout); _contextMenuXamlHost.SetOwningFlyout(_flyout);
} }
private void PrepareNotificationIcon() private void PrepareNotificationIcon()
{ {
_notificationIconHelper = NotificationIconHelper.Create(this); _notificationIconHelper = NotificationIconHelper.Create(this);
@@ -150,6 +151,7 @@ namespace NotificationFlyout.Wpf.UI.Controls
if (_contextMenuXamlHost == null) return; if (_contextMenuXamlHost == null) return;
_contextMenuXamlHost.ShowContextMenuFlyout(); _contextMenuXamlHost.ShowContextMenuFlyout();
} }
private async void UpdateIcons() private async void UpdateIcons()
{ {
if (!IsLoaded) return; if (!IsLoaded) return;
@@ -17,7 +17,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.Windows.SDK.Contracts" Version="10.0.19041.1" /> <PackageReference Include="Microsoft.Windows.SDK.Contracts" Version="10.0.19041.1" />
<PackageReference Include="System.Drawing.Common" Version="5.0.0" /> <PackageReference Include="System.Drawing.Common" Version="5.0.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>