Skip to content

Commit

Permalink
Merge pull request unoplatform#17920 from Youssef1313/fix-events-top-…
Browse files Browse the repository at this point in the history
…level

fix: Fix XAML generator not generating events for non-DO top-level elements
  • Loading branch information
MartinZikmund authored Aug 13, 2024
2 parents ec38914 + ec8b1fc commit b7e74d0
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,52 @@ public MainPage()

await test.RunAsync();
}

[TestMethod]
public async Task When_Event_On_TopLevel_Not_DependencyObject()
{
var xamlFile = new XamlFile(
"MainWindow.xaml",
"""
<Window x:Class="TestRepro.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Closed="Window_Closed">
</Window>
""");

var test = new Verify.Test(xamlFile)
{
TestState =
{
Sources =
{
"""
using System;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace TestRepro
{
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}
private void Window_Closed(object sender, WindowEventArgs args) { }
}
}
"""
}
},
DisableBuildReferences = true,
ReferenceAssemblies = ReferenceAssemblies.Net.Net80.AddPackages([new PackageIdentity("Uno.WinUI", "5.3.114")]),
}.AddGeneratedSources();

await test.RunAsync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// <autogenerated />
namespace MyProject
{
/// <summary>
/// Contains all the static resources defined for the application
/// </summary>
public sealed partial class GlobalStaticResources
{
static bool _initialized;
private static bool _stylesRegistered;
private static bool _dictionariesRegistered;
internal static global::Uno.UI.Xaml.XamlParseContext __ParseContext_ { get; } = new global::Uno.UI.Xaml.XamlParseContext()
{
AssemblyName = "TestProject",
}
;

static GlobalStaticResources()
{
Initialize();
}
public static void Initialize()
{
if (!_initialized)
{
_initialized = true;
global::Uno.UI.GlobalStaticResources.Initialize();
global::Uno.UI.Toolkit.GlobalStaticResources.Initialize();
global::Uno.UI.GlobalStaticResources.RegisterDefaultStyles();
global::Uno.UI.Toolkit.GlobalStaticResources.RegisterDefaultStyles();
global::Uno.UI.GlobalStaticResources.RegisterResourceDictionariesBySource();
global::Uno.UI.Toolkit.GlobalStaticResources.RegisterResourceDictionariesBySource();
}
}
public static void RegisterDefaultStyles()
{
if(!_stylesRegistered)
{
_stylesRegistered = true;
RegisterDefaultStyles_MainWindow_c93db19a7202d9eb84ddc41d72fcb89b();
}
}
// Register ResourceDictionaries using ms-appx:/// syntax, this is called for external resources
public static void RegisterResourceDictionariesBySource()
{
if(!_dictionariesRegistered)
{
_dictionariesRegistered = true;
}
}
// Register ResourceDictionaries using ms-resource:/// syntax, this is called for local resources
internal static void RegisterResourceDictionariesBySourceLocal()
{
}
static partial void RegisterDefaultStyles_MainWindow_c93db19a7202d9eb84ddc41d72fcb89b();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// <auto-generated />
[assembly: global::System.Reflection.AssemblyMetadata("UnoHasLocalizationResources", "False")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// <autogenerated />
#pragma warning disable CS0114
#pragma warning disable CS0108
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Uno.UI;
using Uno.UI.Xaml;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Documents;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.UI.Xaml.Shapes;
using Windows.UI.Text;
using Uno.Extensions;
using Uno;
using Uno.UI.Helpers;
using Uno.UI.Helpers.Xaml;
using MyProject;

#if __ANDROID__
using _View = Android.Views.View;
#elif __IOS__
using _View = UIKit.UIView;
#elif __MACOS__
using _View = AppKit.NSView;
#else
using _View = Microsoft.UI.Xaml.UIElement;
#endif

namespace TestRepro
{
partial class MainWindow : global::Microsoft.UI.Xaml.Window
{
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
private const string __baseUri_prefix_MainWindow_c93db19a7202d9eb84ddc41d72fcb89b = "ms-appx:///TestProject/";
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
private const string __baseUri_MainWindow_c93db19a7202d9eb84ddc41d72fcb89b = "ms-appx:///TestProject/";
private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope();
private void InitializeComponent()
{
var __that = this;
// Source 0\MainWindow.xaml (Line 1:2)
;

this
.GenericApply(((c0) =>
{
// Source 0\MainWindow.xaml (Line 1:2)

// WARNING Property c0.base does not exist on {http://schemas.microsoft.com/winfx/2006/xaml/presentation}Window, the namespace is http://www.w3.org/XML/1998/namespace. This error was considered irrelevant by the XamlFileGenerator
}
))
.GenericApply(((c1) =>
{
// Class TestRepro.MainWindow
var Closed_Window_Closed_That = (__that as global::Uno.UI.DataBinding.IWeakReferenceProvider).WeakReference;
/* second level */ c1.Closed += (Window_Closed_sender,Window_Closed_args) => (Closed_Window_Closed_That.Target as global::TestRepro.MainWindow)?.Window_Closed(Window_Closed_sender,Window_Closed_args);
}
))
;
if (__that.Content != null)
{
NameScope.SetNameScope(__that.Content, __nameScope);
}
OnInitializeCompleted();

}
partial void OnInitializeCompleted();
}
}
namespace MyProject
{
static class MainWindow_c93db19a7202d9eb84ddc41d72fcb89bXamlApplyExtensions
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,6 @@ private bool IsMacOSNSView(XamlType xamlType)
private bool IsDependencyObject(XamlObjectDefinition component)
=> GetType(component.Type).GetAllInterfaces().Any(i => SymbolEqualityComparer.Default.Equals(i, Generation.DependencyObjectSymbol.Value));

private bool IsDependencyObject(INamedTypeSymbol component)
=> component.GetAllInterfaces().Any(i => SymbolEqualityComparer.Default.Equals(i, Generation.DependencyObjectSymbol.Value));

private bool IsUIElement(INamedTypeSymbol? symbol)
=> IsType(symbol, Generation.UIElementSymbol.Value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,10 +782,7 @@ private void BuildGenericControlInitializerBody(IndentedStringBuilder writer, Xa
BuildLiteralProperties(blockWriter, topLevelControl, closure);
}

if (IsDependencyObject(topLevelControlType))
{
BuildExtendedProperties(writer, topLevelControl, useGenericApply: true);
}
BuildExtendedProperties(writer, topLevelControl, useGenericApply: true);
}

writer.AppendLineIndented(";");
Expand Down

0 comments on commit b7e74d0

Please sign in to comment.