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

[net7.0] Corelib Fixes #15457

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Controls/src/Build.Tasks/XamlCAssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public override AssemblyDefinition Resolve(AssemblyNameReference name)
return assembly;
if (IsMscorlib(name)
&& (TryResolve(AssemblyNameReference.Parse("mscorlib"), out assembly)
|| TryResolve(AssemblyNameReference.Parse("System.Private.CoreLib"), out assembly)
|| TryResolve(AssemblyNameReference.Parse("netstandard"), out assembly)
|| TryResolve(AssemblyNameReference.Parse("System.Runtime"), out assembly)))
return assembly;
Expand All @@ -42,6 +43,7 @@ bool TryResolve(AssemblyNameReference assemblyNameReference, out AssemblyDefinit
static bool IsMscorlib(AssemblyNameReference name)
{
return name.Name == "mscorlib"
|| name.Name == "System.Private.CoreLib"
|| name.Name == "System.Runtime"
|| name.Name == "netstandard";
}
Expand Down
10 changes: 10 additions & 0 deletions src/Controls/src/Xaml/XmlTypeXamlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,19 @@ static class XmlTypeXamlExtensions

var potentialTypes = new List<(string typeName, string clrNamespace, string assemblyName)>();
foreach (string typeName in lookupNames)
{
foreach (XmlnsDefinitionAttribute xmlnsDefinitionAttribute in lookupAssemblies)
{
potentialTypes.Add(new(typeName, xmlnsDefinitionAttribute.ClrNamespace, xmlnsDefinitionAttribute.AssemblyName));

// As a fallback, for assembly=mscorlib try assembly=System.Private.CoreLib
if (xmlnsDefinitionAttribute.AssemblyName == "mscorlib" || xmlnsDefinitionAttribute.AssemblyName.StartsWith("mscorlib,", StringComparison.Ordinal))
{
potentialTypes.Add(new(typeName, xmlnsDefinitionAttribute.ClrNamespace, "System.Private.CoreLib"));
}
}
}

T? type = null;
foreach (var typeInfo in potentialTypes)
if ((type = refFromTypeInfo(typeInfo)) != null)
Expand Down
23 changes: 23 additions & 0 deletions src/Controls/tests/DeviceTests/Xaml/RadioButtonUsing.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="Microsoft.Maui.DeviceTests.RadioButtonUsing">
<ContentPage.Resources>
<ResourceDictionary>
<x:Array Type="{x:Type x:String}" x:Key="MyArray">
<x:String>Foo</x:String>
<x:String>Bar</x:String>
<x:String>Baz</x:String>
</x:Array>
<x:Double x:Key="MyNumber">42</x:Double>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<RadioButton>
<RadioButton.Value>
<sys:Int32>1</sys:Int32>
</RadioButton.Value>
</RadioButton>
</ContentPage.Content>
</ContentPage>
14 changes: 14 additions & 0 deletions src/Controls/tests/DeviceTests/Xaml/RadioButtonUsing.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Microsoft.Maui.DeviceTests
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class RadioButtonUsing : ContentPage
{
public RadioButtonUsing()
{
InitializeComponent();
}
}
}
98 changes: 98 additions & 0 deletions src/Controls/tests/DeviceTests/Xaml/XamlTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
public class XamlTests
{
[Fact("Parsed XAML can use mscorlib")]
public void Namespace_mscorlib_Parsed()
{
var page = new ContentPage();
page.LoadFromXaml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<RadioButton>
<RadioButton.Value>
<sys:Int32>1</sys:Int32>
</RadioButton.Value>
</RadioButton>
</ContentPage>
""");
Assert.IsType<RadioButton>(page.Content);
Assert.Equal(1, ((RadioButton)page.Content).Value);
}

[Fact("Compiled XAML can use mscorlib")]
public void Namespace_mscorlib_Compiled()
{
var page = new RadioButtonUsing();
Assert.IsType<RadioButton>(page.Content);
Assert.Equal(1, ((RadioButton)page.Content).Value);
}

[Fact("Parsed XAML can use x:Array")]
public void x_Array_Parsed()
{
var page = new ContentPage();
page.LoadFromXaml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<ContentPage.Resources>
<ResourceDictionary>
<x:Array Type="{x:Type x:String}" x:Key="MyArray">
<x:String>Foo</x:String>
<x:String>Bar</x:String>
<x:String>Baz</x:String>
</x:Array>
</ResourceDictionary>
</ContentPage.Resources>
</ContentPage>
""");
string[] array = page.Resources["MyArray"] as string[];
Assert.Equal(new[] { "Foo", "Bar", "Baz" }, array);
}

[Fact("Compiled XAML can use x:Array")]
public void x_Array_Compiled()
{
var page = new RadioButtonUsing();
string[] array = page.Resources["MyArray"] as string[];
Assert.Equal(new[] { "Foo", "Bar", "Baz" }, array);
}

[Fact("Parsed XAML can use x:Double")]
public void x_Double_Parsed()
{
var page = new ContentPage();
page.LoadFromXaml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<ContentPage.Resources>
<ResourceDictionary>
<x:Double x:Key="MyNumber">42</x:Double>
</ResourceDictionary>
</ContentPage.Resources>
</ContentPage>
""");
Assert.Equal(42d, page.Resources["MyNumber"]);
}

[Fact("Compiled XAML can use x:Double")]
public void x_Double_Compiled()
{
var page = new RadioButtonUsing();
Assert.Equal(42d, page.Resources["MyNumber"]);
}
}
}
15 changes: 14 additions & 1 deletion src/Controls/tests/Xaml.UnitTests/TestXmlnsUsing.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="using:Microsoft.Maui.Controls.Xaml.UnitTests"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.TestXmlnsUsing">
<ContentPage.Content>
<local:CustomXamlView />
<VerticalStackLayout>
<local:CustomXamlView x:Name="CustomView" />
<RadioButton x:Name="Radio1">
<RadioButton.Value>
<sys:Int32>1</sys:Int32>
</RadioButton.Value>
</RadioButton>
<RadioButton x:Name="Radio2">
<RadioButton.Value>
<sys:Int32>2</sys:Int32>
</RadioButton.Value>
</RadioButton>
</VerticalStackLayout>
</ContentPage.Content>
</ContentPage>
4 changes: 3 additions & 1 deletion src/Controls/tests/Xaml.UnitTests/TestXmlnsUsing.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public void SupportUsingXmlns(bool useCompiledXaml)
{
var page = new TestXmlnsUsing(useCompiledXaml);
Assert.That(page.Content, Is.Not.Null);
Assert.That(page.Content, Is.TypeOf<CustomXamlView>());
Assert.That(page.CustomView, Is.TypeOf<CustomXamlView>());
Assert.That(page.Radio1.Value, Is.EqualTo(1));
Assert.That(page.Radio2.Value, Is.EqualTo(2));
}
}
}
Expand Down