Xamarin Forms 对不同的构建配置使用不同的资源字典 [英] Xamarin Forms use different resource dictionaries for different build configurations

查看:32
本文介绍了Xamarin Forms 对不同的构建配置使用不同的资源字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Xamarin Forms 应用程序中为不同的构建配置(调试、发布等...)加载不同的资源字典.

I'm trying to load different resource dictionaries for different build configurations (Debug, Release,etc ....) in my Xamarin Forms app.

我尝试在运行时设置源属性,但它不起作用(为无效的 uri 抛出错误)

I have tried setting the source property at runtime, but it didn't work (throw an error for invalid uri)

public App()
{
        InitializeComponent();
        Device.SetFlags(new string[] { "Expander_Experimental", "Shapes_Experimental" });
        mainResourceDict.Source = new Uri("ResourceDictionaries/WebtrieveRD.xaml");
}

我找不到在 XAML 中添加编译器指令条件的方法.有没有其他方法可以做到这一点?

I couldn't find a way to add compiler directive conditions in XAML. Is there any other way this can be done?

推荐答案

好的,所以我发现 ResourceDictionary 的源只能从 XAML 设置(根据 Xamarin 文档).但它有一个名为 SetAndLoadSource 的方法,它被标记为 供 Xamarin 平台内部使​​用,但仍可用于从后面的代码动态设置源.这是一个用法示例:

Okay, so I figured out that the source for ResourceDictionary can be set only from XAML (As per Xamarin documentation). But it has a method called SetAndLoadSource which is marked as For internal use by Xamarin Platform but can still be used to set the source dynamically from the code behind. Here's a usage example:

#if DEBUG
            var source = new Uri("ResourceDictionaries/DebugRD.xaml", UriKind.RelativeOrAbsolute);
            mainResourceDict.SetAndLoadSource(source, "ResourceDictionaries/DebugRD.xaml", this.GetType().GetTypeInfo().Assembly, null);
#endif

这篇关于Xamarin Forms 对不同的构建配置使用不同的资源字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