In Xamarin, use OnIdiom with OnIdiom

Why doesn't the MergedWith ResourceDictionary attribute support the OnIdiom function? This code works well:

<ResourceDictionary.MergedWith>theme:PhoneTheme</ResourceDictionary.MergedWith>

      

This sets the MergedWith property to null:

<ResourceDictionary.MergedWith>
    <OnIdiom x:TypeArguments="x:String">
        <OnIdiom.Phone>theme:PhoneTheme</OnIdiom.Phone>
        <OnIdiom.Tablet>theme:TabletTheme</OnIdiom.Tablet>
    </OnIdiom>
</ResourceDictionary.MergedWith>

      

Is there any solution to use a different theme between phone idiom and tablet idiom?

+3


source to share


2 answers


You cannot currently do this in XAML, but you can work around it with the codebehind file. I am using xaml and codebehind app to change themes dynamically. Then just do standard inheritance for phones and tablet themes for common colors, styles, etc. Only by overriding what is needed in each specific Iodiom theme.

MyApp.xaml

<Application ...>
    <ResourceDictionary x:Name="appTheme"> <!-- Name the Resource Dict //-->
        ...Any converters, etc
    </ResourceDictionary>
</Application>

      



MyApp.xaml.cs

public partial class MyApp : Application
{
    public MyApp()
    {
        InitializeComponent();
        // Toggle appTheme.MergedWith based on Idiom 
        appTheme.MergedWith = Device.Idiom == TargetIdiom.Phone ? typeof(PhoneTheme) : typeof(TabletTheme);
    }
}

      

+1


source


This isn't support right now, but was coined in the Xamarin forums .



A workaround could be to create separate pages for phone and tablet and depending on which idiom shows one page or the other. You can have your own theme on this page. I think the best thing you can do at this time.

+2


source







All Articles