【问题标题】:In Xamarin, Use OnIdiom with OnIdiom在 Xamarin 中,将 OnIdiom 与 OnIdiom 一起使用
【发布时间】:2017-04-05 07:00:07
【问题描述】:

为什么 MergedWith ResourceDictionary 属性不支持 OnIdiom 功能? 此代码运行良好:

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

这一项将 MergedWith 属性设置为 null :

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

有没有办法在手机成语和平板成语之间使用不同的主题?

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms


    【解决方案1】:

    您目前无法在此阶段在 XAML 中执行此操作,但是您可以使用代码隐藏文件解决此问题。我使用 Application xaml 和 codebehind 来动态更改主题。然后只需将 Phone 和 Tablet 主题的标准继承到常见的颜色、样式等。仅覆盖每个 Iodiom 特定主题中需要的内容。

    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);
        }
    }
    

    【讨论】:

    • 这是一个好主意,但合并后的资源字典被认为是动态资源。这对我来说不是很好。
    【解决方案2】:

    目前不支持,但一直是coined on the Xamarin Forums

    一种解决方法可能是为手机和平板电脑创建单独的页面,并取决于成语显示一个页面或另一个页面。在该页面上,您可以拥有自己的主题。我想这是你目前能做的最好的事情。

    【讨论】:

    • 嗯,我已经这样做了,但我想简化成语主题。谢谢你的回答!
    猜你喜欢
    • 2021-04-23
    • 2021-08-10
    • 2017-11-03
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 2019-03-25
    相关资源
    最近更新 更多