Search code examples
mauiresourcedictionary

Maui App.xaml load between two ResourceDictionary files based on "OnIdiom" for Tablet or Phone


I have two Resource Dictionaries one for Mobile and one for Tablet and I would like to merge the correct one within App.xaml using OnIdiom. With OnPlatform it is straight forward, but I am not seeing a way to do the same with OnIdiom to check if it is Tablet and then load the Tablet file or Phone file etc.

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
                <ResourceDictionary Source="Resources/Styles/Margins.xaml" />
                <!-- The two files below I would like to use OnIdiom to select between Tablet or Mobile -->
                <ResourceDictionary Source="Resources/Styles/FixedSizesMobile.xaml"/>
                <ResourceDictionary Source="Resources/Styles/FixedSizesTablet.xaml"/>
                <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

Solution

  • Thanks to a fellow Discord Member, he pointed me in the right direction, I was over complicating it trying to follow OnPlatform functionality.

    This Merges based on OnIdiom and is working nicely.

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
                <ResourceDictionary Source="Resources/Styles/Margins.xaml" />
                <ResourceDictionary Source='{OnIdiom Phone="Resources/Styles/FixedSizesPhone.xaml"}' />
                <ResourceDictionary Source='{OnIdiom Tablet="Resources/Styles/FixedSizesTablet.xaml"}' />
                <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>