diff options
author | Stephane Delcroix <stephane@delcroix.org> | 2016-09-08 20:39:05 +0200 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-09-08 11:39:05 -0700 |
commit | 85426c5d9495eb1d55b3128bf97e50c68a73b53f (patch) | |
tree | 2f81e5868ce61eb90d15c6c51a354603b8395627 /Xamarin.Forms.Platform.iOS | |
parent | 11326e1c182b3ff5c3d82c6ef7d09c193bc19891 (diff) | |
download | xamarin-forms-85426c5d9495eb1d55b3128bf97e50c68a73b53f.tar.gz xamarin-forms-85426c5d9495eb1d55b3128bf97e50c68a73b53f.tar.bz2 xamarin-forms-85426c5d9495eb1d55b3128bf97e50c68a73b53f.zip |
Native Bindings (#278)
* [C, I, A, W] Support Native Bindings
* fix tabs
Diffstat (limited to 'Xamarin.Forms.Platform.iOS')
-rw-r--r-- | Xamarin.Forms.Platform.iOS/Extensions/LayoutExtensions.cs (renamed from Xamarin.Forms.Platform.iOS/LayoutExtensions.cs) | 0 | ||||
-rw-r--r-- | Xamarin.Forms.Platform.iOS/Extensions/UIViewExtensions.cs | 43 | ||||
-rw-r--r-- | Xamarin.Forms.Platform.iOS/NativeViewPropertyListener.cs | 29 | ||||
-rw-r--r-- | Xamarin.Forms.Platform.iOS/NativeViewWrapper.cs | 8 | ||||
-rw-r--r-- | Xamarin.Forms.Platform.iOS/Xamarin.Forms.Platform.iOS.Classic.csproj | 3 | ||||
-rw-r--r-- | Xamarin.Forms.Platform.iOS/Xamarin.Forms.Platform.iOS.csproj | 3 |
6 files changed, 84 insertions, 2 deletions
diff --git a/Xamarin.Forms.Platform.iOS/LayoutExtensions.cs b/Xamarin.Forms.Platform.iOS/Extensions/LayoutExtensions.cs index 6e7f5738..6e7f5738 100644 --- a/Xamarin.Forms.Platform.iOS/LayoutExtensions.cs +++ b/Xamarin.Forms.Platform.iOS/Extensions/LayoutExtensions.cs diff --git a/Xamarin.Forms.Platform.iOS/Extensions/UIViewExtensions.cs b/Xamarin.Forms.Platform.iOS/Extensions/UIViewExtensions.cs index 9847867c..73475b13 100644 --- a/Xamarin.Forms.Platform.iOS/Extensions/UIViewExtensions.cs +++ b/Xamarin.Forms.Platform.iOS/Extensions/UIViewExtensions.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.Drawing; using System.Linq; using System; +using static System.String; + #if __UNIFIED__ using UIKit; @@ -28,6 +30,47 @@ namespace Xamarin.Forms.Platform.iOS return new SizeRequest(request, minimum); } + public static void SetBinding(this UIView view, string propertyName, BindingBase bindingBase, string updateSourceEventName = null) + { + var binding = bindingBase as Binding; + //This will allow setting bindings from Xaml by reusing the MarkupExtension + updateSourceEventName = updateSourceEventName ?? binding?.UpdateSourceEventName; + + if (!IsNullOrEmpty(updateSourceEventName)) + { + NativeBindingHelpers.SetBinding(view, propertyName, bindingBase, updateSourceEventName); + return; + } + + NativeViewPropertyListener nativePropertyListener = null; + if (bindingBase.Mode == BindingMode.TwoWay) { + nativePropertyListener = new NativeViewPropertyListener(propertyName); + view.AddObserver(nativePropertyListener, propertyName, 0, IntPtr.Zero); + } + + NativeBindingHelpers.SetBinding(view, propertyName, bindingBase, nativePropertyListener); + } + + public static void SetBinding(this UIView self, BindableProperty targetProperty, BindingBase binding) + { + NativeBindingHelpers.SetBinding(self, targetProperty, binding); + } + + public static void SetValue(this UIView target, BindableProperty targetProperty, object value) + { + NativeBindingHelpers.SetValue(target, targetProperty, value); + } + + public static void SetBindingContext(this UIView target, object bindingContext, Func<UIView, IEnumerable<UIView>> getChildren = null) + { + NativeBindingHelpers.SetBindingContext(target, bindingContext, getChildren); + } + + internal static void TransferbindablePropertiesToWrapper(this UIView target, View wrapper) + { + NativeBindingHelpers.TransferBindablePropertiesToWrapper(target, wrapper); + } + internal static T FindDescendantView<T>(this UIView view) where T : UIView { var queue = new Queue<UIView>(); diff --git a/Xamarin.Forms.Platform.iOS/NativeViewPropertyListener.cs b/Xamarin.Forms.Platform.iOS/NativeViewPropertyListener.cs new file mode 100644 index 00000000..ffe117f4 --- /dev/null +++ b/Xamarin.Forms.Platform.iOS/NativeViewPropertyListener.cs @@ -0,0 +1,29 @@ +using System; +using System.ComponentModel; +#if __UNIFIED__ +using Foundation; + +#else +using MonoTouch.Foundation; +#endif + +namespace Xamarin.Forms.Platform.iOS +{ + class NativeViewPropertyListener : NSObject, INotifyPropertyChanged + { + string TargetProperty { get; set; } + + public NativeViewPropertyListener(string targetProperty) + { + TargetProperty = targetProperty; + } + + public event PropertyChangedEventHandler PropertyChanged; + + public override void ObserveValue(NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr context) + { + if (keyPath == TargetProperty) + PropertyChanged?.Invoke(ofObject, new PropertyChangedEventArgs(TargetProperty)); + } + } +} diff --git a/Xamarin.Forms.Platform.iOS/NativeViewWrapper.cs b/Xamarin.Forms.Platform.iOS/NativeViewWrapper.cs index 209aca80..5474c65b 100644 --- a/Xamarin.Forms.Platform.iOS/NativeViewWrapper.cs +++ b/Xamarin.Forms.Platform.iOS/NativeViewWrapper.cs @@ -22,6 +22,8 @@ namespace Xamarin.Forms.Platform.iOS SizeThatFitsDelegate = sizeThatFitsDelegate; LayoutSubViews = layoutSubViews; NativeView = nativeView; + + nativeView.TransferbindablePropertiesToWrapper(this); } public GetDesiredSizeDelegate GetDesiredSizeDelegate { get; } @@ -31,5 +33,11 @@ namespace Xamarin.Forms.Platform.iOS public UIView NativeView { get; } public SizeThatFitsDelegate SizeThatFitsDelegate { get; set; } + + protected override void OnBindingContextChanged() + { + NativeView.SetBindingContext(BindingContext, nv => nv.Subviews); + base.OnBindingContextChanged(); + } } }
\ No newline at end of file diff --git a/Xamarin.Forms.Platform.iOS/Xamarin.Forms.Platform.iOS.Classic.csproj b/Xamarin.Forms.Platform.iOS/Xamarin.Forms.Platform.iOS.Classic.csproj index 5b705d01..45699dae 100644 --- a/Xamarin.Forms.Platform.iOS/Xamarin.Forms.Platform.iOS.Classic.csproj +++ b/Xamarin.Forms.Platform.iOS/Xamarin.Forms.Platform.iOS.Classic.csproj @@ -76,7 +76,6 @@ <Compile Include="NativeViewWrapper.cs" /> <Compile Include="NativeViewWrapperRenderer.cs" /> <Compile Include="PlatformEffect.cs" /> - <Compile Include="LayoutExtensions.cs" /> <Compile Include="Renderers\AlignmentExtensions.cs" /> <Compile Include="PageExtensions.cs" /> <Compile Include="Renderers\ExportCellAttribute.cs" /> @@ -84,6 +83,8 @@ <Compile Include="Renderers\ExportRendererAttribute.cs" /> <Compile Include="Resources\StringResources.Designer.cs" /> <Compile Include="ViewInitializedEventArgs.cs" /> + <Compile Include="NativeViewPropertyListener.cs" /> + <Compile Include="Extensions\LayoutExtensions.cs" /> </ItemGroup> <ItemGroup> <Compile Include="$(MSBuildThisFileDirectory)Deserializer.cs" /> diff --git a/Xamarin.Forms.Platform.iOS/Xamarin.Forms.Platform.iOS.csproj b/Xamarin.Forms.Platform.iOS/Xamarin.Forms.Platform.iOS.csproj index d2364175..8fde4c81 100644 --- a/Xamarin.Forms.Platform.iOS/Xamarin.Forms.Platform.iOS.csproj +++ b/Xamarin.Forms.Platform.iOS/Xamarin.Forms.Platform.iOS.csproj @@ -137,7 +137,6 @@ <Compile Include="NativeViewWrapper.cs" /> <Compile Include="NativeViewWrapperRenderer.cs" /> <Compile Include="PlatformEffect.cs" /> - <Compile Include="LayoutExtensions.cs" /> <Compile Include="Renderers\AlignmentExtensions.cs" /> <Compile Include="Forms.cs" /> <Compile Include="PageExtensions.cs" /> @@ -149,6 +148,8 @@ <Compile Include="ViewInitializedEventArgs.cs" /> <Compile Include="IOSAppIndexingProvider.cs" /> <Compile Include="IOSAppLinks.cs" /> + <Compile Include="NativeViewPropertyListener.cs" /> + <Compile Include="Extensions\LayoutExtensions.cs" /> </ItemGroup> <ItemGroup> <EmbeddedResource Include="Resources\StringResources.ar.resx" /> |