unoplatform / uno

Build Mobile, Desktop and WebAssembly apps with C# and XAML. Today. Open source and professionally supported.
https://platform.uno
Apache License 2.0
8.76k stars 706 forks source link

Using `XamlControlsResources.MergedDictionaries` is ignored by the resource resolver #6867

Open TopperDEL opened 3 years ago

TopperDEL commented 3 years ago

What would you like to be added:

I would love to see ItemsControl.ItemTemplateProperty being implemented.

Why is this needed:

If not implemented I cannot extract a DataTemplate into a ResourceDictionary - I have to place it at the location where it will be used.

For which Platform:

Anything else we need to know?

jeromelaban commented 3 years ago

Thanks for the report!

This property is implemented, so you may be hitting another scenario that we have not seen. Can you provide a repro of the case where it does not work ?

TopperDEL commented 3 years ago

The docs state that it's not implemented.

I have a DataTemplate with a key in a ResourceDictionary that gets loaded on UWP, but is ignored on Android. Will see if I can create a minimal demo.

TopperDEL commented 3 years ago

I've created a sample showing the problem: https://github.com/TopperDEL/ItemTemplateTest

If you run it on UWP, you get some cards-like items. On Android I get a simple list of strings.

GitHub
GitHub - TopperDEL/ItemTemplateTest
Contribute to TopperDEL/ItemTemplateTest development by creating an account on GitHub.
jeromelaban commented 3 years ago

Thanks for the repro. This is looking like more a resource resolution issue than an ItemTemplate issue. Have you tried placing the item template in the page instead of the app-level resource dictionary?

Also, it looks like the docs are incorrect somehow, because this property really is implemented: https://github.com/unoplatform/uno/blob/976c85c29d17754960d8454384f6cad652ea0bdf/src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs#L209

jeromelaban commented 3 years ago

So the issue really is about resource resolution, and it's related to the way merged dictionaries are declared.

Try using this instead:

<Application
    x:Class="ItemTemplateTest.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ItemTemplateTest"
    xmlns:controls="using:Microsoft.UI.Xaml.Controls">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <controls:XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />

                <!--<ResourceDictionary Source="uplink.NET.UnoHelpers/Resources/DefaultStyles.xaml" />
                <ResourceDictionary Source="uplink.NET.UnoHelpers/Resources/Animations.xaml" />
                <ResourceDictionary Source="uplink.NET.UnoHelpers/Resources/Converters.xaml" />-->
                <ResourceDictionary Source="Resources/MyStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application>

The issue here is caused by the fact that you placed merged dictionaries inside the XamlControlsResources which seems to be ignored by the resources resolver. I'll rename the issue.

TopperDEL commented 3 years ago

Thanks @jeromelaban! I can confirm that this fixes the issue.

The way I added the resources was suggested anywhere - I guess in the Readme of the WCT or something like that.

jeromelaban commented 3 years ago

Interesting. We merged yesterday a contribution that changes the default to be more similar to what I suggested: https://github.com/unoplatform/uno/pull/6884, that'll avoid developers to fall into this trap :)

davidjohnoliver commented 3 years ago

btw ItemTemplateProperty is incorrectly marked NotImplemented because it's currently a field, not a property, which is tripping up the documentation generator. This will be fixed in a future breaking changes roll-up.