[C] elements from Source are like items (#13490) fixes #13209

treat elements from the "Source" RD like if they were part of the actual
dictionary, for Count, and Remove
This commit is contained in:
Stephane Delcroix 2021-01-22 13:43:57 +01:00 коммит произвёл GitHub
Родитель b0ec43649c
Коммит 590ac5712d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 65 добавлений и 4 удалений

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

@ -345,7 +345,7 @@ namespace Xamarin.Forms.Core.UnitTests
}
[Test]
public void CountDoesNotIncludeMerged()
public void CountDoesIncludeMerged()
{
var rd = new ResourceDictionary {
{"baz", "Baz"},
@ -353,7 +353,7 @@ namespace Xamarin.Forms.Core.UnitTests
};
rd.MergedWith = typeof(MyRD);
Assert.That(rd.Count, Is.EqualTo(2));
Assert.That(rd.Count, Is.EqualTo(4));
}
[Test]

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

@ -175,7 +175,7 @@ namespace Xamarin.Forms
public int Count
{
get { return _innerDictionary.Count; }
get { return _innerDictionary.Count + (_mergedInstance?.Count ?? 0); }
}
bool ICollection<KeyValuePair<string, object>>.IsReadOnly
@ -230,7 +230,7 @@ namespace Xamarin.Forms
public bool Remove(string key)
{
return _innerDictionary.Remove(key);
return _innerDictionary.Remove(key) || (_mergedInstance?.Remove(key) ?? false);
}
public ICollection<object> Values

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

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" >
<Color x:Key="Color1">Chartreuse</Color>
</ResourceDictionary>

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

@ -0,0 +1,13 @@
<?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.Gh13209">
<StackLayout x:Name="Root">
<StackLayout.Resources>
<ResourceDictionary Source="../AppResources/RD13209.xaml"/>
</StackLayout.Resources>
<Rectangle x:Name="MyRect" BackgroundColor="{StaticResource Color1}" WidthRequest="100" HeightRequest="100" Margin="6"/>
<Label x:Name="MyLabel" WidthRequest="200" HeightRequest="30" Margin="6"/>
</StackLayout>
</ContentPage>

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

@ -0,0 +1,43 @@
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 Gh13209 : ContentPage
{
public Gh13209() => InitializeComponent();
public Gh13209(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() => Device.PlatformServices = null;
[TestCase(true), TestCase(false)]
public void RdWithSource(bool useCompiledXaml)
{
var layout = new Gh13209(useCompiledXaml);
Assert.That(layout.MyRect.BackgroundColor, Is.EqualTo(Color.Chartreuse));
Assert.That(layout.Root.Resources.Count, Is.EqualTo(1));
Assert.That(layout.Root.Resources.MergedDictionaries.Count, Is.EqualTo(0));
Assert.That(layout.Root.Resources["Color1"], Is.Not.Null);
Assert.That(layout.Root.Resources.Remove("Color1"), Is.True);
Assert.Throws<KeyNotFoundException>(()=>
{
var _ = layout.Root.Resources["Color1"];
});
}
}
}
}