Navigation in Avalonia

This commit is contained in:
Daniel Clark
2022-12-05 19:39:13 +00:00
parent 855a4b98d2
commit 358926a5d4
17 changed files with 1660 additions and 59 deletions
@@ -0,0 +1,26 @@
using Avalonia;
using Avalonia.Data.Converters;
using System.Globalization;
namespace Toolkit.Foundation.Avalonia
{
public static class EventArgsExtensions
{
public static dynamic? GetEventArguments(this EventArgs args, string? path, IValueConverter? converter, object? converterParameter)
{
return !string.IsNullOrWhiteSpace(path) ? GetEventArgsPropertyPathValue(args, path) : converter is not null ? converter.Convert(args, typeof(object), converterParameter, CultureInfo.CurrentCulture) : (dynamic)args;
}
private static object GetEventArgsPropertyPathValue(object args, string path)
{
object? value = args;
if (path is { })
{
value = PropertyPathHelper.GetValue(args, path);
}
return value;
}
}
}
@@ -0,0 +1,17 @@
using Avalonia.Data;
namespace Toolkit.Foundation.Avalonia
{
public static class MarkupExtensions
{
public static Binding? ToBinding(this object value)
{
if (value is Binding)
{
return value as Binding;
}
return new Binding { Mode = BindingMode.OneWay, Source = value };
}
}
}
@@ -0,0 +1,28 @@
using Avalonia;
using Avalonia.Data;
namespace Toolkit.Foundation.Avalonia
{
public static class PropertyPathHelper
{
private static readonly Dummy dummy = new();
public static object GetValue(object args, string path)
{
Binding binding = new(path)
{
Mode = BindingMode.OneTime,
Source = args
};
dummy.Bind(Dummy.ValueProperty, binding);
return dummy.GetValue(Dummy.ValueProperty);
}
private class Dummy : AvaloniaObject
{
public static readonly StyledProperty<object> ValueProperty =
AvaloniaProperty.Register<Dummy, object>("Value");
}
}
}