From 28eb6e12eb1b4d7ff80cbe0fd2bf0ea4012cdaa6 Mon Sep 17 00:00:00 2001 From: Eric Maupin Date: Wed, 20 Feb 2019 17:30:04 -0500 Subject: [Core/Win] Add ITypeInfo property editor --- .../Properties/Resources.Designer.cs | 18 ++++++ Xamarin.PropertyEditing/Properties/Resources.resx | 7 +++ .../Reflection/ReflectionObjectEditor.cs | 3 +- .../ViewModels/PropertiesViewModel.cs | 1 + .../ViewModels/TypePropertyViewModel.cs | 69 ++++++++++++++++++++++ .../Xamarin.PropertyEditing.csproj | 1 + 6 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 Xamarin.PropertyEditing/ViewModels/TypePropertyViewModel.cs (limited to 'Xamarin.PropertyEditing') diff --git a/Xamarin.PropertyEditing/Properties/Resources.Designer.cs b/Xamarin.PropertyEditing/Properties/Resources.Designer.cs index 7a52edb..1249b10 100644 --- a/Xamarin.PropertyEditing/Properties/Resources.Designer.cs +++ b/Xamarin.PropertyEditing/Properties/Resources.Designer.cs @@ -1140,6 +1140,15 @@ namespace Xamarin.PropertyEditing.Properties { } } + /// + /// Looks up a localized string similar to Select. + /// + public static string Select { + get { + return ResourceManager.GetString("Select", resourceCulture); + } + } + /// /// Looks up a localized string similar to Select Object. /// @@ -1158,6 +1167,15 @@ namespace Xamarin.PropertyEditing.Properties { } } + /// + /// Looks up a localized string similar to Select {0} type. + /// + public static string SelectTypeForProperty { + get { + return ResourceManager.GetString("SelectTypeForProperty", resourceCulture); + } + } + /// /// Looks up a localized string similar to Shared. /// diff --git a/Xamarin.PropertyEditing/Properties/Resources.resx b/Xamarin.PropertyEditing/Properties/Resources.resx index 168c1eb..9e44bbd 100644 --- a/Xamarin.PropertyEditing/Properties/Resources.resx +++ b/Xamarin.PropertyEditing/Properties/Resources.resx @@ -570,4 +570,11 @@ New {0} New {PropertyName} + + Select + + + Select {0} type + Select {property} type + \ No newline at end of file diff --git a/Xamarin.PropertyEditing/Reflection/ReflectionObjectEditor.cs b/Xamarin.PropertyEditing/Reflection/ReflectionObjectEditor.cs index 3ea7209..a7fbbc5 100644 --- a/Xamarin.PropertyEditing/Reflection/ReflectionObjectEditor.cs +++ b/Xamarin.PropertyEditing/Reflection/ReflectionObjectEditor.cs @@ -159,7 +159,8 @@ namespace Xamarin.PropertyEditing.Reflection } } - types = types.Where (t => realType.IsAssignableFrom (t)); + if (realType != typeof(Type)) + types = types.Where (t => realType.IsAssignableFrom (t)); return new AssignableTypesResult (types.Select (t => { string asmName = t.Assembly.GetName ().Name; diff --git a/Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs b/Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs index 0ee785b..7ddae70 100644 --- a/Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs +++ b/Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs @@ -614,6 +614,7 @@ namespace Xamarin.PropertyEditing.ViewModels { typeof(BindingSource), (tp,p,e,v) => new PropertyViewModel (tp, p, e, v) }, { typeof(Resource), (tp,p,e,v) => new PropertyViewModel (tp, p, e, v) }, { typeof(object), (tp,p,e,v) => new ObjectPropertyViewModel (tp, p, e, v) }, + { typeof(ITypeInfo), (tp,p,e,v) => new TypePropertyViewModel (tp, p, e, v) }, { typeof(CommonRatio), (tp, p, e, v) => new RatioViewModel (tp, p, e, v) }, }; } diff --git a/Xamarin.PropertyEditing/ViewModels/TypePropertyViewModel.cs b/Xamarin.PropertyEditing/ViewModels/TypePropertyViewModel.cs new file mode 100644 index 0000000..fe192d9 --- /dev/null +++ b/Xamarin.PropertyEditing/ViewModels/TypePropertyViewModel.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Input; + +namespace Xamarin.PropertyEditing.ViewModels +{ + internal class TypePropertyViewModel + : PropertyViewModel + { + public TypePropertyViewModel (TargetPlatform platform, IPropertyInfo propertyInfo, IEnumerable editors, PropertyVariation variation = null) + : base (platform, propertyInfo, editors, variation) + { + SelectTypeCommand = new RelayCommand (SetType, () => Property.CanWrite); + } + + public event EventHandler TypeRequested; + + public ICommand SelectTypeCommand + { + get; + } + + public AsyncValue>> AssignableTypes + { + get + { + if (this.assignableTypes == null) + this.assignableTypes = new AsyncValue>> (GetAssignableTypesAsync ()); + + return this.assignableTypes; + } + } + + private AsyncValue>> assignableTypes; + + private async Task>> GetAssignableTypesAsync () + { + AssignableTypesResult result = await Editors.GetCommonAssignableTypes (Property, childTypes: false).ConfigureAwait (false); + return result.GetTypeTree (); + } + + private async void SetType () + { + using (await AsyncWork.RequestAsyncWork (this)) { + ITypeInfo selectedType = null; + var args = new TypeRequestedEventArgs (); + TypeRequested?.Invoke (this, args); + if (args.SelectedType == null) + return; + + try { + selectedType = await args.SelectedType; + if (selectedType == null) + return; + } catch (OperationCanceledException) { + return; + } + + await SetValueAsync (new ValueInfo { + Value = selectedType, + Source = ValueSource.Local + }); + } + } + } +} diff --git a/Xamarin.PropertyEditing/Xamarin.PropertyEditing.csproj b/Xamarin.PropertyEditing/Xamarin.PropertyEditing.csproj index 35df7c9..b638f30 100644 --- a/Xamarin.PropertyEditing/Xamarin.PropertyEditing.csproj +++ b/Xamarin.PropertyEditing/Xamarin.PropertyEditing.csproj @@ -165,6 +165,7 @@ + -- cgit v1.2.3