Skip to content

Commit

Permalink
fix(XamlFileGenerator): Generate literal properties for ResourceDicti…
Browse files Browse the repository at this point in the history
…onary subclasses
  • Loading branch information
kazo0 committed Jun 4, 2021
1 parent aea1019 commit f5b7e4b
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,10 @@ private void InitializeAndBuildResourceDictionary(IIndentedStringBuilder writer,
if (IsResourceDictionarySubclass(topLevelControl.Type))
{
var type = GetType(topLevelControl.Type);
writer.AppendLineInvariant("new {0}()", GetGlobalizedTypeName(type.ToDisplayString()));
using (writer.BlockInvariant("new {0}()", GetGlobalizedTypeName(type.ToDisplayString())))
{
BuildLiteralProperties(writer, topLevelControl);
}
}
else
{
Expand Down Expand Up @@ -2394,7 +2397,10 @@ private void RegisterAndBuildResources(IIndentedStringBuilder writer, XamlObject
writer.AppendLineInvariant("Resources = ");

var type = GetType(rdSubclass.Type);
writer.AppendLineInvariant("new {0}()", GetGlobalizedTypeName(type.ToDisplayString()));
using (writer.BlockInvariant("new {0}()", GetGlobalizedTypeName(type.ToDisplayString())))
{
BuildLiteralProperties(writer, rdSubclass);
}
writer.AppendLineInvariant(isInInitializer ? "," : ";");
}
else if (resourcesRoot != null || mergedDictionaries != null || themeDictionaries != null)
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI.FluentTheme/themeresources.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21626,4 +21626,4 @@
</Setter>
</Style>
<Style TargetType="controls:TwoPaneView" BasedOn="{StaticResource DefaultTwoPaneViewStyle}" />
</ResourceDictionary>
</ResourceDictionary>
1 change: 1 addition & 0 deletions src/Uno.UI.Tests/App/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
Color="CadetBlue" />
</ResourceDictionary>
<local:Subclassed_Dictionary />
<local:Subclassed_Dictionary_With_Property Test="Test123"/>
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ResourceDictionary x:Class="Uno.UI.Tests.App.Xaml.Subclassed_Dictionary_With_Property"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

</ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Uno.UI.Tests.App.Views;
using Uno.UI.Tests.ViewLibrary;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Networking.Sockets;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace Uno.UI.Tests.App.Xaml
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class Subclassed_Dictionary_With_Property : ResourceDictionary
{
public string Test
{
get => (string)GetValue(TestProperty);
set => SetValue(TestProperty, value);
}

public static DependencyProperty TestProperty { get; } =
DependencyProperty.Register(
nameof(Test),
typeof(string),
typeof(Subclassed_Dictionary_With_Property),
new PropertyMetadata(OnPropertyChanged));

private static void OnPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{
if (dependencyObject is Subclassed_Dictionary_With_Property rd)
{
if (!rd.ContainsKey("TestKey"))
{
rd.Add("TestKey", args.NewValue as string);
}
else
{
rd["TestKey"] = args.NewValue as string;
}
}
}

public Subclassed_Dictionary_With_Property()
{
this.InitializeComponent();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<UserControl
x:Class="Uno.UI.Tests.App.Xaml.Test_Control_With_Subclassed_ResourceDictionary_With_Custom_Property"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Uno.UI.Tests.App.Xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<UserControl.Resources>
<local:Subclassed_Dictionary_With_Property Test="Test123"/>
</UserControl.Resources>
<Grid>

</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

namespace Uno.UI.Tests.App.Xaml
{
public sealed partial class Test_Control_With_Subclassed_ResourceDictionary_With_Custom_Property : UserControl
{
public Test_Control_With_Subclassed_ResourceDictionary_With_Custom_Property()
{
this.InitializeComponent();
}
}
}
1 change: 0 additions & 1 deletion src/Uno.UI.Tests/Uno.UI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@
<ItemGroup>
<UpToDateCheckInput Include="**\*.xaml" Exclude="bin\**\*.xaml;obj\**\*.xaml" />
</ItemGroup>

<ItemGroup>
<None Update="ResourceLoader\Controls\When_Collection_And_InlineProperty.xaml">
<Generator>MSBuild:Compile</Generator>
Expand Down
23 changes: 23 additions & 0 deletions src/Uno.UI.Tests/Windows_UI_Xaml/Given_ResourceDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -772,5 +772,28 @@ public void When_Source_And_Globbing_From_Included_File()
button.Style = style;
Assert.AreEqual(new Thickness(99, 33, 7, 7), button.Margin);
}

[TestMethod]
public void When_Custom_Resource_Dictionary_With_Custom_Property()
{
var app = UnitTestsApp.App.EnsureApplication();

var rd = app.Resources.MergedDictionaries.FirstOrDefault(x => x is Subclassed_Dictionary_With_Property);

Assert.IsNotNull(rd);
Assert.IsTrue(rd.ContainsKey("TestKey"));
Assert.AreEqual(rd["TestKey"], "Test123");
}

[TestMethod]
public void When_Custom_Resource_Dictionary_With_Custom_Property_in_Custom_Control()
{
var ctrl = new Test_Control_With_Subclassed_ResourceDictionary_With_Custom_Property();
var resources = ctrl.Resources;

Assert.IsNotNull(resources);
Assert.IsTrue(resources.ContainsKey("TestKey"));
Assert.AreEqual(resources["TestKey"], "Test123");
}
}
}

0 comments on commit f5b7e4b

Please sign in to comment.