Xamarin.Forms.Theme.Dark是为Xamarin Forms应用程序(Android和iOS(设置深色外观的好方法。显然,此项目不再受支持。虽然它仍然适用于 4.2 版,但我正在寻找替代解决方案。据我了解 Xamarin 窗体的新"主题"可能性,必须在每个对象的 XAML 代码中使用主题。 是否还有在"应用程序级别"转换样式的解决方案?
此代码应答上述请求:
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
namespace M...
{
internal static class DarkTheme
{
internal static ResourceDictionary getResources()
{
var ret = new ResourceDictionary();
ret.Add(new Style(typeof(TemplatedPage))
{
ApplyToDerivedTypes = true,
CanCascade = true,
Setters = { new Setter { Property = TemplatedPage.BackgroundColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0.1882353, 0.1882353, 0.1882353, 1.0), Color.FromRgba(0.09019608, 0.09019608, 0.09019608, 1.0)) } }
});
ret.Add(new Style(typeof(NavigationPage))
{
ApplyToDerivedTypes = true,
CanCascade = true,
Setters = {
new Setter { Property = NavigationPage.BarTextColorProperty, Value = iifPlatform<Color>(Color.FromRgba(1.0, 1.0, 1.0, 1.0), Color.FromRgba(0d, 0.47843137, 1.0, 1.0)) } ,
new Setter { Property = NavigationPage.BarBackgroundColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0.12941177, 0.12941177, 0.12941177, 1), Color.FromRgba(0.09019608, 0.09019608, 0.09019608, 1.0)) } ,
}
});
ret.Add(new Style(typeof(Xamarin.Forms.TabbedPage))
{
ApplyToDerivedTypes = true,
CanCascade = true,
Setters = {
new Setter { Property = TabbedPage.BarTextColorProperty, Value = iifPlatform<Color>(Color.FromRgba(1.0, 1.0, 1.0, 1.0), Color.FromRgba(0d, 0.47843137, 1.0, 1.0)) } ,
new Setter { Property = TabbedPage.BarBackgroundColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0.12941177, 0.12941177, 0.12941177, 1), Color.FromRgba(0.09019608, 0.09019608, 0.09019608, 1.0)) } ,
}
});
ret.Add(new Style(typeof(Xamarin.Forms.Button))
{
ApplyToDerivedTypes = true,
CanCascade = true,
Setters = {
new Setter { Property = Button.FontSizeProperty, Value = iifPlatform<int>(16, 18) } ,
new Setter { Property = Button.FontAttributesProperty, Value = iifPlatform<int>(0, 1) } ,
new Setter { Property = Button.CornerRadiusProperty, Value = iifPlatform<int>(0, 6) } ,
new Setter { Property = Button.BorderWidthProperty, Value = 0 } ,
new Setter { Property = Button.BackgroundColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0.46666667, 0.46666667, 0.46666667, 1.0), Color.FromRgba(1.0, 1.0, 1.0, 0d)) } ,
new Setter { Property = Button.BorderColorProperty, Value = Color.FromRgba(0.2, 0.2, 0.2, 1.0) } ,
new Setter { Property = Button.TextColorProperty, Value = iifPlatform<Color>(Color.White, Color.FromRgba(0, 0.47843137, 1.0, 1.0)) } ,
}
});
ret.Add(new Style(typeof(Xamarin.Forms.Entry))
{
ApplyToDerivedTypes = true,
CanCascade = true,
Setters = {
new Setter { Property = Entry.TextColorProperty, Value = Color.FromRgba(0.5921569, 0.5921569, 0.5921569, 1.0) } ,
new Setter { Property = Entry.PlaceholderColorProperty, Value = Color.FromRgba(0.5921569, 0.5921569, 0.5921569, 1.0) } ,
new Setter { Property = Entry.BackgroundColorProperty, Value = iifPlatform<Color>(Color.White, Color.FromRgba(0.2509804, 0.2509804, 0.2509804, 1.0)) } ,
}
});
ret.Add(new Style(typeof(Xamarin.Forms.Editor))
{
ApplyToDerivedTypes = true,
CanCascade = true,
Setters = {
new Setter { Property = Editor.TextColorProperty, Value = Color.FromRgba(0.5921569, 0.5921569, 0.5921569, 1.0) } ,
new Setter { Property = Editor.BackgroundColorProperty, Value = iifPlatform<Color>(Color.White, Color.FromRgba(0.2509804, 0.2509804, 0.2509804, 1.0)) } ,
}
});
ret.Add(new Style(typeof(Xamarin.Forms.Frame))
{
ApplyToDerivedTypes = true,
CanCascade = true,
Setters = {
new Setter { Property = Frame.BorderColorProperty, Value = Color.FromRgba(0.28627452, 0.28627452, 0.28627452, 1.0) } ,
new Setter { Property = Frame.HasShadowProperty, Value = false } ,
new Setter { Property = Frame.BackgroundColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0.92941177, 0.92941177, 0.93333334, 1), Color.FromRgba(0.12156863, 0.12156863, 0.12156863, 1.0)) } ,
}
});
ret.Add(new Style(typeof(Xamarin.Forms.SearchBar))
{
ApplyToDerivedTypes = true,
CanCascade = true,
Setters = { new Setter { Property = SearchBar.TextColorProperty, Value = Color.FromRgba(1.0, 0d, 0d, 1.0) } }
});
ret.Add(new Style(typeof(Xamarin.Forms.Label))
{
ApplyToDerivedTypes = true,
CanCascade = true,
Setters = {
new Setter { Property = Label.TextColorProperty, Value = Color.FromRgba(0.5921569, 0.5921569, 0.5921569, 1.0) } ,
new Setter { Property = Label.FontSizeProperty, Value = 15 } ,
}
});
ret.Add(new Style(typeof(Xamarin.Forms.ImageCell))
{
ApplyToDerivedTypes = true,
CanCascade = true,
Setters = {
new Setter { Property = ImageCell.TextColorProperty, Value = Color.FromRgba(1.0, 1.0, 1.0, 1.0) } ,
new Setter { Property = ImageCell.DetailColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0.12156863, 0.68235296, 0.80784315, 1.0), Color.FromRgba(0, 0.47843137, 1.0, 1.0)) } ,
}
});
ret.Add(new Style(typeof(Xamarin.Forms.TextCell))
{
ApplyToDerivedTypes = true,
CanCascade = true,
Setters = {
new Setter { Property = TextCell.TextColorProperty, Value = Color.FromRgba(1.0, 1.0, 1.0, 1.0) } ,
new Setter { Property = TextCell.DetailColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0.12156863, 0.68235296, 0.80784315, 1.0), Color.FromRgba(0d, 0.47843137, 1.0, 1.0)) } ,
}
});
ret.Add(new Style(typeof(Xamarin.Forms.EntryCell))
{
ApplyToDerivedTypes = true,
CanCascade = true,
Setters = { new Setter { Property = EntryCell.LabelColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0.12156863, 0.68235296, 0.80784315, 1.0), Color.FromRgba(0d, 0.47843137, 1.0, 1.0)) } }
});
ret.Add(new Style(typeof(Xamarin.Forms.ListView))
{
ApplyToDerivedTypes = true,
CanCascade = true,
Setters = {
new Setter { Property = ListView.BackgroundColorProperty, Value = iifPlatform<Color>(Color.FromRgba(1.0, 1.0, 1.0, 0d), Color.FromRgba(0.10980392, 0.10980392, 0.11372549, 1)) } ,
new Setter { Property = ListView.SeparatorColorProperty, Value = Color.FromRgba(0.28627452, 0.28627452, 0.28627452, 1.0) } ,
new Setter { Property = ListView.SeparatorVisibilityProperty, Value = 0 } ,
}
});
ret.Add(new Style(typeof(Xamarin.Forms.ActivityIndicator))
{
ApplyToDerivedTypes = true,
CanCascade = true,
Setters = { new Setter { Property = ActivityIndicator.ColorProperty, Value = iifPlatform<Color>(Color.FromRgba(0d, 0d, 0d, 0d), Color.FromRgba(0d, 0.47843137, 0d, 1.0)) } }
});
return ret;
}
private static T iifPlatform<T>(T androidValue, T iosValue)
{
switch (Device.RuntimePlatform)
{
case Device.iOS:
return iosValue;
default:
return androidValue;
}
}
}
}
它应该在 App.xaml 中调用.cs使用以下代码行:Resources = DarkTheme.getResources((;
这不包括 Xamarin.Forms.Theme.Dark 的所有内容.dll但黑色样式的呈现是相同的。