[C] Allow Multiple Shared RD. (#103)
authorStephane Delcroix <stephane@delcroix.org>
Mon, 18 Apr 2016 16:25:20 +0000 (18:25 +0200)
committerRui Marinho <me@ruimarinho.net>
Mon, 18 Apr 2016 16:25:20 +0000 (17:25 +0100)
Xamarin.Forms.Core/ResourceDictionary.cs
Xamarin.Forms.Xaml.UnitTests/SharedResourceDictionary2.xaml [new file with mode: 0644]
Xamarin.Forms.Xaml.UnitTests/SharedResourceDictionary2.xaml.cs [new file with mode: 0644]
Xamarin.Forms.Xaml.UnitTests/TestSharedResourceDictionary.xaml
Xamarin.Forms.Xaml.UnitTests/TestSharedResourceDictionary.xaml.cs
Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj

index e5e08bc..7460a90 100644 (file)
@@ -26,10 +26,17 @@ namespace Xamarin.Forms
                        }
                }
 
-               static ResourceDictionary _instance;
+               static Dictionary<Type, ResourceDictionary> _instances;
                static ResourceDictionary GetInstance(Type type)
                {
-                       return _instance ?? (_instance = ((ResourceDictionary)Activator.CreateInstance (type)));
+                       _instances = _instances ?? new Dictionary<Type, ResourceDictionary>();
+                       ResourceDictionary rd;
+                       if (!_instances.TryGetValue(type, out rd))
+                       {
+                               rd = ((ResourceDictionary)Activator.CreateInstance(type));
+                               _instances [type] = rd;
+                       }
+                       return rd;
                }
 
                ResourceDictionary _mergedInstance;
diff --git a/Xamarin.Forms.Xaml.UnitTests/SharedResourceDictionary2.xaml b/Xamarin.Forms.Xaml.UnitTests/SharedResourceDictionary2.xaml
new file mode 100644 (file)
index 0000000..24a6ecd
--- /dev/null
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Xamarin.Forms.Xaml.UnitTests.SharedResourceDictionary2">
+       <Style x:Key="sharedStyle2" TargetType="Label">
+               <Setter Property="TextColor" Value="Purple"/>
+       </Style>
+</ResourceDictionary>
diff --git a/Xamarin.Forms.Xaml.UnitTests/SharedResourceDictionary2.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/SharedResourceDictionary2.xaml.cs
new file mode 100644 (file)
index 0000000..dab1f1a
--- /dev/null
@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+
+using Xamarin.Forms;
+
+namespace Xamarin.Forms.Xaml.UnitTests
+{
+       public partial class SharedResourceDictionary2 : ResourceDictionary
+       {
+               public SharedResourceDictionary2 ()
+               {
+                       InitializeComponent ();
+               }
+       }
+}
index eb1eb7e..c87982f 100644 (file)
@@ -3,9 +3,19 @@
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                xmlns:local="clr-namespace:Xamarin.Forms.Xaml.UnitTests"
                x:Class="Xamarin.Forms.Xaml.UnitTests.TestSharedResourceDictionary">
-       <ContentPage.Resources>
-               <ResourceDictionary MergedWith="local:SharedResourceDictionary">
-               </ResourceDictionary>
-       </ContentPage.Resources>
-       <Label x:Name="label" Style="{StaticResource sharedfoo}"/>
+       <StackLayout>
+               <ContentView>
+                       <ContentView.Resources>
+                               <ResourceDictionary MergedWith="local:SharedResourceDictionary"/>
+                       </ContentView.Resources>
+                       <Label x:Name="label" Style="{StaticResource sharedfoo}"/>
+               </ContentView>
+               <ContentView>
+                       <ContentView.Resources>
+                               <ResourceDictionary MergedWith="local:SharedResourceDictionary2"/>
+                       </ContentView.Resources>
+                       <Label x:Name="label2" Style="{StaticResource sharedStyle2}"/>
+               </ContentView>
+       </StackLayout>
+
 </ContentPage>
\ No newline at end of file
index dff3f89..e12e83b 100644 (file)
@@ -26,6 +26,15 @@ namespace Xamarin.Forms.Xaml.UnitTests
                                var layout = new TestSharedResourceDictionary (useCompiledXaml);
                                Assert.AreEqual (Color.Pink, layout.label.TextColor);
                        }
+
+                       [TestCase (false)]
+                       [TestCase (true)]
+                       public void NoConflictsBetweenSharedRDs (bool useCompiledXaml)
+                       {
+                               var layout = new TestSharedResourceDictionary (useCompiledXaml);
+                               Assert.AreEqual (Color.Pink, layout.label.TextColor);
+                               Assert.AreEqual (Color.Purple, layout.label2.TextColor);
+                       }
                }
        }
 }
\ No newline at end of file
index 54433f5..9c4ba82 100644 (file)
     <Compile Include="TestSharedResourceDictionary.xaml.cs">
       <DependentUpon>TestSharedResourceDictionary.xaml</DependentUpon>
     </Compile>
+    <Compile Include="SharedResourceDictionary2.xaml.cs">
+      <DependentUpon>SharedResourceDictionary2.xaml</DependentUpon>
+    </Compile>
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <Import Project="..\.nuspec\Xamarin.Forms.Debug.targets" />
     <EmbeddedResource Include="TestSharedResourceDictionary.xaml">
       <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
     </EmbeddedResource>
+    <EmbeddedResource Include="SharedResourceDictionary2.xaml">
+      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
+    </EmbeddedResource>
   </ItemGroup>
   <ItemGroup>
     <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />