зеркало из https://github.com/DeGsoft/maui-linux.git
[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:
Родитель
a03c8f32d2
Коммит
d2708bbadd
|
@ -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;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче