* [C] unset the FromStyle flag on manual setting

* [C] Do not reset overriden values to default

* complete the fix
This commit is contained in:
Stephane Delcroix 2017-04-06 23:13:49 +02:00 коммит произвёл Jason Smith
Родитель eb3db860e4
Коммит 0ee636003b
9 изменённых файлов: 257 добавлений и 9 удалений

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

@ -8,12 +8,19 @@ namespace Xamarin.Forms.Core.UnitTests
public class StyleTests : BaseTestFixture
{
[SetUp]
public void Setup ()
public override void Setup ()
{
base.Setup ();
Device.PlatformServices = new MockPlatformServices ();
}
[TearDown]
public override void TearDown()
{
base.TearDown();
Application.Current = null;
}
[Test]
public void ApplyUnapplyStyle ()
{
@ -703,5 +710,54 @@ namespace Xamarin.Forms.Core.UnitTests
Assert.AreEqual (Color.Pink, button.TextColor);
Assert.AreEqual (20d, button.FontSize);
}
[Test]
public void ReplacingResourcesDoesNotOverrideManuallySetProperties()
{
var label0 = new Label {
TextColor = Color.Pink
};
var label1 = new Label();
Assume.That(label0.TextColor, Is.EqualTo(Color.Pink));
Assume.That(label1.TextColor, Is.EqualTo(Color.Default));
var rd0 = new ResourceDictionary {
new Style (typeof(Label)) {
Setters = {
new Setter {Property = Label.TextColorProperty, Value = Color.Olive}
}
}
};
var rd1 = new ResourceDictionary {
new Style (typeof(Label)) {
Setters = {
new Setter {Property = Label.TextColorProperty, Value = Color.Lavender}
}
}
};
var mockApp = new MockApplication();
Application.Current = mockApp;
mockApp.Resources = rd0;
var layout = new StackLayout {
Children = {
label0,
label1,
}
};
mockApp.MainPage = new ContentPage { Content = layout};
//Assert.That(label0.TextColor, Is.EqualTo(Color.Pink));
//Assert.That(label1.TextColor, Is.EqualTo(Color.Default));
Assert.That(label0.TextColor, Is.EqualTo(Color.Pink));
Assert.That(label1.TextColor, Is.EqualTo(Color.Olive));
mockApp.Resources = rd1;
Assert.That(label0.TextColor, Is.EqualTo(Color.Pink));
Assert.That(label1.TextColor, Is.EqualTo(Color.Lavender));
}
}
}

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

@ -363,9 +363,10 @@ namespace Xamarin.Forms
value = property.CoerceValue(this, value);
BindablePropertyContext context = GetOrCreateContext(property);
if (manuallySet)
if (manuallySet) {
context.Attributes |= BindableContextAttributes.IsManuallySet;
else
context.Attributes &= ~BindableContextAttributes.IsSetFromStyle;
} else
context.Attributes &= ~BindableContextAttributes.IsManuallySet;
if (fromStyle)

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

@ -63,12 +63,12 @@ namespace Xamarin.Forms
internal void UnApply(BindableObject target, bool fromStyle = false)
{
if (target == null)
throw new ArgumentNullException("target");
throw new ArgumentNullException(nameof(target));
if (Property == null)
return;
object actual = target.GetValue(Property);
if (!fromStyle && !Equals(actual, Value))
if (!Equals(actual, Value) && !(Value is Binding) && !(Value is DynamicResource))
{
//Do not reset default value if the value has been changed
_originalValues.Remove(target);

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

@ -44,10 +44,10 @@ namespace Xamarin.Forms.Xaml.UnitTests
Assert.NotNull (image0);
cell0.BindingContext = new {IsSelected = true};
Assert.AreEqual ("Remove.png", (image0.Source as FileImageSource).File);
Assert.AreEqual ("Remove.png", (image0.Source as FileImageSource)?.File);
cell0.BindingContext = new {IsSelected = false};
Assert.AreEqual ("Add.png", (image0.Source as FileImageSource).File);
Assert.AreEqual ("Add.png", (image0.Source as FileImageSource)?.File);
var cell1 = template.CreateContent () as ViewCell;
Assert.NotNull (cell1);
@ -55,10 +55,10 @@ namespace Xamarin.Forms.Xaml.UnitTests
Assert.NotNull (image1);
cell1.BindingContext = new {IsSelected = true};
Assert.AreEqual ("Remove.png", (image1.Source as FileImageSource).File);
Assert.AreEqual ("Remove.png", (image1.Source as FileImageSource)?.File);
cell1.BindingContext = new {IsSelected = false};
Assert.AreEqual ("Add.png", (image1.Source as FileImageSource).File);
Assert.AreEqual ("Add.png", (image1.Source as FileImageSource)?.File);
}
}
}

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

