gpt4 book ai didi

c# - 如何使所有平台的 View 相同

转载 作者:太空宇宙 更新时间:2023-11-03 14:56:11 25 4
gpt4 key购买 nike

当我尝试使用自定义布局制作 ListView 时,它在 UWP 上看起来与 Android 上完全不同。我怎样才能让它们在所有平台上看起来都一样而不为每个平台制作一个页面?我尽量让我的应用看起来像线框。

我尝试将对象可视化为具有背景图像和两个标签的视单元。

我目前正在使用 Xamarin 和 .NET 标准 作为代码共享策略,为 Android 和 UWP 开发应用。

这个项目的想法是在共享项目(上面的项目文件)中创建每个 View /业务逻辑,因为应用程序不会使用任何平台特定的功能。

这是我的 XAML 代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="inleiding.Views.SubList"
Title="Your pokélist"
NavigationPage.BackButtonTitle="go back"
NavigationPage.HasBackButton="True"
NavigationPage.HasNavigationBar="True"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>

<Label Text="Cars" FontSize="Large" TextColor="LightGray" />

<ListView x:Name="lvwPok" ItemSelected="lvwPok_ItemSelected">


<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>

<RelativeLayout>
<Image VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
Source = "{Binding Pic}"></Image>


<Grid
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
BackgroundColor="Transparent"
>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" WidthRequest="100" HeightRequest="100" Source = "{Binding ImgPok}">
</Image>
<Label Grid.Row="1" Grid.Column="1" Text="{Binding Spec}" />
<Label Grid.Row="0" Grid.Column="1" Text="{ Binding Type }" />
</Grid>

</RelativeLayout>


</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>

</ListView>
</Grid>
</ContentPage>

( Pastebin link )

This is what I try to become

我没有想法。

最佳答案

您需要在 Xaml 中添加 OnPlatform 支持

Within the tags, include an OnPlatform tag.

OnPlatform is a generic class. You need to specify the generic type argument, in the below exmaple Thickness, which is the type of Padding property. Fortunately, there’s a XAML attribute specifically to define generic arguments called x:TypeArguments. This should match the type of the property you're setting:

例子

<StackLayout.Padding>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="Android" Value="0, 0, 0, 0"/>
<On Platform="WinPhone" Value="0, 0, 0, 0"/>
<On Platform="iOS" Value="0, 20, 0, 0"/>
</OnPlatform>
</StackLayout.Padding>

其他资源

Part 2. Essential XAML Syntax - Xamarin

关于c# - 如何使所有平台的 View 相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48924768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com