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

100 views Asked by At

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>
2

There are 2 answers

0
Andy Donegan On BEST ANSWER

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>
2
Jianwei Sun - MSFT On

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.

Unfortunately it can't do that. I tried to use OnIdiom to modify it on XAML but failed. In addition, I also found this case: In Xamarin, Use OnIdiom with OnIdiom. But it does not have MergedWith for MAUI, also Device.Idiom is deprecated.

For controls, it works well.

You can report it as an issue or a feature request on GitHub.