Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trim/AOT annotations to WinUI XAML behaviors #255

Merged
merged 11 commits into from
Jul 24, 2024
36 changes: 18 additions & 18 deletions scripts/Microsoft.Xaml.Behaviors.WinUI.Managed.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@
<copyright>&#169; Microsoft Corporation. All rights reserved.</copyright>
<tags>Behavior Action Behaviors Actions Blend Managed C# Interaction Interactivity Interactions WinUI</tags>
<dependencies>
<group targetFramework="net5.0-windows10.0.17763.0">
<dependency id="Microsoft.WindowsAppSDK" version="1.0.0" />
<group targetFramework="net8.0-windows10.0.17763.0">
<dependency id="Microsoft.WindowsAppSDK" version="1.5.240607001" />
</group>
</dependencies>
</metadata>

<files>
<file target="lib\net5.0-windows10.0.17763.0\Microsoft.Xaml.Interactivity.dll" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactivity.dll" />
<file target="lib\net5.0-windows10.0.17763.0\Microsoft.Xaml.Interactivity.pdb" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactivity.pdb" />
<file target="lib\net5.0-windows10.0.17763.0\Microsoft.Xaml.Interactivity.xml" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactivity.xml" />
<file target="lib\net5.0-windows10.0.17763.0\Microsoft.Xaml.Interactivity.pri" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactivity.pri" />
<file target="lib\net5.0-windows10.0.17763.0\Microsoft.Xaml.Interactions.dll" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactions.dll" />
<file target="lib\net5.0-windows10.0.17763.0\Microsoft.Xaml.Interactions.pdb" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactions.pdb" />
<file target="lib\net5.0-windows10.0.17763.0\Microsoft.Xaml.Interactions.xml" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactions.xml" />
<file target="lib\net5.0-windows10.0.17763.0\Microsoft.Xaml.Interactions.pri" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactions.pri" />
<file target="lib\net8.0-windows10.0.17763.0\Microsoft.Xaml.Interactivity.dll" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactivity.dll" />
<file target="lib\net8.0-windows10.0.17763.0\Microsoft.Xaml.Interactivity.pdb" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactivity.pdb" />
<file target="lib\net8.0-windows10.0.17763.0\Microsoft.Xaml.Interactivity.xml" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactivity.xml" />
<file target="lib\net8.0-windows10.0.17763.0\Microsoft.Xaml.Interactivity.pri" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactivity.pri" />
<file target="lib\net8.0-windows10.0.17763.0\Microsoft.Xaml.Interactions.dll" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactions.dll" />
<file target="lib\net8.0-windows10.0.17763.0\Microsoft.Xaml.Interactions.pdb" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactions.pdb" />
<file target="lib\net8.0-windows10.0.17763.0\Microsoft.Xaml.Interactions.xml" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactions.xml" />
<file target="lib\net8.0-windows10.0.17763.0\Microsoft.Xaml.Interactions.pri" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactions.pri" />

<!--
<file target="lib\net5.0-windows10.0.17763.0\Design\Microsoft.Xaml.Interactivity.Design.dll" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactivity.Design.dll" />
<file target="lib\net5.0-windows10.0.17763.0\Design\Microsoft.Xaml.Interactivity.Design.pdb" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactivity.Design.pdb" />
<file target="lib\net5.0-windows10.0.17763.0\Design\Microsoft.Xaml.Interactions.Design.dll" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactions.Design.dll" />
<file target="lib\net5.0-windows10.0.17763.0\Design\Microsoft.Xaml.Interactions.Design.pdb" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactions.Design.pdb" />
<file target="lib\net8.0-windows10.0.17763.0\Design\Microsoft.Xaml.Interactivity.Design.dll" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactivity.Design.dll" />
<file target="lib\net8.0-windows10.0.17763.0\Design\Microsoft.Xaml.Interactivity.Design.pdb" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactivity.Design.pdb" />
<file target="lib\net8.0-windows10.0.17763.0\Design\Microsoft.Xaml.Interactions.Design.dll" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactions.Design.dll" />
<file target="lib\net8.0-windows10.0.17763.0\Design\Microsoft.Xaml.Interactions.Design.pdb" src="..\out\WinUI\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactions.Design.pdb" />
-->

