committed by
GitHub
32 changed files with 441 additions and 87 deletions
Binary file not shown.
@ -0,0 +1,99 @@ |
|||||
|
using System; |
||||
|
using System.ComponentModel; |
||||
|
using System.Globalization; |
||||
|
using System.Linq; |
||||
|
using System.Linq.Expressions; |
||||
|
using System.Reflection; |
||||
|
using Avalonia.Controls; |
||||
|
using Avalonia.Markup.Xaml.PortableXaml; |
||||
|
using Portable.Xaml; |
||||
|
|
||||
|
namespace Avalonia.Markup.Xaml.Converters |
||||
|
{ |
||||
|
internal class AvaloniaEventConverter : TypeConverter |
||||
|
{ |
||||
|
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) |
||||
|
{ |
||||
|
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); |
||||
|
} |
||||
|
|
||||
|
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) |
||||
|
{ |
||||
|
var text = value as string; |
||||
|
if (text != null) |
||||
|
{ |
||||
|
var rootObjectProvider = context.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider; |
||||
|
var destinationTypeProvider = context.GetService(typeof(IDestinationTypeProvider)) as IDestinationTypeProvider; |
||||
|
if (rootObjectProvider != null && destinationTypeProvider != null) |
||||
|
{ |
||||
|
var target = rootObjectProvider.RootObject; |
||||
|
var eventType = destinationTypeProvider.GetDestinationType(); |
||||
|
var eventParameters = eventType.GetRuntimeMethods().First(r => r.Name == "Invoke").GetParameters(); |
||||
|
// go in reverse to match System.Xaml behaviour
|
||||
|
var methods = target.GetType().GetRuntimeMethods().Reverse(); |
||||
|
|
||||
|
// find based on exact match parameter types first
|
||||
|
foreach (var method in methods) |
||||
|
{ |
||||
|
if (method.Name != text) |
||||
|
continue; |
||||
|
var parameters = method.GetParameters(); |
||||
|
if (eventParameters.Length != parameters.Length) |
||||
|
continue; |
||||
|
if (parameters.Length == 0) |
||||
|
return method.CreateDelegate(eventType, target); |
||||
|
|
||||
|
for (int i = 0; i < parameters.Length; i++) |
||||
|
{ |
||||
|
var param = parameters[i]; |
||||
|
var eventParam = eventParameters[i]; |
||||
|
if (param.ParameterType != eventParam.ParameterType) |
||||
|
break; |
||||
|
if (i == parameters.Length - 1) |
||||
|
return method.CreateDelegate(eventType, target); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// EnhancedXaml: Find method with compatible base class parameters
|
||||
|
foreach (var method in methods) |
||||
|
{ |
||||
|
if (method.Name != text) |
||||
|
continue; |
||||
|
var parameters = method.GetParameters(); |
||||
|
if (parameters.Length == 0 || eventParameters.Length != parameters.Length) |
||||
|
continue; |
||||
|
|
||||
|
for (int i = 0; i < parameters.Length; i++) |
||||
|
{ |
||||
|
var param = parameters[i]; |
||||
|
var eventParam = eventParameters[i]; |
||||
|
if (!param.ParameterType.GetTypeInfo().IsAssignableFrom(eventParam.ParameterType.GetTypeInfo())) |
||||
|
break; |
||||
|
if (i == parameters.Length - 1) |
||||
|
return method.CreateDelegate(eventType, target); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
var contextProvider = (IXamlSchemaContextProvider)context.GetService(typeof(IXamlSchemaContextProvider)); |
||||
|
var avaloniaContext = (AvaloniaXamlSchemaContext)contextProvider.SchemaContext; |
||||
|
|
||||
|
if (avaloniaContext.IsDesignMode) |
||||
|
{ |
||||
|
// We want to ignore missing events in the designer, so if event handler
|
||||
|
// wasn't found create an empty delegate.
|
||||
|
var lambdaExpression = Expression.Lambda( |
||||
|
eventType, |
||||
|
Expression.Empty(), |
||||
|
eventParameters.Select(x => Expression.Parameter(x.ParameterType))); |
||||
|
return lambdaExpression.Compile(); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
throw new XamlObjectWriterException($"Referenced value method {text} in type {target.GetType()} indicated by event {eventType.FullName} was not found"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return base.ConvertFrom(context, culture, value); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
using Avalonia.Controls.Shapes; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Controls.UnitTests.Shapes |
||||
|
{ |
||||
|
public class PathTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Path_With_Null_Data_Does_Not_Throw_On_Measure() |
||||
|
{ |
||||
|
var target = new Path(); |
||||
|
|
||||
|
target.Measure(Size.Infinity); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,66 @@ |
|||||
|
// Copyright (c) The Avalonia Project. All rights reserved.
|
||||
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
||||
|
|
||||
|
using System; |
||||
|
using Avalonia.Controls; |
||||
|
using Avalonia.Input; |
||||
|
using Portable.Xaml; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Markup.Xaml.UnitTests.Xaml |
||||
|
{ |
||||
|
public class EventTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Event_Is_Attached() |
||||
|
{ |
||||
|
var xaml = @"<Button xmlns='https://github.com/avaloniaui' Click='OnClick'/>"; |
||||
|
var loader = new AvaloniaXamlLoader(); |
||||
|
var target = new MyButton(); |
||||
|
|
||||
|
loader.Load(xaml, rootInstance: target); |
||||
|
RaiseClick(target); |
||||
|
|
||||
|
Assert.True(target.Clicked); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Exception_Is_Thrown_If_Event_Not_Found() |
||||
|
{ |
||||
|
var xaml = @"<Button xmlns='https://github.com/avaloniaui' Click='NotFound'/>"; |
||||
|
var loader = new AvaloniaXamlLoader(); |
||||
|
var target = new MyButton(); |
||||
|
|
||||
|
Assert.Throws<XamlObjectWriterException>(() => loader.Load(xaml, rootInstance: target)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Exception_Is_Not_Thrown_If_Event_Not_Found_In_Design_Mode() |
||||
|
{ |
||||
|
var xaml = @"<Button xmlns='https://github.com/avaloniaui' Click='NotFound'/>"; |
||||
|
var loader = new AvaloniaXamlLoader { IsDesignMode = true }; |
||||
|
var target = new MyButton(); |
||||
|
|
||||
|
loader.Load(xaml, rootInstance: target); |
||||
|
} |
||||
|
|
||||
|
private void RaiseClick(MyButton target) |
||||
|
{ |
||||
|
target.RaiseEvent(new KeyEventArgs |
||||
|
{ |
||||
|
RoutedEvent = Button.KeyDownEvent, |
||||
|
Key = Key.Enter, |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
class MyButton : Button |
||||
|
{ |
||||
|
public bool Clicked { get; private set; } |
||||
|
|
||||
|
public void OnClick(object sender, EventArgs e) |
||||
|
{ |
||||
|
Clicked = true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue