Backport #793 (#796)
authorStephane Delcroix <stephane@delcroix.org>
Thu, 2 Mar 2017 22:36:25 +0000 (23:36 +0100)
committerJason Smith <jason.smith@xamarin.com>
Thu, 2 Mar 2017 22:36:25 +0000 (14:36 -0800)
* [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

Xamarin.Forms.Core.UnitTests/DynamicResourceTests.cs
Xamarin.Forms.Core/MergedStyle.cs
Xamarin.Forms.Core/ResourcesExtensions.cs

index faea996..25ae329 100644 (file)
@@ -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
index 59a1dea..941ebbd 100644 (file)
@@ -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;
index 8930abf..7d2f8d0 100644 (file)
@@ -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;
                }