public bool UseDesignProperties { get; set; }
}
- internal static Action<Exception> ExceptionHandler { get; set; }
+ static Action<Exception> exceptionHandler1;
+
+ [Obsolete("2 is better than 1")]
+ internal static Action<Exception> ExceptionHandler {
+ get => exceptionHandler1;
+ set {
+ exceptionHandler1 = value;
+ ExceptionHandler2 = value != null ? ((Exception exception, string filepath) err) => exceptionHandler1(err.exception) : (Action<(Exception, string)>)null;
+ }
+ }
+
+ internal static Action<(Exception exception, string filepath)> ExceptionHandler2 { get; set; }
}
-}
\ No newline at end of file
+}
try {
return (T)element.FindByName(name);
}
- catch (InvalidCastException ice) when (ResourceLoader.ExceptionHandler != null) {
- ResourceLoader.ExceptionHandler(ice);
+ catch (InvalidCastException ice) when (ResourceLoader.ExceptionHandler2 != null) {
+ ResourceLoader.ExceptionHandler2((ice, null));
return default(T);
}
}
XamlLoader.FallbackTypeResolver = null;
XamlLoader.ValueCreatedCallback = null;
XamlLoader.InstantiationFailedCallback = null;
- Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = null;
#pragma warning disable 0618
+ Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = null;
Xamarin.Forms.Xaml.Internals.XamlLoader.DoNotThrowOnExceptions = false;
#pragma warning restore 0618
}
</ContentPage>";
var exceptions = new List<Exception>();
+#pragma warning disable CS0618 // Type or member is obsolete
Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add;
+#pragma warning restore CS0618 // Type or member is obsolete
var page = (ContentPage)XamlLoader.Create(xaml, true);
Assert.That(page.Content, Is.TypeOf<Button>());
</ContentPage>";
var exceptions = new List<Exception>();
+#pragma warning disable CS0618 // Type or member is obsolete
Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add;
+#pragma warning restore CS0618 // Type or member is obsolete
Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true));
Assert.That(exceptions.Count, Is.EqualTo(2));
}
</ContentPage>";
var exceptions = new List<Exception>();
+#pragma warning disable CS0618 // Type or member is obsolete
Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add;
+#pragma warning restore CS0618 // Type or member is obsolete
Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true));
Assert.That(exceptions.Count, Is.EqualTo(1));
}
</StackLayout>
</ContentPage>";
var exceptions = new List<Exception>();
+#pragma warning disable CS0618 // Type or member is obsolete
Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add;
+#pragma warning restore CS0618 // Type or member is obsolete
Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true));
Assert.That(exceptions.Count, Is.GreaterThan(1));
}
</StackLayout>
</ContentPage>";
var exceptions = new List<Exception>();
+#pragma warning disable CS0618 // Type or member is obsolete
Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add;
+#pragma warning restore CS0618 // Type or member is obsolete
Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true));
Assert.That(exceptions.Count, Is.GreaterThan(1));
}
</ContentPage>";
var exceptions = new List<Exception>();
+#pragma warning disable CS0618 // Type or member is obsolete
Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add;
+#pragma warning restore CS0618 // Type or member is obsolete
var content = (ContentPage)XamlLoader.Create(xaml, true);
Assert.DoesNotThrow(() => content.FindByName<Button>("MyName"));
Assert.That(exceptions.Count, Is.GreaterThanOrEqualTo(1));
</ContentPage>";
XamlLoader.Create(xaml, true);
var exceptions = new List<Exception>();
+#pragma warning disable CS0618 // Type or member is obsolete
Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = exceptions.Add;
+#pragma warning restore CS0618 // Type or member is obsolete
Assert.DoesNotThrow(() => XamlLoader.Create(xaml, true));
Assert.That(exceptions.Count, Is.GreaterThanOrEqualTo(1));
}
Device.PlatformServices = new MockPlatformServices();
//there's a test not resetting the values correctly, but can't find which one...
- Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = null;
#pragma warning disable 0618
+ Xamarin.Forms.Internals.ResourceLoader.ExceptionHandler = null;
Xamarin.Forms.Xaml.Internals.XamlLoader.DoNotThrowOnExceptions = false;
#pragma warning restore 0618
}
using System;
+using System.Linq;
+using System.Reflection;
using System.Runtime.CompilerServices;
namespace Xamarin.Forms.Xaml
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class XamlFilePathAttribute : Attribute
{
- public XamlFilePathAttribute([CallerFilePath] string filePath = "")
- {
- }
+ public XamlFilePathAttribute([CallerFilePath] string filePath = "") => FilePath = filePath;
+
+ public string FilePath { get; }
+
+ internal static string GetFilePathForObject(object view) => (view?.GetType().GetTypeInfo().GetCustomAttributes(typeof(XamlFilePathAttribute), false).FirstOrDefault() as XamlFilePathAttribute)?.FilePath;
}
}
\ No newline at end of file
var rootnode = new RuntimeRootNode(new XmlType(reader.NamespaceURI, reader.Name, null), view, (IXmlNamespaceResolver)reader);
XamlParser.ParseXaml(rootnode, reader);
- Visit(rootnode, new HydrationContext {
- RootElement = view,
#pragma warning disable 0618
- ExceptionHandler = ResourceLoader.ExceptionHandler ?? (Internals.XamlLoader.DoNotThrowOnExceptions ? e => { } : (Action<Exception>)null)
+ var doNotThrow = ResourceLoader.ExceptionHandler2 != null || Internals.XamlLoader.DoNotThrowOnExceptions;
#pragma warning restore 0618
+ void ehandler(Exception e) => ResourceLoader.ExceptionHandler2?.Invoke((e, XamlFilePathAttribute.GetFilePathForObject(view)));
+ Visit(rootnode, new HydrationContext {
+ RootElement = view,
+
+ ExceptionHandler = doNotThrow ? ehandler : (Action<Exception>)null
}, useDesignProperties);
break;
}
public static object Create(string xaml, bool doNotThrow, bool useDesignProperties)
{
- doNotThrow = doNotThrow || ResourceLoader.ExceptionHandler != null;
- var exceptionHandler = doNotThrow ? (ResourceLoader.ExceptionHandler ?? (e => { })) : null;
+ doNotThrow = doNotThrow || ResourceLoader.ExceptionHandler2 != null;
+ void ehandler(Exception e) => ResourceLoader.ExceptionHandler2?.Invoke((e, null));
+
object inflatedView = null;
using (var textreader = new StringReader(xaml))
using (var reader = XmlReader.Create(textreader)) {
var rootnode = new RuntimeRootNode(new XmlType(reader.NamespaceURI, reader.Name, typeArguments), null, (IXmlNamespaceResolver)reader);
XamlParser.ParseXaml(rootnode, reader);
var visitorContext = new HydrationContext {
- ExceptionHandler = exceptionHandler,
+ ExceptionHandler = doNotThrow ? ehandler : (Action<Exception>)null,
};
var cvv = new CreateValuesVisitor(visitorContext);
cvv.Visit((ElementNode)rootnode, null);