dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.11k stars 1.73k forks source link

Resources from a nuget are not available for {StaticResource ...} #10112

Open AndreasReitberger opened 2 years ago

AndreasReitberger commented 2 years ago

Description

I created a MAUI library to manage all my XAML styles. When I add the library project to my app project solution, I can access all resources with `{StaticResource ...}. Once I start typing, it gives me results of all available keys.

Once I pack the library as nuget and install it over the Nuget-Manager, this stops working.

This is the main app.

<?xml version = "1.0" encoding = "UTF-8" ?>
<Application
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:AppBasement"
    xmlns:themes="clr-namespace:AppBasement.Themes"

    x:Class="AppBasement.App"
    >
    <Application.Resources>
        <!-- All styles combined i a ResoureDictionary -->
        <themes:DefaultTheme />
    </Application.Resources>
</Application>

The DefaultTheme (from the app) merges the shared styles.

<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="AppBasement.Themes.DefaultTheme"

    xmlns:shared="clr-namespace:AndreasReitberger.Shared;assembly=SharedMauiXamlStylesLibrary"
    >
    <ResourceDictionary.MergedDictionaries>
        <shared:DefaultTheme/>
        <!-- Override colors -->
        <ResourceDictionary Source="/Themes/DefaultColors.xaml" />
        <!-- Item Templates -->
        <ResourceDictionary Source="/Themes/ItemTemplates/ListViewItemTemplates.xaml" />
        <!-- Additional Control Styles -->
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfBusyIndicator.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

And this the styles coming from the library.

<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="AndreasReitberger.Shared.DefaultTheme"
    >
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="SharedColors.xaml" />
        <ResourceDictionary Source="/Themes/SharedIcons.xaml" />
        <ResourceDictionary Source="/Themes/SharedFonts.xaml" />

        <ResourceDictionary Source="/Themes/Controls/ActivityIndicator.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Border.xaml" />
        <ResourceDictionary Source="/Themes/Controls/BoxView.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Button.xaml" />
        <ResourceDictionary Source="/Themes/Controls/CheckBox.xaml" />
        <ResourceDictionary Source="/Themes/Controls/DatePicker.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Editor.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Entry.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Label.xaml" />
        <ResourceDictionary Source="/Themes/Controls/ListView.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Page.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Picker.xaml" />
        <ResourceDictionary Source="/Themes/Controls/ProgressBar.xaml" />
        <ResourceDictionary Source="/Themes/Controls/RadioButton.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Grid.xaml" />
        <ResourceDictionary Source="/Themes/Controls/IndicatorView.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Frame.xaml" />
        <ResourceDictionary Source="/Themes/Controls/RefreshView.xaml" />
        <ResourceDictionary Source="/Themes/Controls/SearchBar.xaml" />
        <ResourceDictionary Source="/Themes/Controls/SearchHandler.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Shadow.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Shell.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Slider.xaml" />
        <ResourceDictionary Source="/Themes/Controls/StackLayout.xaml" />
        <ResourceDictionary Source="/Themes/Controls/SwipeItem.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Switch.xaml" />
        <ResourceDictionary Source="/Themes/Controls/TimePicker.xaml" />

        <!-- Item Templates -->
        <ResourceDictionary Source="/Themes/ItemTemplates/ListViewItemTemplates.xaml" />
        <!--
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfAutoComplete.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfBadgeView.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfBorder.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfBusyIndicator.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfButtons.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfCardView.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfCharts.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfCircularProgressBar.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfCircularGauge.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfComboBoxes.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfGradientView.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfListView.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfMaskedEdit.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfNumericTextInputs.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfNumericUpDowns.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfPicker.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfPullToRefresh.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfRotator.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfRangeSlider.xaml" />

        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfTextInputLayout.xaml" />
        -->
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfBadgeView.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfBusyIndicator.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfListView.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfRadialGauge.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfSlider.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfRangeSlider.xaml" />
        <ResourceDictionary Source="/Themes/Controls/Syncfusion/SfTabView.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

With adding the project directly. image

Added as nuget: image

I also noticed, that the same Keys does not get overrided. For instance, the shared theme uses "PrimaryColor" as key. This is overriden in the app theme (DefaultColors). However it takes always the color set in the shared library. I already tried to change the position of the directories, however no changes.

Steps to Reproduce

  1. Create a styles library and add it as nuget to the project

Link to public reproduction project repository

https://github.com/AndreasReitberger/SharedMauiXamlStyles

Version with bug

6.0.486 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows

Affected platform versions

All

Did you find any workaround?

Add as project to the app project and link to it.

Relevant log output

No response

PureWeen commented 2 years ago

@StephaneDelcroix thoughts?

AndreasReitberger commented 2 years ago

Any news on this @PureWeen @StephaneDelcroix ? Is this a bug or some problems of my nuget project? Thank you!

AndreasReitberger commented 1 year ago

Still seeing this issues. Also with other maui libraries. Any news on this? @PureWeen @StephaneDelcroix

AndreasReitberger commented 1 year ago

@PureWeen @StephaneDelcroix any news on this?

HavenDV commented 1 year ago

I also noticed, that the same Keys does not get overrided. For instance, the shared theme uses "PrimaryColor" as key. This is overriden in the app theme (DefaultColors). However it takes always the color set in the shared library. I already tried to change the position of the directories, however no changes.

I'm not sure, but try reordering the resources. https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/resource-dictionaries?view=net-maui-7.0#merge-resource-dictionaries-from-other-assemblies

The resources contained in the resource dictionaries that were merged via the MergedDictionaries collection, in the reverse order they are listed in the MergedDictionaries property.

Order like this works for me and overrides resources from default theme:

        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <themes:DefaultTheme />

                <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

But in general, I think the problem is with the IDE, not MAUI

AndreasReitberger commented 1 year ago

Hi @HavenDV Thanks for the example. If I reorder the ResourceDictionary in my DefaultTheme, there is no change of the described behaviour. All Red stuff should be actually Purple

image

image

However, if I remove the DefaultColors.xaml from the DefaultTheme.xaml and move it directly to the App.Resources and keep the order you suggest, it works on my end.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <themes:DefaultTheme />
            <ResourceDictionary Source="/Themes/DefaultColors.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

image

But Intelisense still doesn't find the keys of the Styles, however the IDE marks keys with are wrong / not available.

image

It only doesn't suggest it while typing.

Zhanglirong-Winnie commented 1 year ago

Verified this issue with Visual Studio Enterprise 17.8.0 Preview 1.0. Can repro on android platform with sample project. SharedMauiXamlStyles-main.zip Screenshot 2023-08-23 134434

haavamoa commented 11 months ago

As far as I know, this has been an issue in Xamarin and is still an issue in MAUI.

We’ve created an library that we want to reuse for all of our apps that are in production. This library has resources like icons, sizes, animations etc. All of theese resources can be StaticResources, but because of this bug, the developers get no intellisense. Which is no good!

We’ve taken advantage of MarkupExtensions for each resource type, and mapped a dictionary of resources with keys to enum values. Heres our icons API:

https://github.com/DIPSAS/DIPS.Mobile.UI/blob/main/src/library/DIPS.Mobile.UI/Resources/Icons/IconsExtension.cs

And here is how our developers use it: https://github.com/DIPSAS/DIPS.Mobile.UI/wiki/Icons

We’ve done it this way for both Xamarin and MAUI, which feels weird, but at the same time it works. 👍🏻🤷‍♂️

AndreasReitberger commented 5 days ago

Any news on this?