-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Simplify TemplateBindings #1695
Changes from all commits
b8f127f
be39c7c
03edafc
8bd8a61
62a1040
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,7 +139,7 @@ public object Load(Uri uri, Uri baseUri = null, object rootInstance = null) | |
{ | ||
uriString = new Uri(baseUri, uri).AbsoluteUri; | ||
} | ||
throw new XamlLoadException("Error loading xaml at " + uriString, e); | ||
throw new XamlLoadException("Error loading xaml at " + uriString + ": " + e.Message, e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also fixed the annoying problem where the XAML exception message didn't actually include the description of what went wrong. |
||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,85 +2,80 @@ | |
// Licensed under the MIT license. See licence.md file in the project root for full license information. | ||
|
||
using System; | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using Avalonia.Markup.Xaml.Templates; | ||
using Avalonia.Styling; | ||
using Portable.Xaml; | ||
using Portable.Xaml.ComponentModel; | ||
using Portable.Xaml.Markup; | ||
|
||
namespace Avalonia.Markup.Xaml.Converters | ||
{ | ||
using Avalonia.Styling; | ||
using Portable.Xaml; | ||
using Portable.Xaml.ComponentModel; | ||
using System.ComponentModel; | ||
using Portable.Xaml.Markup; | ||
|
||
public class AvaloniaPropertyTypeConverter : TypeConverter | ||
{ | ||
private static readonly Regex regex = new Regex(@"^\(?(\w*)\.(\w*)\)?|(.*)$"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This allows attached properties to be optionally specified with braces surrounding the property, as in a standard There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way we can do this without using regular expressions? Regex uses lot of memory for us just checking if the string starts and ends with parentheses. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah definitely! I looked at using |
||
|
||
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) | ||
{ | ||
return sourceType == typeof(string); | ||
} | ||
|
||
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) | ||
{ | ||
var s = (string)value; | ||
var (owner, propertyName) = ParseProperty((string)value); | ||
var ownerType = TryResolveOwnerByName(context, owner) ?? | ||
context.GetFirstAmbientValue<ControlTemplate>()?.TargetType ?? | ||
context.GetFirstAmbientValue<Style>()?.Selector?.TargetType; | ||
|
||
string typeName; | ||
string propertyName; | ||
Type type = null; | ||
if (ownerType == null) | ||
{ | ||
throw new XamlLoadException( | ||
$"Could not determine the owner type for property '{propertyName}'. " + | ||
"Please fully qualify the property name or specify a target type on " + | ||
"the containing template."); | ||
} | ||
|
||
ParseProperty(s, out typeName, out propertyName); | ||
var property = AvaloniaPropertyRegistry.Instance.FindRegistered(ownerType, propertyName); | ||
|
||
if (typeName == null) | ||
if (property == null) | ||
{ | ||
var style = context.GetFirstAmbientValue<Style>(); | ||
throw new XamlLoadException($"Could not find AvaloniaProperty '{ownerType.Name}.{propertyName}'."); | ||
} | ||
|
||
type = style?.Selector?.TargetType; | ||
return property; | ||
} | ||
|
||
if (type == null) | ||
{ | ||
throw new Exception( | ||
"Could not determine the target type. Please fully qualify the property name."); | ||
} | ||
} | ||
else | ||
private Type TryResolveOwnerByName(ITypeDescriptorContext context, string owner) | ||
{ | ||
if (owner != null) | ||
{ | ||
var typeResolver = context.GetService<IXamlTypeResolver>(); | ||
type = typeResolver.Resolve(typeName); | ||
var resolver = context.GetService<IXamlTypeResolver>(); | ||
var result = resolver.Resolve(owner); | ||
|
||
if (type == null) | ||
if (result == null) | ||
{ | ||
throw new Exception($"Could not find type '{typeName}'."); | ||
throw new XamlLoadException($"Could not find type '{owner}'."); | ||
} | ||
} | ||
|
||
AvaloniaProperty property = AvaloniaPropertyRegistry.Instance.FindRegistered(type, propertyName); | ||
|
||
if (property == null) | ||
{ | ||
throw new Exception( | ||
$"Could not find AvaloniaProperty '{type.Name}.{propertyName}'."); | ||
return result; | ||
} | ||
|
||
return property; | ||
return null; | ||
} | ||
|
||
private void ParseProperty(string s, out string typeName, out string propertyName) | ||
private (string owner, string property) ParseProperty(string s) | ||
{ | ||
var split = s.Split('.'); | ||
var result = regex.Match(s); | ||
|
||
if (split.Length == 1) | ||
{ | ||
typeName = null; | ||
propertyName = split[0]; | ||
} | ||
else if (split.Length == 2) | ||
if (result.Groups[1].Success) | ||
{ | ||
typeName = split[0]; | ||
propertyName = split[1]; | ||
return (result.Groups[1].Value, result.Groups[2].Value); | ||
} | ||
else | ||
{ | ||
throw new Exception($"Invalid property name: '{s}'."); | ||
return (null, result.Groups[3].Value); | ||
} | ||
} | ||
} | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This setter was both unnecessary and caused problems in that it was binding to
Track.Orientation
instead ofSlider.Orientation
which are actually differentAvaloniaProperty
s (#1696). This was resulting in theSlider
getting messed up.