我想對我的 android 應用程式強制執行 lightmode 主題。
我已將這行代碼添加到 app.cs
public partial class App : Application
{
public App()
{
InitializeComponent();
App.Current.UserAppTheme = OSAppTheme.Light;
MainPage = new MainPage();
}
我讀了這個https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/theming/system-theme-changes但不明白我錯過了什么。
uj5u.com熱心網友回復:
分配App.Current.UserAppTheme是不夠的,您必須通過使用 xaml 擴展名的不同控制元件的樣式來定義淺色或/和深色主題,AppThemeBinding如檔案鏈接中的示例所示。
uj5u.com熱心網友回復:
如果您打算讓您的 XF 應用程式跨平臺并且您不會在主題之間切換,您可以通過在自定義頁面渲染器中設定 OverrideUserInterfaceStyle 在 iOS 上強制使用淺色主題:
using TheNameSpace;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomPageRenderer))]
namespace TheNameSpace
{
public class CustomPageRenderer : PageRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
OverrideUserInterfaceStyle = UIUserInterfaceStyle.Light;
}
}
}
uj5u.com熱心網友回復:
我通過將這行代碼放在 Android 專案的 MainActivity.cs 中解決了這個問題
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
將這行代碼直接放在 OnCreate 方法的開頭即可解決問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/449249.html
標籤:xamarin xamarin.forms xamarin.android
