summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephane Delcroix <stephane@delcroix.org>2017-03-02 23:28:37 +0100
committerGitHub <noreply@github.com>2017-03-02 23:28:37 +0100
commitd2708bbadd0bf09c2542e354683c0fd1bbc84707 (patch)
tree6f267d261bbc60cfa9a723a9ade669e061f17aa8
parenta03c8f32d20a61a1a553b7db0e2f978c44a4ca84 (diff)
downloadxamarin-forms-d2708bbadd0bf09c2542e354683c0fd1bbc84707.tar.gz
xamarin-forms-d2708bbadd0bf09c2542e354683c0fd1bbc84707.tar.bz2
xamarin-forms-d2708bbadd0bf09c2542e354683c0fd1bbc84707.zip
[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
-rw-r--r--Xamarin.Forms.Controls/SdxApp.xaml5
-rw-r--r--Xamarin.Forms.Controls/SdxApp.xaml.cs22
-rw-r--r--Xamarin.Forms.Controls/SdxPage.xaml5
-rw-r--r--Xamarin.Forms.Controls/SdxPage.xaml.cs22
-rw-r--r--Xamarin.Forms.Core.UnitTests/DynamicResourceTests.cs22
-rw-r--r--Xamarin.Forms.Core/MergedStyle.cs7
-rw-r--r--Xamarin.Forms.Core/ResourcesExtensions.cs5
7 files changed, 83 insertions, 5 deletions
diff --git a/Xamarin.Forms.Controls/SdxApp.xaml b/Xamarin.Forms.Controls/SdxApp.xaml
new file mode 100644
index 00000000..a859f491
--- /dev/null
+++ b/Xamarin.Forms.Controls/SdxApp.xaml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Xamarin.Forms.Controls.SdxApp">
+ <ContentPage.Content>
+ </ContentPage.Content>
+</ContentPage>
diff --git a/Xamarin.Forms.Controls/SdxApp.xaml.cs b/Xamarin.Forms.Controls/SdxApp.xaml.cs
new file mode 100644
index 00000000..5ad3a618
--- /dev/null
+++ b/Xamarin.Forms.Controls/SdxApp.xaml.cs
@@ -0,0 +1,22 @@
+//
+// SdxApp.xaml.cs
+//
+// Author:
+// Stephane Delcroix <stdelc@microsoft.com>
+//
+// Copyright (c) 2017
+using System;
+using System.Collections.Generic;
+
+using Xamarin.Forms;
+
+namespace Xamarin.Forms.Controls
+{
+ public partial class SdxApp : ContentPage
+ {
+ public SdxApp()
+ {
+ InitializeComponent();
+ }
+ }
+}
diff --git a/Xamarin.Forms.Controls/SdxPage.xaml b/Xamarin.Forms.Controls/SdxPage.xaml
new file mode 100644
index 00000000..7dffd414
--- /dev/null
+++ b/Xamarin.Forms.Controls/SdxPage.xaml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Xamarin.Forms.Controls.SdxPage">
+ <ContentPage.Content>
+ </ContentPage.Content>
+</ContentPage>
diff --git a/Xamarin.Forms.Controls/SdxPage.xaml.cs b/Xamarin.Forms.Controls/SdxPage.xaml.cs
new file mode 100644
index 00000000..d4153161
--- /dev/null
+++ b/Xamarin.Forms.Controls/SdxPage.xaml.cs
@@ -0,0 +1,22 @@
+//
+// SdxPage.xaml.cs
+//
+// Author:
+// Stephane Delcroix <stdelc@microsoft.com>
+//
+// Copyright (c) 2017
+using System;
+using System.Collections.Generic;
+
+using Xamarin.Forms;
+
+namespace Xamarin.Forms.Controls
+{
+ public partial class SdxPage : ContentPage
+ {
+ public SdxPage()
+ {
+ InitializeComponent();
+ }
+ }
+}
diff --git a/Xamarin.Forms.Core.UnitTests/DynamicResourceTests.cs b/Xamarin.Forms.Core.UnitTests/DynamicResourceTests.cs
index faea9963..25ae3290 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 59a1dea6..941ebbd8 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 8930abf2..7d2f8d0f 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;
}