如何为 WkWebViewRenderer 中的文本更改激活暗模式?

How to activate darkmode for the text changed in WkWebViewRenderer?

我试图使用 webView 渲染器调整 Xamarin.iOS 中 webView 中文本的大小。它有效,但我的问题是暗模式不再有效。我已经更改了网页视图的背景,所以显示正确,但我不知道如何处理文本。

  public class NavigationDelegat : WKNavigationDelegate
{
    public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
    {
        string Size = "300%"; 
        string text = String.Format(@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '{0}'", Size);
        
        WKJavascriptEvaluationResult handler = (NSObject result, NSError err) => {
            if (err != null)
            {
                System.Console.WriteLine(err);
            }
            if (result != null)
            {
                System.Console.WriteLine(result);
            }
        };

        webView.EvaluateJavaScript(text, handler);
        webView.Opaque = false;
        webView.BackgroundColor = UIColor.Clear;


    }
}

怎么做?文本始终为黑色,在激活暗模式时也是如此。

您可以尝试使用如下代码:

public class NavigationDelegat : WKNavigationDelegate
{
    public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
    {
        string Size = "300%"; 
        string textSize = String.Format(@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '{0}'", Size);
        string TextColor = "#ffffff"; 
        string textColor = String.Format(@"document.getElementsByTagName('body')[0].style.webkitTextFillColor = '{0}'", TextColor);
        
        WKJavascriptEvaluationResult handler = (NSObject result, NSError err) => {
            if (err != null)
            {
                System.Console.WriteLine(err);
            }
            if (result != null)
            {
                System.Console.WriteLine(result);
            }
        };

        webView.EvaluateJavaScript(textSize , handler);
        webView.EvaluateJavaScript(textColor, handler);
        webView.Opaque = false;
        webView.BackgroundColor = UIColor.Clear;
    }
}

============================更新====== =======================

private class WKwebviewdeleagte : WKNavigationDelegate
{
    public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
    {
        base.DidFinishNavigation(webView, navigation);
        if(UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
        {
            string Size = "300%";
            string TextSize = String.Format(@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '{0}'", Size);
            string TextColor;
            string BackgroundColor;
            if (UITraitCollection.CurrentTraitCollection.UserInterfaceStyle == UIUserInterfaceStyle.Light)
            {
                string textColor = "#666666";
                TextColor = String.Format(@"document.getElementsByTagName('body')[0].style.webkitTextFillColor = '{0}'", textColor);
                string backgroundColor = "\"#FFFFFF\"";
                BackgroundColor = String.Format(@"document.body.style.backgroundColor='{0}'", backgroundColor);
            }
            else
            {
                string textColor = "#ffffff";
                TextColor = String.Format(@"document.getElementsByTagName('body')[0].style.webkitTextFillColor = '{0}'", textColor);
                string backgroundColor = "\"#001A1A\"";
                BackgroundColor = String.Format(@"document.body.style.backgroundColor='{0}'", backgroundColor);
            }

            WKJavascriptEvaluationResult handler = (NSObject result, NSError err) => {
                if (err != null)
                {
                    System.Console.WriteLine(err);
                }
                if (result != null)
                {
                    System.Console.WriteLine(result);
                }
            };

            webView.EvaluateJavaScript(TextSize, handler);
            webView.EvaluateJavaScript(TextColor, handler);
            webView.EvaluateJavaScript(BackgroundColor, handler);
            //webView.Opaque = false;
            //webView.BackgroundColor = UIColor.Clear;
        }
    }
}