應用于我的 xamarin android 專案的默認主題在物理手機上的顯示與在模擬手機上的不同。
除了一些針對 android 的覆寫之外,我沒有更改任何樣式。
據我所知,控制元件已顯示并可使用,只有顏色錯誤。
我還通過 APK 發布了該應用程式并安裝了它。所以沒有附加除錯器或任何東西,只是簡單的安裝。雖然結果相同。
模擬:

身體的:

這是我的登錄視圖:
<ContentPage.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Margin" Value="4,0,0,0" />
</Style>
</ContentPage.Resources>
<ContentPage.BindingContext>
<vm:LoginViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
<Grid Padding="20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackLayout>
<Image Source="teleport_foundation_logo.jpg"
Margin="0, 20, 0, 50" />
</StackLayout>
<StackLayout Grid.Row="1"
VerticalOptions="StartAndExpand">
<Label Text="{Binding UsernameTitle}" />
<Entry Text="{Binding Username}"/>
<Label Text="{Binding PasswordTitle}" />
<Entry IsPassword="True"
Text="{Binding Password}" />
<StackLayout Orientation="Horizontal"
HorizontalOptions="StartAndExpand">
<CheckBox IsChecked="{Binding SaveUsername}" />
<Label Text="Remember username"
VerticalOptions="Center"/>
</StackLayout>
<StackLayout Orientation="Horizontal">
<CheckBox IsChecked="{Binding ShowAdvanced}" />
<Label Text="Advanced options"
VerticalOptions="Center" />
</StackLayout>
<Grid IsVisible="{Binding ShowAdvanced}"
Margin="0, 10, 0, 0">
<StackLayout>
<Label Text="Server Address" />
<Entry Text="{Binding ServerAddress}" />
</StackLayout>
</Grid>
</StackLayout>
<Button VerticalOptions="Center"
Grid.Row="2"
Text="Login" Command="{Binding LoginCommand}"/>
</Grid>
</ContentPage.Content>
我的 App.xaml:
<Application.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type Label}">
<Setter Property="Margin" Value="3, 0, 0, 0" />
</Style>
<Color x:Key="Primary">#1976D2</Color>
<Style TargetType="Button">
<Setter Property="TextColor" Value="White"></Setter>
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="{StaticResource Primary}" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Disabled">
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="#ffe6e6" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ResourceDictionary>
</Application.Resources>
以及 Android 上的 Accent Color 覆寫(styles.xml):
<style name="MainTheme" parent="MainTheme.Base">
<item name="colorAccent">#1976D2</item>
</style>
這是我的第一個真正的 xamarin 專案,因此感謝您的任何意見!
uj5u.com熱心網友回復:
好的,
系統首選主題會覆寫不同的元素,因此為什么在模擬環境(淺色主題)中按預期呈現的元素和在物理手機(暗模式)上不呈現的元素。
由于我想為所有設備使用一種樣式,因此相當簡單的解決方案是在您的 Android 專案中添加以下行MainAcitity.cs:
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/459770.html
