有没有办法根据浅色模式和暗模式动态设置iOS中EntryCell的背景?



我正在尝试在我的应用程序中支持暗模式和浅色模式。它完美地工作,除了与入口单元格。似乎它有自己的背景,我尝试动态设置入口单元格背景,但这不起作用。它不与动态资源做出反应。我不走运地尝试动态设置 EntryCell 的背景颜色。现在我只能设置为黑色或白色背景。有什么想法吗? 这是我的:

这是我的页面渲染器


[assembly: ExportRenderer(typeof(ContentPage), typeof(Mobile.Base.iOS.PageRenderer))]
namespace Mobile.Base.iOS
{
public class PageRenderer : Xamarin.Forms.Platform.iOS.PageRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
try
{
SetAppTheme();
}
catch (Exception ex)
{
Console.WriteLine($"tttERROR: {ex.Message}");
}
}
public override void TraitCollectionDidChange(UITraitCollection previousTraitCollection)
{
base.TraitCollectionDidChange(previousTraitCollection);
Console.WriteLine($"TraitCollectionDidChange: {TraitCollection.UserInterfaceStyle} != {previousTraitCollection.UserInterfaceStyle}");
if (previousTraitCollection != null && TraitCollection.UserInterfaceStyle != previousTraitCollection.UserInterfaceStyle)
{
SetAppTheme();
}

}
private void SetAppTheme()
{
if (TraitCollection.UserInterfaceStyle == UIUserInterfaceStyle.Dark)
{
if (App.AppTheme == "dark")
return;
//Add a Check for App Theme since this is called even when not changed really
App.Current.Resources = new DarkTheme();
App.AppTheme = "dark";
}
else
{
if (App.AppTheme != "dark")
return;
App.Current.Resources = new LightTheme();
App.AppTheme = "light";
}
}
}
}

这是我的自定义入口单元格渲染器


namespace Mobile.Base.iOS
{
public class customEntryCell : EntryCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var nativeCell = (EntryCell)item;
var cell = base.GetCell(nativeCell, reusableCell, tv);
((UITextField)cell.Subviews[0].Subviews[0]).BackgroundColor = UIColor.White;
return cell;
}
}
}

您可以通过更改自定义渲染器中的cell.ContentView.BackgroundColor来更改Entrycell's backgroundColor

public class myEntryCelliOSCellRenderer : EntryCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var nativeCell = (EntryCell)item;
var cell = base.GetCell(nativeCell, reusableCell, tv);
((UITextField)cell.Subviews[0].Subviews[0]).TextColor = UIColor.Orange;
((UITextField)cell.Subviews[0].Subviews[0]).BackgroundColor = UIColor.Green;
//Change the entrycell backgroundColor
cell.ContentView.BackgroundColor = UIColor.Red;
return cell;
}
}

这是您可以检查的EntryCellRenderer的源代码。

最新更新