<file target="lib\net5.0-windows10.0.17763.0\Design\Microsoft.Xaml.Interactivity.DesignTools.dll" src="..\out\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactivity.DesignTools.dll" />
<file target="lib\net5.0-windows10.0.17763.0\Design\Microsoft.Xaml.Interactivity.DesignTools.pdb" src="..\out\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactivity.DesignTools.pdb" />
<file target="lib\net5.0-windows10.0.17763.0\Design\Microsoft.Xaml.Interactions.DesignTools.dll" src="..\out\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactions.DesignTools.dll" />
<file target="lib\net5.0-windows10.0.17763.0\Design\Microsoft.Xaml.Interactions.DesignTools.pdb" src="..\out\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactions.DesignTools.pdb" />
<file target="lib\net8.0-windows10.0.17763.0\Design\Microsoft.Xaml.Interactivity.DesignTools.dll" src="..\out\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactivity.DesignTools.dll" />
<file target="lib\net8.0-windows10.0.17763.0\Design\Microsoft.Xaml.Interactivity.DesignTools.pdb" src="..\out\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactivity.DesignTools.pdb" />
<file target="lib\net8.0-windows10.0.17763.0\Design\Microsoft.Xaml.Interactions.DesignTools.dll" src="..\out\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactions.DesignTools.dll" />
<file target="lib\net8.0-windows10.0.17763.0\Design\Microsoft.Xaml.Interactions.DesignTools.pdb" src="..\out\BehaviorsSDKManaged\bin\AnyCPU\Release\Microsoft.Xaml.Interactions.DesignTools.pdb" />

<file target="src\BehaviorsSDKManaged\Microsoft.Xaml.Interactions.WinUI" src="..\src\BehaviorsSDKManaged\Microsoft.Xaml.Interactions.Shared\**\*.cs" />
<file target="src\BehaviorsSDKManaged\Microsoft.Xaml.Interactivity.WinUI" src="..\src\BehaviorsSDKManaged\Microsoft.Xaml.Interactivity.Shared\**\*.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace Microsoft.Xaml.Interactions.Core
/// <summary>
/// An action that calls a method on a specified object when invoked.
/// </summary>
#if NET8_0_OR_GREATER
[RequiresUnreferencedCode("This action is not trim-safe.")]
#endif
public sealed class CallMethodAction : DependencyObject, IAction
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ namespace Microsoft.Xaml.Interactions.Core
/// <summary>
/// An action that will change a specified property to a specified value when invoked.
/// </summary>
#if NET8_0_OR_GREATER
[RequiresUnreferencedCode("This action is not trim-safe.")]
#endif
public sealed class ChangePropertyAction : DependencyObject, IAction
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

using System;
using System.Collections.Generic;
#if NET8_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using System.Reflection;

using Microsoft.Xaml.Interactivity;
Expand All @@ -29,6 +32,9 @@ internal static class DataBindingHelper
/// bindings on the action may not be up-to-date. This routine is called before the action
/// is executed in order to guarantee that all bindings are refreshed with the most current data.
/// </remarks>
#if NET8_0_OR_GREATER
[RequiresUnreferencedCode("This method accesses all fields of input action objects.")]
#endif
public static void RefreshDataBindingsOnActions(ActionCollection actions)
{
foreach (DependencyObject action in actions)
Expand All @@ -40,7 +46,11 @@ public static void RefreshDataBindingsOnActions(ActionCollection actions)
}
}

