Skip to content

Commit 2732b61

Browse files
committed
fix: Fix type resolution from x xmlns namespace
1 parent 34c5837 commit 2732b61

File tree

5 files changed

+233
-12
lines changed

5 files changed

+233
-12
lines changed

src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Given_AttachedPropertyWithNoFuzzyMatching.cs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ namespace Uno.UI.SourceGenerators.Tests.XamlCodeGeneratorTests;
77
using Verify = XamlSourceGeneratorVerifier;
88

99
[TestClass]
10-
public class Given_AttachedPropertyWithNoFuzzyMatching
10+
public class Given_NoFuzzyMatching
1111
{
1212
[TestMethod]
13-
public async Task TestVisibleBoundsPadding()
13+
public async Task TestAttachedPropertyVisibleBoundsPadding()
1414
{
1515
var xamlFile = new XamlFile("MainPage.xaml", """
1616
<Page x:Class="TestRepro.MainPage"
@@ -58,4 +58,57 @@ public MainPage()
5858

5959
await test.RunAsync();
6060
}
61+
62+
[TestMethod]
63+
public async Task TestTypePropertyWithXString()
64+
{
65+
var xamlFile = new XamlFile("MainPage.xaml", """
66+
<Page x:Class="TestRepro.MainPage"
67+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
68+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
69+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
70+
xmlns:local="using:TestRepro"
71+
xmlns:toolkit="using:Uno.UI.Toolkit"
72+
mc:Ignorable="android ios">
73+
74+
<local:MyButton MyProperty="x:String" />
75+
76+
</Page>
77+
""");
78+
79+
var test = new Verify.Test(xamlFile)
80+
{
81+
TestState =
82+
{
83+
Sources =
84+
{
85+
"""
86+
using System;
87+
using Microsoft.UI.Xaml;
88+
using Microsoft.UI.Xaml.Controls;
89+
90+
namespace TestRepro
91+
{
92+
public sealed partial class MyButton : Button
93+
{
94+
public Type MyProperty { get; set; }
95+
}
96+
97+
public sealed partial class MainPage : Page
98+
{
99+
public MainPage()
100+
{
101+
this.InitializeComponent();
102+
}
103+
}
104+
}
105+
"""
106+
}
107+
},
108+
ReferenceAssemblies = ReferenceAssemblies.Net.Net70.AddPackages(ImmutableArray.Create(new PackageIdentity("Uno.WinUI", "5.0.118"))),
109+
DisableBuildReferences = true,
110+
}.AddGeneratedSources();
111+
112+
await test.RunAsync();
113+
}
61114
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// <autogenerated />
2+
#if __WASM__
3+
#error invalid internal source generator state. The __WASM__ DefineConstant was not propagated properly.
4+
#endif
5+
namespace MyProject
6+
{
7+
/// <summary>
8+
/// Contains all the static resources defined for the application
9+
/// </summary>
10+
public sealed partial class GlobalStaticResources
11+
{
12+
static bool _initialized;
13+
private static bool _stylesRegistered;
14+
private static bool _dictionariesRegistered;
15+
internal static global::Uno.UI.Xaml.XamlParseContext __ParseContext_ {get; } = new global::Uno.UI.Xaml.XamlParseContext()
16+
{
17+
AssemblyName = "TestProject",
18+
}
19+
;
20+
21+
static GlobalStaticResources()
22+
{
23+
Initialize();
24+
}
25+
public static void Initialize()
26+
{
27+
if (!_initialized)
28+
{
29+
_initialized = true;
30+
global::Uno.UI.GlobalStaticResources.Initialize();
31+
global::Uno.UI.Toolkit.GlobalStaticResources.Initialize();
32+
global::Uno.UI.GlobalStaticResources.RegisterDefaultStyles();
33+
global::Uno.UI.Toolkit.GlobalStaticResources.RegisterDefaultStyles();
34+
global::Uno.UI.GlobalStaticResources.RegisterResourceDictionariesBySource();
35+
global::Uno.UI.Toolkit.GlobalStaticResources.RegisterResourceDictionariesBySource();
36+
}
37+
}
38+
public static void RegisterDefaultStyles()
39+
{
40+
if(!_stylesRegistered)
41+
{
42+
_stylesRegistered = true;
43+
RegisterDefaultStyles_MainPage_d6cd66944958ced0c513e0a04797b51d();
44+
}
45+
}
46+
// Register ResourceDictionaries using ms-appx:/// syntax, this is called for external resources
47+
public static void RegisterResourceDictionariesBySource()
48+
{
49+
if(!_dictionariesRegistered)
50+
{
51+
_dictionariesRegistered = true;
52+
}
53+
}
54+
// Register ResourceDictionaries using ms-resource:/// syntax, this is called for local resources
55+
internal static void RegisterResourceDictionariesBySourceLocal()
56+
{
57+
}
58+
static partial void RegisterDefaultStyles_MainPage_d6cd66944958ced0c513e0a04797b51d();
59+
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// <auto-generated />
2+
[assembly: global::System.Reflection.AssemblyMetadata("UnoHasLocalizationResources", "False")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// <autogenerated />
2+
#pragma warning disable CS0114
3+
#pragma warning disable CS0108
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Diagnostics;
7+
using System.Linq;
8+
using Uno.UI;
9+
using Uno.UI.Xaml;
10+
using Microsoft.UI.Xaml;
11+
using Microsoft.UI.Xaml.Controls;
12+
using Microsoft.UI.Xaml.Controls.Primitives;
13+
using Microsoft.UI.Xaml.Data;
14+
using Microsoft.UI.Xaml.Documents;
15+
using Microsoft.UI.Xaml.Media;
16+
using Microsoft.UI.Xaml.Media.Animation;
17+
using Microsoft.UI.Xaml.Shapes;
18+
using Windows.UI.Text;
19+
using Uno.Extensions;
20+
using Uno;
21+
using Uno.UI.Helpers;
22+
using Uno.UI.Helpers.Xaml;
23+
using MyProject;
24+
25+
#if __ANDROID__
26+
using _View = Android.Views.View;
27+
#elif __IOS__
28+
using _View = UIKit.UIView;
29+
#elif __MACOS__
30+
using _View = AppKit.NSView;
31+
#else
32+
using _View = Microsoft.UI.Xaml.UIElement;
33+
#endif
34+
35+
namespace TestRepro
36+
{
37+
partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page
38+
{
39+
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
40+
private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/";
41+
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
42+
private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/";
43+
private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope();
44+
private void InitializeComponent()
45+
{
46+
NameScope.SetNameScope(this, __nameScope);
47+
var __that = this;
48+
base.IsParsing = true;
49+
// Source 0\MainPage.xaml (Line 1:2)
50+
base.Content =
51+
new global::TestRepro.MyButton
52+
{
53+
IsParsing = true,
54+
MyProperty = typeof(string),
55+
// Source 0\MainPage.xaml (Line 9:3)
56+
}
57+
.MainPage_d6cd66944958ced0c513e0a04797b51d_XamlApply((MainPage_d6cd66944958ced0c513e0a04797b51dXamlApplyExtensions.XamlApplyHandler0)(c0 =>
58+
{
59+
global::Uno.UI.FrameworkElementHelper.SetBaseUri(c0, __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d);
60+
c0.CreationComplete();
61+
}
62+
))
63+
;
64+
65+
this
66+
.GenericApply(((c1) =>
67+
{
68+
// Source 0\MainPage.xaml (Line 1:2)
69+
70+
// WARNING Property c1.base does not exist on {http://schemas.microsoft.com/winfx/2006/xaml/presentation}Page, the namespace is http://www.w3.org/XML/1998/namespace. This error was considered irrelevant by the XamlFileGenerator
71+
}
72+
))
73+
.GenericApply(((c2) =>
74+
{
75+
// Class TestRepro.MainPage
76+
global::Uno.UI.FrameworkElementHelper.SetBaseUri(c2, __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d);
77+
c2.CreationComplete();
78+
}
79+
))
80+
;
81+
OnInitializeCompleted();
82+
83+
}
84+
partial void OnInitializeCompleted();
85+
}
86+
}
87+
namespace MyProject
88+
{
89+
static class MainPage_d6cd66944958ced0c513e0a04797b51dXamlApplyExtensions
90+
{
91+
public delegate void XamlApplyHandler0(global::TestRepro.MyButton instance);
92+
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
93+
public static global::TestRepro.MyButton MainPage_d6cd66944958ced0c513e0a04797b51d_XamlApply(this global::TestRepro.MyButton instance, XamlApplyHandler0 handler)
94+
{
95+
handler(instance);
96+
return instance;
97+
}
98+
}
99+
}

src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.Reflection.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -514,23 +514,29 @@ private static bool HasInitializer(XamlObjectDefinition objectDefinition)
514514
return null;
515515
}
516516

517-
private INamedTypeSymbol? SearchClrNamespaces(string name)
517+
private INamedTypeSymbol? SearchNamespaces(string name, string[] namespaces)
518518
{
519-
if (_clrNamespaces != null)
519+
foreach (var @namespace in namespaces)
520520
{
521-
// Search first using the default namespace
522-
foreach (var clrNamespace in _clrNamespaces)
521+
if (_metadataHelper.FindTypeByFullName(@namespace + "." + name) is INamedTypeSymbol type)
523522
{
524-
if (_metadataHelper.FindTypeByFullName(clrNamespace + "." + name) is INamedTypeSymbol type)
525-
{
526-
return type;
527-
}
523+
return type;
528524
}
529525
}
530526

531527
return null;
532528
}
533529

530+
private INamedTypeSymbol? SearchClrNamespaces(string name)
531+
{
532+
if (_clrNamespaces != null)
533+
{
534+
return SearchNamespaces(name, _clrNamespaces);
535+
}
536+
537+
return null;
538+
}
539+
534540
private INamedTypeSymbol GetType(string name, XamlObjectDefinition? objectDefinition = null)
535541
{
536542
while (objectDefinition is not null)
@@ -604,9 +610,9 @@ private INamedTypeSymbol GetType(XamlType type)
604610
namespaceUrl = ns.Namespace.Substring(0, indexOfQuestionMark);
605611
}
606612

607-
if (namespaceUrl.Equals("http://schemas.microsoft.com/winfx/2006/xaml/presentation", StringComparison.Ordinal))
613+
if (_knownNamespaces.TryGetValue(namespaceUrl, out var knownNamespaces))
608614
{
609-
return SearchClrNamespaces(fields[1]);
615+
return SearchNamespaces(fields[1], knownNamespaces);
610616
}
611617

612618
var nsName = GetTrimmedNamespace(namespaceUrl);

0 commit comments

Comments
 (0)