Skip to content

Commit dbba74a

Browse files
ghost1372niels9001Zakariathr22marcelwgn
authored
Add NativeAOT Support (#1916)
**This PR adds native AOT support #1627 .** - [x] Using x:Bind instead of Binding - [x] Using CsWin32 with Disabled Marshaling (NativeMethods) - [x] Mark Classes as partial - [x] Remove Reflections - [x] BindingPage (NonExistentProperty Removed because xBind does not support it) - [ ] Enforce AOT warnings as errors (Seperate PR) > [!Note] > - The app is using reflection and DllImport somewhere, but I didn’t see any trimming or AOT warnings, and I tested it locally without any issues—so I didn’t modify that code. > - There’s a warning related to `FreeLibrarySafeHandle`, which comes from CsWin32. However, according to this [answer](microsoft/CsWin32#1438 (comment)), it's safe to ignore for now as long as you're not passing these handles across the WinRT ABI. # Notes for Future Developers / Contributors ### Please read carefully before modifying the codebase: > [!Warning] > - Avoid using reflection whenever possible. It can cause issues with trimming and Native AOT. > - Enum bindings: Use the recommended pattern. > Instead of: > `private IReadOnlyList<TitleBarTheme> titleBarThemes = Enum.GetValues(typeof(TitleBarTheme)).Cast<TitleBarTheme>().ToList();` > Use: > `private IReadOnlyList<TitleBarTheme> titleBarThemes { get; set; } = new List<TitleBarTheme>(Enum.GetValues<TitleBarTheme>());` > - Avoid using P/Invoke directly. Use [CsWin32](https://github.com/microsoft/cswin32) and define your APIs in the NativeMethods.txt file. > - Mark classes as partial. > - Always provide a DataTemplate with x:DataType and avoid using `SelectedValuePath`, `DisplayMemberPath`. > - Avoid binding directly to IEnumerable. Always call `.ToList()` to avoid runtime exceptions. --------- Co-authored-by: Niels Laute <niels.laute@live.nl> Co-authored-by: Zakaria Tahri <zakotahri@outlook.com> Co-authored-by: Marcel Wagner <marcelwagner@microsoft.com> Co-authored-by: Marcel W. <marcel.alex.wagner@outlook.com>
1 parent 4acbf11 commit dbba74a

File tree

84 files changed

+549
-498
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+549
-498
lines changed

WinUIGallery/App.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@
6969
x:Key="inverseNullToVisibilityConverter"
7070
NonNullValue="Collapsed"
7171
NullValue="Visible" />
72-
<converters:BooleanToValueConverter x:Key="booleanToValueConverter" />
7372
<converters:DoubleToThicknessConverter x:Key="doubleToThicknessConverter" />
73+
<converters:BrushToColorConverter x:Key="brushToColorConverter" />
7474
</ResourceDictionary>
7575
</Application.Resources>
7676
</Application>

WinUIGallery/App.xaml.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
using Windows.ApplicationModel.Activation;
1414
using WinUIGallery.Helpers;
1515
using WinUIGallery.Pages;
16-
using static WinUIGallery.Helpers.Win32;
16+
using static WinUIGallery.Helpers.NativeMethods;
1717

1818
namespace WinUIGallery;
1919

@@ -23,8 +23,6 @@ namespace WinUIGallery;
2323
sealed partial class App : Application
2424
{
2525
internal static MainWindow MainWindow { get; private set; } = null!;
26-
private static int registeredKeyPressedHook = 0;
27-
private HookProc keyEventHook;
2826

2927
/// <summary>
3028
/// Initializes the singleton Application object. This is the first line of authored code
@@ -55,8 +53,7 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar
5553
}
5654
#endif
5755

58-
keyEventHook = KeyEventHook;
59-
registeredKeyPressedHook = SetWindowKeyHook(keyEventHook);
56+
SetWindowKeyHook();
6057

6158
EnsureWindow();
6259

@@ -66,16 +63,6 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar
6663
};
6764
}
6865

