Browse Source
When passed to Binding. Found another problem with OmniXAML which means we can't continue along this road: OmniXAML issue #50.pull/297/head
19 changed files with 282 additions and 86 deletions
@ -0,0 +1,33 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using System.Globalization; |
|||
using Perspex; |
|||
using Perspex.Markup; |
|||
|
|||
namespace XamlTestApplication |
|||
{ |
|||
public class StringNullOrEmpty : IValueConverter |
|||
{ |
|||
public static readonly StringNullOrEmpty Instance = new StringNullOrEmpty(); |
|||
|
|||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
|||
{ |
|||
if (value == null) |
|||
{ |
|||
return true; |
|||
} |
|||
else |
|||
{ |
|||
var s = value as string; |
|||
return s != null ? string.IsNullOrEmpty(s) : PerspexProperty.UnsetValue; |
|||
} |
|||
} |
|||
|
|||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using OmniXaml; |
|||
using Perspex.Markup.Xaml.Data; |
|||
|
|||
namespace Perspex.Markup.Xaml.MarkupExtensions |
|||
{ |
|||
public class BindingExtension : MarkupExtension |
|||
{ |
|||
public BindingExtension() |
|||
{ |
|||
} |
|||
|
|||
public BindingExtension(string path) |
|||
{ |
|||
Path = path; |
|||
} |
|||
|
|||
public override object ProvideValue(MarkupExtensionContext extensionContext) |
|||
{ |
|||
return new Data.Binding |
|||
{ |
|||
Mode = Mode, |
|||
SourcePropertyPath = Path, |
|||
}; |
|||
} |
|||
|
|||
public object Converter { get; set; } |
|||
public BindingMode Mode { get; set; } |
|||
public string Path { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,86 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
using Glass; |
|||
using OmniXaml; |
|||
|
|||
namespace Perspex.Markup.Xaml.MarkupExtensions |
|||
{ |
|||
public class StaticExtension : MarkupExtension |
|||
{ |
|||
public StaticExtension() |
|||
{ |
|||
} |
|||
|
|||
public StaticExtension(string identifier) |
|||
{ |
|||
Identifier = identifier; |
|||
} |
|||
|
|||
public string Identifier { get; set; } |
|||
|
|||
public override object ProvideValue(MarkupExtensionContext markupExtensionContext) |
|||
{ |
|||
var typeRepository = markupExtensionContext.TypeRepository; |
|||
var typeAndMember = GetTypeAndMember(Identifier); |
|||
var prefixAndType = GetPrefixAndType(typeAndMember.Item1); |
|||
var xamlType = typeRepository.GetByPrefix(prefixAndType.Item1, prefixAndType.Item2); |
|||
return GetValue(xamlType.UnderlyingType, typeAndMember.Item2); |
|||
} |
|||
|
|||
private static Tuple<string, string> GetTypeAndMember(string s) |
|||
{ |
|||
var parts = s.Split('.'); |
|||
|
|||
if (parts.Length != 2) |
|||
{ |
|||
throw new ArgumentException("Static member must be in the form Type.Member."); |
|||
} |
|||
|
|||
return Tuple.Create(parts[0], parts[1]); |
|||
} |
|||
|
|||
private static Tuple<string, string> GetPrefixAndType(string s) |
|||
{ |
|||
if (s.Contains(":")) |
|||
{ |
|||
return s.Dicotomize(':'); |
|||
} |
|||
else |
|||
{ |
|||
return new Tuple<string, string>(string.Empty, s); |
|||
} |
|||
} |
|||
|
|||
private object GetValue(Type type, string name) |
|||
{ |
|||
var t = type; |
|||
|
|||
while (t != null) |
|||
{ |
|||
var result = t.GetTypeInfo().DeclaredMembers.FirstOrDefault(x => x.Name == name); |
|||
|
|||
if (result is PropertyInfo) |
|||
{ |
|||
var property = ((PropertyInfo)result); |
|||
|
|||
if (property.GetMethod.IsStatic) |
|||
{ |
|||
return ((PropertyInfo)result).GetValue(null); |
|||
} |
|||
} |
|||
else if (result is FieldInfo) |
|||
{ |
|||
return ((FieldInfo)result).GetValue(null); |
|||
} |
|||
|
|||
t = t.GetTypeInfo().BaseType; |
|||
} |
|||
|
|||
throw new ArgumentException($"Static member '{type}.{name}' not found."); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue