From: Stephane Delcroix Date: Thu, 2 Mar 2017 22:36:25 +0000 (+0100) Subject: Backport #793 (#796) X-Git-Tag: beta-2.3.4-pre3~8 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=dbe0d4945da400a5b00599155d22cda78754294a;p=platform%2Fupstream%2Fxamarin-forms.git Backport #793 (#796) * [Xaml] Fallback to App.Current for DynResources (Previewer) (#793) * [Xaml] Fallback to App.Current for DynResources (Previewer) * [C] avoid NRE and ensure setting the style * remove files committed by accident --- diff --git a/Xamarin.Forms.Core.UnitTests/DynamicResourceTests.cs b/Xamarin.Forms.Core.UnitTests/DynamicResourceTests.cs index faea996..25ae329 100644 --- a/Xamarin.Forms.Core.UnitTests/DynamicResourceTests.cs +++ b/Xamarin.Forms.Core.UnitTests/DynamicResourceTests.cs @@ -10,6 +10,13 @@ namespace Xamarin.Forms.Core.UnitTests { base.Setup (); Device.PlatformServices = new MockPlatformServices (); + Application.Current = new MockApplication(); + } + + [TearDown] + public override void TearDown() + { + Application.Current = null; } [Test] @@ -143,11 +150,24 @@ namespace Xamarin.Forms.Core.UnitTests label.SetDynamicResource (Label.TextProperty, "foo"); label.Resources = new ResourceDictionary { {"foo","FOO"}}; - Assert.AreEqual ("FOO", label.Text); + Assume.That(label.Text, Is.EqualTo("FOO")); label.Resources ["foo"] = "BAR"; Assert.AreEqual ("BAR", label.Text); } + + [Test] + public void FallbackToApplicationCurrent() + { + Application.Current.Resources = new ResourceDictionary { { "foo", "FOO" } }; + + var label = new Label(); + label.BindingContext = new MockViewModel(); + label.SetBinding(Label.TextProperty, "Text", BindingMode.TwoWay); + label.SetDynamicResource(Label.TextProperty, "foo"); + + Assert.That(label.Text, Is.EqualTo("FOO")); + } } } \ No newline at end of file diff --git a/Xamarin.Forms.Core/MergedStyle.cs b/Xamarin.Forms.Core/MergedStyle.cs index 59a1dea..941ebbd 100644 --- a/Xamarin.Forms.Core/MergedStyle.cs +++ b/Xamarin.Forms.Core/MergedStyle.cs @@ -125,12 +125,11 @@ namespace Xamarin.Forms void RegisterImplicitStyles() { Type type = TargetType; - while (true) - { + while (true) { BindableProperty implicitStyleProperty = BindableProperty.Create("ImplicitStyle", typeof(Style), typeof(VisualElement), default(Style), - propertyChanged: (bindable, oldvalue, newvalue) => ((VisualElement)bindable)._mergedStyle.OnImplicitStyleChanged()); - Target.SetDynamicResource(implicitStyleProperty, type.FullName); + propertyChanged: (bindable, oldvalue, newvalue) => OnImplicitStyleChanged()); _implicitStyles.Add(implicitStyleProperty); + Target.SetDynamicResource(implicitStyleProperty, type.FullName); type = type.GetTypeInfo().BaseType; if (s_stopAtTypes.Contains(type)) return; diff --git a/Xamarin.Forms.Core/ResourcesExtensions.cs b/Xamarin.Forms.Core/ResourcesExtensions.cs index 8930abf..7d2f8d0 100644 --- a/Xamarin.Forms.Core/ResourcesExtensions.cs +++ b/Xamarin.Forms.Core/ResourcesExtensions.cs @@ -55,6 +55,11 @@ namespace Xamarin.Forms return true; element = element.Parent; } + + //Fallback for the XF previewer + if (Application.Current != null && Application.Current.Resources != null && Application.Current.Resources.TryGetValue(key, out value)) + return true; + value = null; return false; }