[X] resilient to unyped x:Name (#5552)

Allow the previewer to recover from an x:Name attribute on a element
with unresolved type.

- fixes #5546
- closes #5547
This commit is contained in:
Stephane Delcroix 2019-03-15 01:42:54 +01:00 коммит произвёл Samantha Houts
Родитель 6ed70b00bd
Коммит 5c07fa9b43
2 изменённых файлов: 37 добавлений и 7 удалений

Просмотреть файл

@ -627,6 +627,25 @@ namespace Xamarin.Forms.Xaml.UnitTests
Assert.That(clrNamespace, Is.EqualTo("my.namespace"));
Assert.That(typeName, Is.EqualTo("MissingType"));
}
[Test]
public void IgnoreNamedMissingTypeException()
{
var xaml = @"
<ContentPage xmlns=""http://xamarin.com/schemas/2014/forms""
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
xmlns:local=""clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests"">
<StackLayout>
<local:Missing x:Name=""MyName"" />
<Button x:Name=""button"" />
<Button x:Name=""button"" />
</StackLayout>
</ContentPage>";
var exceptions = new List<Exception>();
Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add;
Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true));
Assert.That(exceptions.Count, Is.GreaterThan(1));
}
}
public class InstantiateThrows

Просмотреть файл

@ -30,18 +30,29 @@ namespace Xamarin.Forms.Xaml
{
if (!IsXNameProperty(node, parentNode))
return;
try
{
try {
((IElementNode)parentNode).Namescope.RegisterName((string)node.Value, Values[parentNode]);
}
catch (ArgumentException ae)
{
catch (ArgumentException ae) {
if (ae.ParamName != "name")
throw ae;
throw new XamlParseException($"An element with the name \"{(string)node.Value}\" already exists in this NameScope", node);
var xpe = new XamlParseException($"An element with the name \"{(string)node.Value}\" already exists in this NameScope", node);
if (Context.ExceptionHandler != null) {
Context.ExceptionHandler(xpe);
return;
}
throw xpe;
}
var element = Values[parentNode] as Element;
if (element != null)
catch (KeyNotFoundException knfe) {
if (Context.ExceptionHandler != null) {
Context.ExceptionHandler(knfe);
return;
}
throw knfe;
}
if (Values[parentNode] is Element element)
element.StyleId = element.StyleId ?? (string)node.Value;
}