Skip to content

Commit ce5a954

Browse files
authored
Migrate SystemBackdrop to XamlCompositionBrushBase (#12)
Migrate SystemBackdrop to XamlCompositionBrushBase
1 parent 98e6629 commit ce5a954

51 files changed

Lines changed: 3024 additions & 4246 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<wux:Application
2-
x:Class="MileXamlControlsDemoNetCore.App"
3-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5-
xmlns:wux="using:Windows.UI.Xaml">
6-
<wux:Application.Resources>
7-
<wux:ResourceDictionary>
8-
<wux:ResourceDictionary.MergedDictionaries>
9-
<wux:ResourceDictionary Source="ms-appx:///Mile.Xaml.Styles.SunValley.xbf" />
10-
</wux:ResourceDictionary.MergedDictionaries>
11-
</wux:ResourceDictionary>
12-
</wux:Application.Resources>
2+
x:Class="MileXamlControlsDemoNetCore.App"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:wux="using:Windows.UI.Xaml">
6+
<wux:Application.Resources>
7+
<wux:ResourceDictionary>
8+
<wux:ResourceDictionary.MergedDictionaries>
9+
<wux:ResourceDictionary Source="ms-appx:///Mile.Xaml.Styles.SunValley.xbf" />
10+
</wux:ResourceDictionary.MergedDictionaries>
11+
</wux:ResourceDictionary>
12+
</wux:Application.Resources>
1313
</wux:Application>

MileXamlControlsDemoNetCore/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace MileXamlControlsDemoNetCore
55
{
6-
sealed partial class App : Application
6+
public sealed partial class App : Application
77
{
88
public App()
99
{

MileXamlControlsDemoNetCore/Form1.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ namespace MileXamlControlsDemoNetCore
66
{
77
public partial class Form1 : Form
88
{
9-
WindowsXamlHost xamlHost = new WindowsXamlHost();
9+
private readonly WindowsXamlHost xamlHost = new();
1010

1111
public Form1()
1212
{
1313
InitializeComponent();
1414

15-
1615
this.Controls.Add(xamlHost);
1716
xamlHost.AutoSize = true;
1817
xamlHost.Dock = DockStyle.Fill;
@@ -23,7 +22,6 @@ public Form1()
2322

2423
private void Form1_Load(object sender, EventArgs e)
2524
{
26-
2725
}
2826
}
2927
}
Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,10 @@
1-
using System;
2-
using System.Runtime.InteropServices;
3-
using System.Windows.Forms;
4-
using Windows.System;
5-
using Windows.UI.Composition.Desktop;
6-
using Windows.UI.Xaml;
7-
using MileXamlControlsDemoNetCore.WindowsAPI.ComTypes;
8-
using MileXamlControlsDemoNetCore.WindowsAPI.PInvoke.Combase;
9-
using System.Runtime.InteropServices.Marshalling;
1+
using MileXamlControlsDemoNetCore.WindowsAPI.ComTypes;
2+
using Windows.Foundation;
103

11-
namespace MileXamlControlsDemoNetCore.Backdrop
4+
namespace MileXamlControlsDemoNetCore.Helpers
125
{
136
public static class BackdropHelper
147
{
15-
private static readonly StrategyBasedComWrappers strategyBasedComWrappers = new();
16-
17-
public static Lazy<IPropertyValueStatics> PropertyValueStatics { get; } = new(() => GetActivationFactory<IPropertyValueStatics>("Windows.Foundation.PropertyValue", typeof(IPropertyValueStatics).GUID));
18-
19-
public static T GetActivationFactory<T>(string activatableClassId, Guid iid)
20-
{
21-
if (!string.IsNullOrEmpty(activatableClassId))
22-
{
23-
Marshal.ThrowExceptionForHR(CombaseLibrary.WindowsCreateString(activatableClassId, activatableClassId.Length, out IntPtr stringPtr));
24-
Marshal.ThrowExceptionForHR(CombaseLibrary.RoGetActivationFactory(stringPtr, iid, out IntPtr comp));
25-
Marshal.ThrowExceptionForHR(CombaseLibrary.WindowsDeleteString(stringPtr));
26-
return (T)strategyBasedComWrappers.GetOrCreateObjectForComInstance(comp, CreateObjectFlags.None);
27-
}
28-
else
29-
{
30-
return default;
31-
}
32-
}
33-
34-
public static DesktopWindowTarget InitializeDesktopWindowTarget(Form form, bool isTopMost)
35-
{
36-
if (form.Handle == IntPtr.Zero)
37-
{
38-
throw new NullReferenceException("Form window not initialized");
39-
}
40-
41-
IntPtr desktopWindowTargetPtr = IntPtr.Zero;
42-
if (DispatcherQueue.GetForCurrentThread() is not null)
43-
{
44-
ICompositorDesktopInterop interop = Window.Current.Compositor as object as ICompositorDesktopInterop;
45-
interop.CreateDesktopWindowTarget(form.Handle, isTopMost, out desktopWindowTargetPtr);
46-
}
47-
48-
return desktopWindowTargetPtr != IntPtr.Zero ? DesktopWindowTarget.FromAbi(desktopWindowTargetPtr) : null;
49-
}
8+
public static IPropertyValueStatics PropertyValueStatics { get; } = PropertyValue.As<IPropertyValueStatics>();
509
}
5110
}

MileXamlControlsDemoNetCore/MainPage.xaml

Lines changed: 364 additions & 358 deletions
Large diffs are not rendered by default.

MileXamlControlsDemoNetCore/MainPage.xaml.cs

Lines changed: 39 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
using System;
1+
using MileXamlControlsDemoNetCore.UI.Backdrop;
2+
using System;
23
using System.Collections.ObjectModel;
34
using Windows.Media.Core;
45
using Windows.System;
5-
using Windows.UI.Composition.Desktop;
66
using Windows.UI;
77
using Windows.UI.Xaml;
88
using Windows.UI.Xaml.Controls;
99
using Windows.UI.Xaml.Media;
10-
using MileXamlControlsDemoNetCore.UI.Backdrop;
11-
using MileXamlControlsDemoNetCore.Backdrop;
10+
11+
#pragma warning disable IDE0060
1212

1313
namespace MileXamlControlsDemoNetCore
1414
{
1515
public sealed partial class MainPage : Page
1616
{
17-
private ObservableCollection<TreeViewItemModel> TreeViewDataList = [
17+
private ObservableCollection<TreeViewItemModel> TreeViewDataList { get; } = [
1818
new() {
1919
Name = "TreeView Root Node 1", IsSelected = true,
2020
ChildrenNode = [
@@ -32,11 +32,8 @@ public sealed partial class MainPage : Page
3232
}
3333
];
3434

35-
private DesktopWindowTarget DesktopWindowTarget { get; set; }
36-
3735
private ElementTheme currentTheme = ElementTheme.Default;
3836
private bool currentInputActiveState = false;
39-
private SystemBackdrop currentSystemBackdrop = null;
4037

4138
public MainPage()
4239
{
@@ -52,6 +49,7 @@ private async void HyperlinkButton_Click(object sender, RoutedEventArgs args)
5249
var vp9LinkUri = new Uri("ms-windows-store://pdp/?productid=9N4D0MSMP0PT");
5350
await Launcher.LaunchUriAsync(vp9LinkUri);
5451
}
52+
5553
private async void HyperlinkButton_Click1(object sender, RoutedEventArgs args)
5654
{
5755
var vp9LinkUri = new Uri("https://learn.microsoft.com/en-us/answers/questions/635070/(ver-10-0-19041-0)-c-winrt-treeview-nodes-not-prop?childtoview=635698");
@@ -60,15 +58,7 @@ private async void HyperlinkButton_Click1(object sender, RoutedEventArgs args)
6058

6159
private void OnLoaded(object sender, RoutedEventArgs e)
6260
{
63-
DesktopWindowTarget = BackdropHelper.InitializeDesktopWindowTarget(Program.MainForm, false);
64-
65-
currentSystemBackdrop = new MicaBackdrop(DesktopWindowTarget, this, Program.MainForm)
66-
{
67-
Kind = MicaKind.Base,
68-
RequestedTheme = currentTheme,
69-
IsInputActive = currentInputActiveState
70-
};
71-
currentSystemBackdrop.InitializeBackdrop();
61+
Background = new MicaBrush(MicaKind.Base, this, Program.MainForm, currentInputActiveState);
7262

7363
currentTheme = ElementTheme.Default;
7464
currentInputActiveState = false;
@@ -84,86 +74,39 @@ private void SwitchSystemBackdropClick(object sender, RoutedEventArgs args)
8474
if (SystemBackdropNameText.Text == "None")
8575
{
8676
Background = new SolidColorBrush(Colors.Transparent);
87-
currentSystemBackdrop?.Dispose();
88-
currentSystemBackdrop = null;
89-
currentSystemBackdrop = new MicaBackdrop(DesktopWindowTarget, this, Program.MainForm)
90-
{
91-
Kind = MicaKind.Base,
92-
RequestedTheme = currentTheme,
93-
IsInputActive = currentInputActiveState
94-
};
95-
currentSystemBackdrop.InitializeBackdrop();
77+
Background = new MicaBrush(MicaKind.Base, this, Program.MainForm, currentInputActiveState);
9678

9779
SystemBackdropNameText.Text = "MicaBase";
9880
ThemeNameText.Text = currentTheme.ToString();
9981
InputActiveStateText.Text = currentInputActiveState.ToString();
10082
}
10183
else if (SystemBackdropNameText.Text == "MicaBase")
10284
{
103-
Background = new SolidColorBrush(Colors.Transparent);
104-
currentSystemBackdrop?.Dispose();
105-
currentSystemBackdrop = null;
106-
currentSystemBackdrop = new MicaBackdrop(DesktopWindowTarget, this, Program.MainForm)
107-
{
108-
Kind = MicaKind.BaseAlt,
109-
RequestedTheme = currentTheme,
110-
IsInputActive = currentInputActiveState
111-
};
112-
currentSystemBackdrop.InitializeBackdrop();
85+
Background = new MicaBrush(MicaKind.BaseAlt, this, Program.MainForm, currentInputActiveState);
11386

11487
SystemBackdropNameText.Text = "MicaAlt";
11588
ThemeNameText.Text = currentTheme.ToString();
11689
InputActiveStateText.Text = currentInputActiveState.ToString();
11790
}
11891
else if (SystemBackdropNameText.Text == "MicaAlt")
11992
{
120-
Background = new SolidColorBrush(Colors.Transparent);
121-
currentSystemBackdrop?.Dispose();
122-
currentSystemBackdrop = null;
123-
currentSystemBackdrop = new DesktopAcrylicBackdrop(DesktopWindowTarget, this, Program.MainForm)
124-
{
125-
Kind = DesktopAcrylicKind.Default,
126-
RequestedTheme = currentTheme,
127-
IsInputActive = currentInputActiveState,
128-
UseHostBackdropBrush = true
129-
};
130-
currentSystemBackdrop.InitializeBackdrop();
93+
Background = new DesktopAcrylicBrush(DesktopAcrylicKind.Default, this, Program.MainForm, currentInputActiveState, true);
13194

13295
SystemBackdropNameText.Text = "DesktopAcrylicDefault";
13396
ThemeNameText.Text = currentTheme.ToString();
13497
InputActiveStateText.Text = currentInputActiveState.ToString();
13598
}
13699
else if (SystemBackdropNameText.Text == "DesktopAcrylicDefault")
137100
{
138-
Background = new SolidColorBrush(Colors.Transparent);
139-
currentSystemBackdrop?.Dispose();
140-
currentSystemBackdrop = null;
141-
currentSystemBackdrop = new DesktopAcrylicBackdrop(DesktopWindowTarget, this, Program.MainForm)
142-
{
143-
Kind = DesktopAcrylicKind.Base,
144-
RequestedTheme = currentTheme,
145-
IsInputActive = currentInputActiveState,
146-
UseHostBackdropBrush = true
147-
};
148-
currentSystemBackdrop.InitializeBackdrop();
101+
Background = new DesktopAcrylicBrush(DesktopAcrylicKind.Base, this, Program.MainForm, currentInputActiveState, true);
149102

150103
SystemBackdropNameText.Text = "DesktopAcrylicBase";
151104
ThemeNameText.Text = currentTheme.ToString();
152105
InputActiveStateText.Text = currentInputActiveState.ToString();
153106
}
154107
else if (SystemBackdropNameText.Text == "DesktopAcrylicBase")
155108
{
156-
Background = new SolidColorBrush(Colors.Transparent);
157-
currentSystemBackdrop?.Dispose();
158-
currentSystemBackdrop = null;
159-
currentSystemBackdrop = new DesktopAcrylicBackdrop(DesktopWindowTarget, this, Program.MainForm)
160-
{
161-
Kind = DesktopAcrylicKind.Thin,
162-
RequestedTheme = currentTheme,
163-
IsInputActive = currentInputActiveState,
164-
UseHostBackdropBrush = true
165-
};
166-
currentSystemBackdrop.InitializeBackdrop();
109+
Background = new DesktopAcrylicBrush(DesktopAcrylicKind.Thin, this, Program.MainForm, currentInputActiveState, true);
167110

168111
SystemBackdropNameText.Text = "DesktopAcrylicThin";
169112
ThemeNameText.Text = currentTheme.ToString();
@@ -172,10 +115,7 @@ private void SwitchSystemBackdropClick(object sender, RoutedEventArgs args)
172115
else if (SystemBackdropNameText.Text == "DesktopAcrylicThin")
173116
{
174117
SystemBackdropNameText.Text = "None";
175-
currentSystemBackdrop?.Dispose();
176-
currentSystemBackdrop = null;
177118
ThemeNameText.Text = currentTheme.ToString();
178-
InputActiveStateText.Text = currentInputActiveState.ToString();
179119

180120
if (ActualTheme is ElementTheme.Light)
181121
{
@@ -195,11 +135,7 @@ private void SwitchThemeClick(object sender, RoutedEventArgs args)
195135
RequestedTheme = ElementTheme.Light;
196136
currentTheme = ElementTheme.Light;
197137
ThemeNameText.Text = ElementTheme.Light.ToString();
198-
if (currentSystemBackdrop is not null)
199-
{
200-
currentSystemBackdrop.RequestedTheme = ElementTheme.Light;
201-
}
202-
else
138+
if (SystemBackdropNameText.Text == "None")
203139
{
204140
Background = new SolidColorBrush(Color.FromArgb(255, 243, 243, 243));
205141
}
@@ -209,11 +145,7 @@ private void SwitchThemeClick(object sender, RoutedEventArgs args)
209145
RequestedTheme = ElementTheme.Dark;
210146
currentTheme = ElementTheme.Dark;
211147
ThemeNameText.Text = ElementTheme.Dark.ToString();
212-
if (currentSystemBackdrop is not null)
213-
{
214-
currentSystemBackdrop.RequestedTheme = ElementTheme.Dark;
215-
}
216-
else
148+
if (SystemBackdropNameText.Text == "None")
217149
{
218150
Background = new SolidColorBrush(Color.FromArgb(255, 32, 32, 32));
219151
}
@@ -223,20 +155,13 @@ private void SwitchThemeClick(object sender, RoutedEventArgs args)
223155
RequestedTheme = ElementTheme.Default;
224156
currentTheme = ElementTheme.Default;
225157
ThemeNameText.Text = ElementTheme.Default.ToString();
226-
if (currentSystemBackdrop is not null)
158+
if (ActualTheme is ElementTheme.Light)
227159
{
228-
currentSystemBackdrop.RequestedTheme = ElementTheme.Default;
160+
Background = new SolidColorBrush(Color.FromArgb(255, 243, 243, 243));
229161
}
230162
else
231163
{
232-
if (ActualTheme is ElementTheme.Light)
233-
{
234-
Background = new SolidColorBrush(Color.FromArgb(255, 243, 243, 243));
235-
}
236-
else
237-
{
238-
Background = new SolidColorBrush(Color.FromArgb(255, 32, 32, 32));
239-
}
164+
Background = new SolidColorBrush(Color.FromArgb(255, 32, 32, 32));
240165
}
241166
}
242167
}
@@ -247,25 +172,38 @@ private void SwitchInputActiveStateClick(object sender, RoutedEventArgs args)
247172
{
248173
currentInputActiveState = false;
249174
InputActiveStateText.Text = currentInputActiveState.ToString();
250-
if (currentSystemBackdrop is not null)
251-
{
252-
currentSystemBackdrop.IsInputActive = false;
253-
}
254175
}
255176
else
256177
{
257178
currentInputActiveState = true;
258179
InputActiveStateText.Text = currentInputActiveState.ToString();
259-
if (currentSystemBackdrop is not null)
260-
{
261-
currentSystemBackdrop.IsInputActive = true;
262-
}
180+
}
181+
182+
if (SystemBackdropNameText.Text == "Base")
183+
{
184+
Background = new MicaBrush(MicaKind.Base, this, Program.MainForm, currentInputActiveState);
185+
}
186+
else if (SystemBackdropNameText.Text == "MicaAlt")
187+
{
188+
Background = new MicaBrush(MicaKind.BaseAlt, this, Program.MainForm, currentInputActiveState);
189+
}
190+
else if (SystemBackdropNameText.Text == "DesktopAcrylicDefault")
191+
{
192+
Background = new DesktopAcrylicBrush(DesktopAcrylicKind.Default, this, Program.MainForm, currentInputActiveState, true);
193+
}
194+
else if (SystemBackdropNameText.Text == "DesktopAcrylicBase")
195+
{
196+
Background = new DesktopAcrylicBrush(DesktopAcrylicKind.Base, this, Program.MainForm, currentInputActiveState, true);
197+
}
198+
else if (SystemBackdropNameText.Text == "DesktopAcrylicThin")
199+
{
200+
Background = new DesktopAcrylicBrush(DesktopAcrylicKind.Thin, this, Program.MainForm, currentInputActiveState, true);
263201
}
264202
}
265203

266204
private void OnActualThemeChanged(FrameworkElement sender, object args)
267205
{
268-
if (currentSystemBackdrop is not null)
206+
if (SystemBackdropNameText.Text == "None")
269207
{
270208
if (ActualTheme is ElementTheme.Light)
271209
{

MileXamlControlsDemoNetCore/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public static class Program
1111
private static void Main()
1212
{
1313
Application.EnableVisualStyles();
14+
Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
1415
Application.SetCompatibleTextRenderingDefault(false);
1516

1617
App app = new();

MileXamlControlsDemoNetCore/TreeViewItemModel.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Collections.ObjectModel;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
1+
using System.Collections.ObjectModel;
72

83
namespace MileXamlControlsDemoNetCore
94
{

0 commit comments

Comments
 (0)