C# 是否有一种方法可以简化向Xamarin中的资源字典添加字符串、对象,以便使用列表或数组来完成此操作?
当我试图设置资源字典时,有没有一种方法可以将多个设置语句组合成一个 下面是我现在正在做的一个例子:C# 是否有一种方法可以简化向Xamarin中的资源字典添加字符串、对象,以便使用列表或数组来完成此操作?,c#,xamarin,C#,Xamarin,当我试图设置资源字典时,有没有一种方法可以将多个设置语句组合成一个 下面是我现在正在做的一个例子: Application.Current.Resources[nameof(Fonts.CarouselHeadingFontSize)] = 24; Application.Current.Resources[nameof(Fonts.CarouselSubHeadingFontSize)] = 36; Application.Current.Resources[nameof(Fonts.Cont
Application.Current.Resources[nameof(Fonts.CarouselHeadingFontSize)] = 24;
Application.Current.Resources[nameof(Fonts.CarouselSubHeadingFontSize)] = 36;
Application.Current.Resources[nameof(Fonts.ContentLabelTextFontSize)] = 13.5 * Fs;
我所做的是创建一个方法来设置:
private void SetResource(string keyName, object v)
{
Application.Current.Resources[keyName] = v;
}
像这样使用它:
SetResource(nameof(Fonts.ButtonLinkLabelTextFontSize), 15 * Ms);
SetResource(nameof(Fonts.CarouselFooterFontSize), 16);
SetResources((nameof(Fonts.ButtonLinkLabelTextFontSize), 15 * Ms),
(nameof(Fonts.CarouselFooterFontSize), 16));
但是,有没有一种方法可以进一步简化此设置,使其如下所示:
SetResource(nameof(Fonts.ButtonLinkLabelTextFontSize), 15 * Ms);
SetResource(nameof(Fonts.CarouselFooterFontSize), 16);
SetResources((nameof(Fonts.ButtonLinkLabelTextFontSize), 15 * Ms),
(nameof(Fonts.CarouselFooterFontSize), 16));
请注意,资源字典是
IDictionary<string, object>
IDictionary
您可以将元组与参数结合使用
:
private void SetResources(参数(字符串、对象)[]资源)
{
foreach(资源中的var(键、值)
{
Application.Current.Resources[key]=值;
}
}
然后您可以这样称呼它(在您的示例中是如何称呼它的):
SetResources((字体名称(Fonts.ButtonLinkLabelTextFontSize),15*Ms),
(字体名称(Fonts.CarouselFooterFontSize),16);
您可以将元组与参数结合使用
:
private void SetResources(参数(字符串、对象)[]资源)
{
foreach(资源中的var(键、值)
{
Application.Current.Resources[key]=值;
}
}
然后您可以这样称呼它(在您的示例中是如何称呼它的):
SetResources((字体名称(Fonts.ButtonLinkLabelTextFontSize),15*Ms),
(字体名称(Fonts.CarouselFooterFontSize),16);