[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
This commit is contained in:
Stephane Delcroix 2017-03-02 23:28:37 +01:00 коммит произвёл GitHub
Родитель a03c8f32d2
Коммит d2708bbadd
7 изменённых файлов: 83 добавлений и 5 удалений

Просмотреть файл

@ -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>

Просмотреть файл

@ -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();
}
}
}

Просмотреть файл

@ -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>

Просмотреть файл

@ -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();
}
}
}

Просмотреть файл

@ -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"));
}
}
}

Просмотреть файл

@ -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;

Просмотреть файл

@ -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;
}