xamarin.forms - 在使用最新版本的 Xamarin Forms 进行的开发中替换 Xamarin.Forms.Theme.Dark
问题描述
Xamarin.Forms.Theme.Dark是为 Xamarin Forms 应用程序(Android 和 iOS)设置深色外观的好方法。显然,该项目不再受支持。虽然它仍然适用于 4.2 版,但我正在寻找替代解决方案。据我了解 Xamarin Forms 的新“主题”可能性,必须在每个对象的 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 的所有内容,但黑色样式的呈现是相同的。
推荐阅读
- jquery - Session Timeout 通过jquery全局绑定函数
- c++ - 使用 C++ 中的 remove 函数删除用户指定的文件
- python-3.x - 指数符号不在 seaborn 或 matplotlib 中绘制
- javascript - 添加滚动操作时避免性能问题
- algorithm - 在有一些限制的情况下在数线上移动汽车。查找无法到达的号码以及到达特定号码的最佳方式
- vb.net - 插入语句中的语法错误有什么问题
- ruby-on-rails - 带有设计的 Rails 中的 Auth0 在 request.env['omniauth.auth'] 中为零
- css - 在 Vuetify 的卡片组件中对齐行
- javascript - 用 HTML 链接替换字符串模板
- php - 如何使用 sql 请求即时转换 sql 表中的 dd.mm.yyyy 日期并获取小于