69-
private int KeyEventHook(int nCode, IntPtr wParam, IntPtr lParam)
70-
{
71-
if (nCode >= 0 && IsKeyDownHook(lParam))
72-
{
73-
RootFrameNavigationHelper.RaiseKeyPressed((uint)wParam);
74-
}
75-
76-
return CallNextHookEx(registeredKeyPressedHook, nCode, wParam, lParam);
77-
}
78-
7966
private void DebugSettings_BindingFailed(object sender, BindingFailedEventArgs e)
8067
{
8168
// Ignore the exception from NonExistentProperty in BindingPage.xaml,

WinUIGallery/Assets/AnimatedVisuals/LottieLogo1.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515
namespace AnimatedVisuals;
1616

17-
sealed class LottieLogo1 : IAnimatedVisualSource
17+
sealed partial class LottieLogo1 : IAnimatedVisualSource
1818
{
1919
public IAnimatedVisual TryCreateAnimatedVisual(Compositor compositor, out object diagnostics)
2020
{
2121
diagnostics = null;
2222
return new AnimatedVisual(compositor);
2323
}
2424

25-
sealed class AnimatedVisual : IAnimatedVisual
25+
sealed partial class AnimatedVisual : IAnimatedVisual
2626
{
2727
const long c_durationTicks = 59670000;
2828
readonly Compositor _c;

WinUIGallery/Controls/CopyButton.xaml

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,27 @@
66
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
77
xmlns:local="using:WinUIGallery.Controls">
88

9+
<ResourceDictionary.ThemeDictionaries>
10+
<ResourceDictionary x:Key="Dark">
11+
<StaticResource x:Key="CopyButtonPointerOverForegroundBrush" ResourceKey="ButtonForegroundPointerOver" />
12+
<StaticResource x:Key="CopyButtonPressedForegroundBrush" ResourceKey="ButtonForegroundPressed" />
13+
</ResourceDictionary>
14+
<ResourceDictionary x:Key="Light">
15+
<StaticResource x:Key="CopyButtonPointerOverForegroundBrush" ResourceKey="ButtonForegroundPointerOver" />
16+
<StaticResource x:Key="CopyButtonPressedForegroundBrush" ResourceKey="ButtonForegroundPressed" />
17+
</ResourceDictionary>
18+
<ResourceDictionary x:Key="HighContrast">
19+
<StaticResource x:Key="CopyButtonPointerOverForegroundBrush" ResourceKey="ButtonForegroundPointerOver" />
20+
<StaticResource x:Key="CopyButtonPressedForegroundBrush" ResourceKey="ButtonForegroundPressed" />
21+
</ResourceDictionary>
22+
</ResourceDictionary.ThemeDictionaries>
23+
924
<Style BasedOn="{StaticResource DefaultCopyButtonStyle}" TargetType="local:CopyButton" />
1025

1126
<Style x:Key="DefaultCopyButtonStyle" TargetType="local:CopyButton">
1227
<Setter Property="Background" Value="{ThemeResource ButtonBackground}" />
1328
<Setter Property="BackgroundSizing" Value="InnerBorderEdge" />
14-
<Setter Property="Foreground" Value="{x:Null}" />
29+
<Setter Property="Foreground" Value="{ThemeResource ButtonForeground}" />
1530
<Setter Property="BorderBrush" Value="{ThemeResource ButtonBorderBrush}" />
1631
<Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}" />
1732
<Setter Property="Padding" Value="6" />
@@ -138,15 +153,15 @@
138153
ContentTransitions="{TemplateBinding ContentTransitions}"
139154
FontFamily="{TemplateBinding FontFamily}"
140155
FontSize="{TemplateBinding FontSize}"
141-
Foreground="{Binding Foreground, RelativeSource={RelativeSource Mode=TemplatedParent}, TargetNullValue={ThemeResource ButtonForeground}}"
156+
Foreground="{TemplateBinding Foreground}"
142157
RenderTransformOrigin=".5,.5">
143158
<ContentPresenter.RenderTransform>
144159
<ScaleTransform x:Name="CopyToClipboardTextScaleTransform" />
145160
</ContentPresenter.RenderTransform>
146161
</ContentPresenter>
147162
<ContentPresenter
148163
x:Name="CopySuccessGlyph"
149-
Foreground="{Binding Foreground, RelativeSource={RelativeSource Mode=TemplatedParent}, TargetNullValue={ThemeResource ButtonForeground}}"
164+
Foreground="{TemplateBinding Foreground}"
150165
Opacity="0"
151166
RenderTransformOrigin=".5,.5">
152167
<ContentPresenter.RenderTransform>
@@ -167,10 +182,10 @@
167182
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushPointerOver}" />
168183
</ObjectAnimationUsingKeyFrames>
169184
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
170-
<DiscreteObjectKeyFrame KeyTime="0" Value="{Binding Foreground, RelativeSource={RelativeSource Mode=TemplatedParent}, TargetNullValue={ThemeResource ButtonForegroundPointerOver}}" />
185+
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CopyButtonPointerOverForegroundBrush}" />
171186
</ObjectAnimationUsingKeyFrames>
172187
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="CopySuccessGlyph" Storyboard.TargetProperty="Foreground">
173-
<DiscreteObjectKeyFrame KeyTime="0" Value="{Binding Foreground, RelativeSource={RelativeSource Mode=TemplatedParent}, TargetNullValue={ThemeResource ButtonForegroundPointerOver}}" />
188+
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CopyButtonPointerOverForegroundBrush}" />
174189
</ObjectAnimationUsingKeyFrames>
175190
</Storyboard>
176191
<VisualState.Setters>
@@ -187,10 +202,10 @@
187202
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushPressed}" />
188203
</ObjectAnimationUsingKeyFrames>
189204
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
190-
<DiscreteObjectKeyFrame KeyTime="0" Value="{Binding Foreground, RelativeSource={RelativeSource Mode=TemplatedParent}, TargetNullValue={ThemeResource ButtonForegroundPressed}}" />
205+
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CopyButtonPressedForegroundBrush}" />
191206
</ObjectAnimationUsingKeyFrames>
192207
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="CopySuccessGlyph" Storyboard.TargetProperty="Foreground">
193-
<DiscreteObjectKeyFrame KeyTime="0" Value="{Binding Foreground, RelativeSource={RelativeSource Mode=TemplatedParent}, TargetNullValue={ThemeResource ButtonForegroundPressed}}" />
208+
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource CopyButtonPressedForegroundBrush}" />
194209
</ObjectAnimationUsingKeyFrames>
195210
</Storyboard>
196211
<VisualState.Setters>

WinUIGallery/Controls/CopyButton.xaml.cs

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

99
namespace WinUIGallery.Controls;
1010

11-
public sealed class CopyButton : Button
11+
public sealed partial class CopyButton : Button
1212
{
1313
public CopyButton()
1414
{

WinUIGallery/Controls/DesignGuidance/ColorTile.xaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@
5252
Click="CopyBrushNameButton_Click"
5353
Content="&#xE8C8;"
5454
Foreground="{x:Bind Foreground, Mode=OneWay}"
55-
ToolTipService.ToolTip="Copy brush name" />
55+
ToolTipService.ToolTip="Copy brush name">
56+
<local:CopyButton.Resources>
57+
<SolidColorBrush x:Key="CopyButtonPointerOverForegroundBrush" Color="{x:Bind Foreground, Mode=OneWay, Converter={StaticResource brushToColorConverter}}" />
58+
<SolidColorBrush x:Key="CopyButtonPressedForegroundBrush" Color="{x:Bind Foreground, Mode=OneWay, Converter={StaticResource brushToColorConverter}}" />
59+
</local:CopyButton.Resources>
60+
</local:CopyButton>
5661

5762
<TextBlock
5863
Grid.Row="1"

WinUIGallery/Controls/PageHeader.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@
121121
<HyperlinkButton
122122
HorizontalAlignment="Stretch"
123123
HorizontalContentAlignment="Left"
124-
NavigateUri="{Binding Uri}"
125-
ToolTipService.ToolTip="{Binding Uri}">
124+
NavigateUri="{x:Bind Uri}"
125+
ToolTipService.ToolTip="{x:Bind Uri}">
126126
<TextBlock Text="{x:Bind Title}" />
127127
</HyperlinkButton>
128128
</DataTemplate>

WinUIGallery/Controls/SampleCodePresenter.xaml.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using System.Collections.Generic;
1313
using System.IO;
1414
using System.Linq;
15-
using System.Reflection;
1615
using System.Text.RegularExpressions;
1716
using Windows.ApplicationModel.DataTransfer;
1817
using Windows.Storage;
@@ -169,10 +168,12 @@ private async void FormatAndRenderSampleFromFile(string sourceRelativePath, Cont
169168
{
170169
string sampleString = null;
171170
StorageFile file = null;
172-
if (!NativeHelper.IsAppPackaged)
171+
if (!NativeMethods.IsAppPackaged)
173172
{
174173
var relativePath = GetDerivedSourceUnpackaged(sourceRelativePath);
175-
var sourcePath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), relativePath));
174+
175+
var sourcePath = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, relativePath));
176+
176177
file = await StorageFile.GetFileFromPathAsync(sourcePath);
177178
}
178179
else

WinUIGallery/Converters/BooleanToInvertedVisibilityConverter.cs

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

88
namespace WinUIGallery.Converters;
99

10-
public sealed class BooleanToInvertedVisibilityConverter : IValueConverter
10+
public sealed partial class BooleanToInvertedVisibilityConverter : IValueConverter
1111
{
1212
public object Convert(object value, Type targetType, object parameter, string language)
1313
{

WinUIGallery/Converters/BooleanToValueConverter.cs renamed to WinUIGallery/Converters/BrushToColorConverter.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using Microsoft.UI;
45
using Microsoft.UI.Xaml.Data;
6+
using Microsoft.UI.Xaml.Media;
57
using System;
68

79
namespace WinUIGallery.Converters;
8-
9-
public sealed class BooleanToValueConverter : IValueConverter
10+
public partial class BrushToColorConverter : IValueConverter
1011
{
1112
public object Convert(object value, Type targetType, object parameter, string language)
1213
{
13-
return (bool)value ? parameter : null;
14+
if (value is SolidColorBrush solidColorBrush)
15+
{
16+
return solidColorBrush.Color;
17+
}
18+
19+
return Colors.Transparent;
1420
}
1521

1622
public object ConvertBack(object value, Type targetType, object parameter, string language)

0 commit comments

Comments
 (0)