Added API for opening a window from flyout w/ sample

This commit is contained in:
Daniel Clark
2021-02-11 15:09:20 +00:00
parent 0d28dad842
commit 4a6c6ca4a5
12 changed files with 182 additions and 77 deletions
@@ -123,6 +123,9 @@
<Compile Include="Shell.xaml.cs">
<DependentUpon>Shell.xaml</DependentUpon>
</Compile>
<Compile Include="WindowContent.xaml.cs">
<DependentUpon>WindowContent.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<AppxManifest Include="Package.appxmanifest">
@@ -163,6 +166,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="WindowContent.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\NotificationFlyout.Uwp.UI.Controls\NotificationFlyout.Uwp.UI.Controls.csproj">
+1 -1
View File
@@ -44,7 +44,7 @@
IsIndeterminate="True"
ShowError="False"
ShowPaused="False" />
<Button Margin="0,0,0,8" Content="Button" />
<Button Margin="0,0,0,8" Content="Open window" Click="Button_Click" />
<Slider Margin="0,0,0,8" />
<TextBox Margin="0,0,0,8" />
<CalendarView Margin="0,0,0,8" />
@@ -28,5 +28,11 @@
var app = GetApplication();
app.Exit();
}
private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
var app = GetApplication();
app.OpenAsWindow<WindowContent>();
}
}
}
@@ -0,0 +1,27 @@
<UserControl
x:Class="NotificationFlyoutSample.WindowContent"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls">
<muxc:NavigationView x:Name="nvSample">
<muxc:NavigationView.MenuItems>
<muxc:NavigationViewItem
Content="Menu Item1"
Icon="Play"
Tag="SamplePage1" />
<muxc:NavigationViewItem
Content="Menu Item2"
Icon="Save"
Tag="SamplePage2" />
<muxc:NavigationViewItem
Content="Menu Item3"
Icon="Refresh"
Tag="SamplePage3" />
<muxc:NavigationViewItem
Content="Menu Item4"
Icon="Download"
Tag="SamplePage4" />
</muxc:NavigationView.MenuItems>
<Frame x:Name="contentFrame" />
</muxc:NavigationView>
</UserControl>
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236
namespace NotificationFlyoutSample
{
public sealed partial class WindowContent : UserControl
{
public WindowContent()
{
this.InitializeComponent();
}
}
}
@@ -1,7 +1,11 @@
namespace NotificationFlyout.Uwp.UI
using Windows.UI.Xaml;
namespace NotificationFlyout.Uwp.UI
{
public interface INotificationFlyoutApplication
{
void Exit();
void OpenAsWindow<TUIElement>() where TUIElement : UIElement;
}
}
@@ -48,6 +48,12 @@ namespace NotificationFlyout.Wpf.UI.Controls
_notificationFlyoutXamlHost.HideFlyout();
}
public void OpenAsWindow<TUIElement>() where TUIElement : Windows.UI.Xaml.UIElement
{
var window = new XamlHost<TUIElement>();
window.Show();
}
public void ShowFlyout()
{
_notificationFlyoutXamlHost.ShowFlyout();
@@ -1,70 +0,0 @@
using Microsoft.Toolkit.Wpf.UI.XamlHost;
using NotificationFlyout.Wpf.UI.Extensions;
using System;
using System.Windows;
using System.Windows.Media;
namespace NotificationFlyout.Wpf.UI.Controls
{
internal class XamlHostWindow<TXamlContent> : Window where TXamlContent : Windows.UI.Xaml.UIElement
{
internal const double WindowSize = 5;
protected new bool IsLoaded;
private WindowsXamlHost _xamlHost;
public XamlHostWindow()
{
PrepareDefaultWindow();
PrepareWindowsXamlHost();
Loaded += OnLoaded;
ContentRendered += OnContentRendered;
}
internal TXamlContent GetHostContent()
{
if (_xamlHost == null) return null;
return _xamlHost.GetUwpInternalObject() as TXamlContent;
}
protected virtual void OnContentLoaded()
{
}
private void OnContentRendered(object sender, EventArgs args)
{
IsLoaded = true;
OnContentLoaded();
}
private void OnLoaded(object sender, RoutedEventArgs args)
{
this.Hidden();
}
private void PrepareDefaultWindow()
{
ShowInTaskbar = false;
ShowActivated = false;
WindowStyle = WindowStyle.None;
ResizeMode = ResizeMode.NoResize;
AllowsTransparency = true;
Background = new SolidColorBrush(Colors.Transparent);
Height = WindowSize;
Width = WindowSize;
}
private void PrepareWindowsXamlHost()
{
_xamlHost = new WindowsXamlHost
{
InitialTypeName = typeof(TXamlContent).FullName,
Height = 0,
Width = 0,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch
};
Content = _xamlHost;
}
}
}
@@ -7,7 +7,7 @@ using Windows.UI.Xaml.Controls;
namespace NotificationFlyout.Wpf.UI.Controls
{
internal class ContextMenuXamlHost : XamlHostWindow<ContextMenuFlyoutHost>
internal class ContextMenuXamlHost : TransparentXamlHost<ContextMenuFlyoutHost>
{
private Uwp.UI.Controls.NotificationFlyout _flyout;
@@ -1,15 +1,15 @@
using NotificationFlyout.Uwp.UI.Controls;
using NotificationFlyout.Wpf.UI.Extensions;
using NotificationFlyout.Uwp.UI.Extensions;
using NotificationFlyout.Wpf.UI.Extensions;
using NotificationFlyout.Wpf.UI.Helpers;
using System;
using System.Windows;
using Windows.UI.Xaml.Controls.Primitives;
using System.Windows.Input;
using Windows.UI.Xaml.Controls.Primitives;
namespace NotificationFlyout.Wpf.UI.Controls
{
internal class NotificationFlyoutXamlHost : XamlHostWindow<NotificationFlyoutHost>
internal class NotificationFlyoutXamlHost : TransparentXamlHost<NotificationFlyoutHost>
{
private const string ShellTrayHandleName = "Shell_TrayWnd";
@@ -176,7 +176,6 @@ namespace NotificationFlyout.Wpf.UI.Controls
_notificationIconHelper.SetIcon(icon.Handle);
}
private void UpdateRequestedTheme()
{
if (_flyout == null) return;
@@ -216,24 +215,28 @@ namespace NotificationFlyout.Wpf.UI.Controls
height = windowHeight;
width = windowWidth;
break;
case TaskbarPosition.Top:
top = taskbarRect.Bottom;
left = FlowDirection == FlowDirection.RightToLeft ? taskbarRect.Left : taskbarRect.Right - windowWidth;
height = windowHeight;
width = windowWidth;
break;
case TaskbarPosition.Right:
top = taskbarRect.Bottom - windowHeight;
left = taskbarRect.Left - windowWidth;
height = windowHeight;
width = windowWidth;
break;
case TaskbarPosition.Bottom:
top = taskbarRect.Top - windowHeight;
left = FlowDirection == FlowDirection.RightToLeft ? taskbarRect.Left : taskbarRect.Right - windowWidth;
height = windowHeight;
width = windowWidth;
break;
default:
throw new ArgumentOutOfRangeException();
}
@@ -0,0 +1,43 @@
using Microsoft.Toolkit.Wpf.UI.XamlHost;
using NotificationFlyout.Wpf.UI.Extensions;
using System.Windows;
using System.Windows.Media;
namespace NotificationFlyout.Wpf.UI.Controls
{
internal class TransparentXamlHost<TXamlContent> : XamlHost<TXamlContent> where TXamlContent : Windows.UI.Xaml.UIElement
{
internal const double WindowSize = 5;
public TransparentXamlHost()
{
Loaded += OnLoaded;
PrepareDefaultWindow();
}
protected override WindowsXamlHost OnPreparingXamlHost(WindowsXamlHost xamlHost)
{
xamlHost.Height = 0;
xamlHost.Width = 0;
return base.OnPreparingXamlHost(xamlHost);
}
private void OnLoaded(object sender, RoutedEventArgs args)
{
this.Hidden();
}
private void PrepareDefaultWindow()
{
ShowInTaskbar = false;
ShowActivated = false;
WindowStyle = WindowStyle.None;
ResizeMode = ResizeMode.NoResize;
AllowsTransparency = true;
Background = new SolidColorBrush(Colors.Transparent);
Height = WindowSize;
Width = WindowSize;
}
}
}
@@ -0,0 +1,52 @@
using Microsoft.Toolkit.Wpf.UI.XamlHost;
using NotificationFlyout.Wpf.UI.Extensions;
using System;
using System.Windows;
namespace NotificationFlyout.Wpf.UI.Controls
{
internal class XamlHost<TXamlContent> : Window where TXamlContent : Windows.UI.Xaml.UIElement
{
protected new bool IsLoaded;
private WindowsXamlHost _xamlHost;
public XamlHost()
{
PrepareWindowsXamlHost();
ContentRendered += OnContentRendered;
}
protected TXamlContent GetHostContent()
{
if (_xamlHost == null) return null;
return _xamlHost.GetUwpInternalObject() as TXamlContent;
}
protected virtual void OnContentLoaded()
{
}
protected virtual WindowsXamlHost OnPreparingXamlHost(WindowsXamlHost xamlHost)
{
xamlHost.InitialTypeName = typeof(TXamlContent).FullName;
xamlHost.HorizontalAlignment = HorizontalAlignment.Stretch;
xamlHost.VerticalAlignment = VerticalAlignment.Stretch;
return xamlHost;
}
private void OnContentRendered(object sender, EventArgs args)
{
IsLoaded = true;
OnContentLoaded();
}
private void PrepareWindowsXamlHost()
{
_xamlHost = new WindowsXamlHost();
OnPreparingXamlHost(_xamlHost);
Content = _xamlHost;
}
}
}