private static IEnumerable<DependencyProperty> GetDependencyProperties(Type type)
private static IEnumerable<DependencyProperty> GetDependencyProperties(
#if NET8_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
Type type)
{
List<DependencyProperty> propertyList = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace Microsoft.Xaml.Interactions.Core
/// <summary>
/// A behavior that performs actions when the bound data meets a specified condition.
/// </summary>
#if NET8_0_OR_GREATER
[RequiresUnreferencedCode("This behavior is not trim-safe.")]
#endif
public sealed class DataTriggerBehavior : Trigger
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace Microsoft.Xaml.Interactions.Core
/// <summary>
/// A behavior that listens for a specified event on its source and executes its actions when that event is fired.
/// </summary>
#if NET8_0_OR_GREATER
[RequiresUnreferencedCode("This behavior is not trim-safe.")]
#endif
public sealed class EventTriggerBehavior : Trigger
{
/// <summary>
Expand Down Expand Up @@ -181,7 +184,7 @@ private void RegisterEvent(string eventName)
else if (!this._isLoadedEventRegistered)
{
FrameworkElement element = this._resolvedSource as FrameworkElement;
if (element != null && !EventTriggerBehavior.IsElementLoaded(element))
if (element != null && !EventTriggerBehaviorHelpers.IsElementLoaded(element))
{
this._isLoadedEventRegistered = true;
element.Loaded += this.OnEvent;
Expand Down Expand Up @@ -253,7 +256,33 @@ private static void OnEventNameChanged(DependencyObject dependencyObject, Depend
behavior.RegisterEvent(newEventName);
}

internal static bool IsElementLoaded(FrameworkElement element)
#if !WinUI
private static bool IsWindowsRuntimeEvent(EventInfo eventInfo)
{
return eventInfo != null &&
EventTriggerBehavior.IsWindowsRuntimeType(eventInfo.EventHandlerType) &&
EventTriggerBehavior.IsWindowsRuntimeType(eventInfo.DeclaringType);
}

private static bool IsWindowsRuntimeType(Type type)
{
if (type != null)
{
// This will only work when using built-in WinRT interop, ie. where .winmd files are directly
// referenced instead of generated projections. That is, this would not work on modern .NET.
return type.AssemblyQualifiedName.EndsWith("ContentType=WindowsRuntime", StringComparison.Ordinal);
}

return false;
}
#endif
}

internal static class EventTriggerBehaviorHelpers
{
// This method has to be outside of 'EventTriggerBehavior', because it's actually trim-safe.
// We want to allow other callers inside the library use this without getting trim warnings.
public static bool IsElementLoaded(FrameworkElement element)
{
if (element == null)
{
Expand All @@ -280,22 +309,5 @@ internal static bool IsElementLoaded(FrameworkElement element)

return (parent != null || (rootVisual != null && element == rootVisual));
}

private static bool IsWindowsRuntimeEvent(EventInfo eventInfo)
{
return eventInfo != null &&
EventTriggerBehavior.IsWindowsRuntimeType(eventInfo.EventHandlerType) &&
EventTriggerBehavior.IsWindowsRuntimeType(eventInfo.DeclaringType);
}

private static bool IsWindowsRuntimeType(Type type)
{
if (type != null)
{
return type.AssemblyQualifiedName.EndsWith("ContentType=WindowsRuntime", StringComparison.Ordinal);
}

return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public object Execute(object sender, object parameter)
}

FrameworkElement element = sender as FrameworkElement;
if (element == null || !EventTriggerBehavior.IsElementLoaded(element))
if (element == null || !EventTriggerBehaviorHelpers.IsElementLoaded(element))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace Microsoft.Xaml.Interactions.Core

internal static class ResourceHelper
{
#if NET5_0
#if NET8_0_OR_GREATER
private static ResourceLoader strings = new ResourceLoader(ResourceLoader.GetDefaultResourceFilePath(), "Microsoft.Xaml.Interactions/Strings");
#endif

public static string GetString(string resourceName)
{
#if !NET5_0
#if !NET8_0_OR_GREATER
var strings = ResourceLoader.GetForCurrentView("Microsoft.Xaml.Interactions/Strings");
#endif
return strings.GetString(resourceName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net5.0-windows10.0.17763.0</TargetFramework>
<TargetFramework>net8.0-windows10.0.17763.0</TargetFramework>
<TargetPlatformVersion>10.0.17763.0</TargetPlatformVersion>
<RootNamespace>Microsoft.Xaml.Interactions</RootNamespace>
<Platforms>AnyCPU;x86;x64</Platforms>
<Platforms>AnyCPU;x86;x64;arm64</Platforms>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion>$(TargetPlatformMinVersion)</SupportedOSPlatformVersion>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -122,7 +124,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.0" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240607001" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.Xaml.Interactivity.WinUI\Microsoft.Xaml.Interactivity.WinUI.csproj">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private void VerifyOldCollectionIntegrity()
{
for (int i = 0; i < this.Count; i++)
{
if (this[i] != this._oldCollection[i])
if (!ReferenceEquals(this[i], this._oldCollection[i]))
{
isValid = false;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace Microsoft.Xaml.Interactivity

internal static class ResourceHelper
{
#if NET5_0
#if NET8_0_OR_GREATER
private static ResourceLoader strings = new ResourceLoader(ResourceLoader.GetDefaultResourceFilePath(), "Microsoft.Xaml.Interactivity/Strings");
#endif

public static string GetString(string resourceName)
{
#if !NET5_0
#if !NET8_0_OR_GREATER
ResourceLoader strings = ResourceLoader.GetForCurrentView("Microsoft.Xaml.Interactivity/Strings");
#endif
return strings.GetString(resourceName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net5.0-windows10.0.17763.0</TargetFramework>
<TargetFramework>net8.0-windows10.0.17763.0</TargetFramework>
<TargetPlatformVersion>10.0.17763.0</TargetPlatformVersion>
<RootNamespace>Microsoft.Xaml.Interactivity</RootNamespace>
<Platforms>AnyCPU;x86;x64</Platforms>
<Platforms>AnyCPU;x86;x64;arm64</Platforms>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion>$(TargetPlatformMinVersion)</SupportedOSPlatformVersion>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>
<PropertyGroup>
<OutputPath>..\..\..\out\WinUI\$(SolutionName)\bin\$(Platform)\$(Configuration)\</OutputPath>
Expand Down Expand Up @@ -118,7 +120,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.0" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240607001" />
</ItemGroup>
<ItemGroup>
<PRIResource Include="..\Microsoft.Xaml.Interactivity\Resources\de-DE\Strings.resw">
Expand Down
3 changes: 2 additions & 1 deletion src/BehaviorsSDKManaged/global.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"sdk":
{
"version": "5.0.403"
"version": "8.0.300",
"rollForward": "latestFeature"
},
"msbuild-sdks": {
"MSBuild.Sdk.Extras": "3.0.23"
Expand Down