@ -0,0 +1,26 @@
<?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.Xaml.UnitTests.Bz41048">
<ContentPage.Resources>
<ResourceDictionary>
<!-- Standard Style -->
<Style x:Key="StandardLabelStyle" TargetType="Label">
<Setter Property="TextColor" Value="Red"/>
<Setter Property="LineBreakMode" Value="TailTruncation"/>
</Style>
<!-- Derived style with bold font -->
<Style x:Key="StandarBoldLabelStyle" TargetType="Label" BasedOn="{StaticResource StandardLabelStyle}">
<Setter Property="FontAttributes" Value="Bold"/>
</Style>
<!-- Use the StandardLabelStyle as implicit style for all labels -->
<Style TargetType="Label" BasedOn="{StaticResource StandardLabelStyle}"/>
</ResourceDictionary>
</ContentPage.Resources>
<Label x:Name="label0"
Style="{StaticResource StandarBoldLabelStyle}"
LineBreakMode="WordWrap" />
</ContentPage>

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

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xamarin.Forms;
using Xamarin.Forms.Core.UnitTests;
namespace Xamarin.Forms.Xaml.UnitTests
{
public partial class Bz41048 : ContentPage
{
public Bz41048()
{
InitializeComponent();
}
public Bz41048(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}
[TestFixture]
class Tests
{
[SetUp]
public void Setup()
{
Device.PlatformServices = new MockPlatformServices();
}
[TearDown]
public void TearDown()
{
Application.Current = null;
Device.PlatformServices = null;
}
[TestCase(true)]
[TestCase(false)]
public void StyleDoesNotOverrideValues(bool useCompiledXaml)
{
var layout = new Bz41048(useCompiledXaml);
var label = layout.label0;
Assert.That (label.TextColor, Is.EqualTo(Color.Red));
Assert.That (label.FontAttributes, Is.EqualTo(FontAttributes.Bold));
Assert.That (label.LineBreakMode, Is.EqualTo(LineBreakMode.WordWrap));
}
}
}
}

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

@ -0,0 +1,8 @@
<?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.Xaml.UnitTests.Bz54334">
<StackLayout Padding="10">
<Label x:Name="label" HorizontalTextAlignment="Center" Text="I have a set textcolor, but in since pre-3 styles will override it.." TextColor="Black" />
<Label x:Name="themedLabel" HorizontalTextAlignment="Center" Text="I dont have a set textcolor, syles can override me as much as they want" />
<Button x:Name="btn" Text="Change Theme"></Button>
</StackLayout>
</ContentPage>

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

@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xamarin.Forms;
using Xamarin.Forms.Core.UnitTests;
namespace Xamarin.Forms.Xaml.UnitTests
{
public class Bz54334App : Application
{
bool daymode = true;
public Bz54334App(bool useCompiledXaml)
{
Resources = new ResourceDictionary{
new Style(typeof(Label)) {
Setters = {
new Setter {Property = Label.TextColorProperty, Value=Color.Blue}
}
}
};
MainPage = new Bz54334(useCompiledXaml);
MessagingCenter.Subscribe<ContentPage>(this, "ChangeTheme", (s) => {
ToggleTheme();
});
}
void ToggleTheme()
{
Resources = daymode ? new ResourceDictionary{
new Style(typeof(Label)) {
Setters = {
new Setter {Property = Label.TextColorProperty, Value=Color.Red}
}
}
} : new ResourceDictionary{
new Style(typeof(Label)) {
Setters = {
new Setter {Property = Label.TextColorProperty, Value=Color.Blue}
}
}
};
daymode = !daymode;
}
}
public partial class Bz54334 : ContentPage
{
public Bz54334()
{
InitializeComponent();
}
public Bz54334(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}
[TestFixture]
class Tests
{
[SetUp]
public void Setup()
{
Device.PlatformServices = new MockPlatformServices();
}
[TearDown]
public void TearDown()
{
Application.Current = null;
Device.PlatformServices = null;
}
[TestCase(true)]
[TestCase(false)]
public void Foo(bool useCompiledXaml)
{
var app = Application.Current = new Bz54334App(useCompiledXaml);
var page = app.MainPage as Bz54334;
var l0 = page.label;
var l1 = page.themedLabel;
Assert.That(l0.TextColor, Is.EqualTo(Color.Black));
Assert.That(l1.TextColor, Is.EqualTo(Color.Blue));
MessagingCenter.Send<ContentPage>(page, "ChangeTheme");
Assert.That(l0.TextColor, Is.EqualTo(Color.Black));
Assert.That(l1.TextColor, Is.EqualTo(Color.Red));
MessagingCenter.Send<ContentPage>(page, "ChangeTheme");
Assert.That(l0.TextColor, Is.EqualTo(Color.Black));
Assert.That(l1.TextColor, Is.EqualTo(Color.Blue));
}
}
}
}

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

@ -461,6 +461,12 @@
<Compile Include="Issues\Bz53203.xaml.cs">
<DependentUpon>Bz53203.xaml</DependentUpon>
</Compile>
<Compile Include="Issues\Bz54334.xaml.cs">
<DependentUpon>Bz54334.xaml</DependentUpon>
</Compile>
<Compile Include="Issues\Bz41048.xaml.cs">
<DependentUpon>Bz41048.xaml</DependentUpon>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="..\.nuspec\Xamarin.Forms.Debug.targets" />
@ -842,6 +848,12 @@
<EmbeddedResource Include="Issues\Bz53203.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="Issues\Bz54334.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="Issues\Bz41048.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />