8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

iOS 中的 CollectionView 在 iOS 和 macOS 中无法正确呈现

Henrik 1月前

21 0

在我的 .NET8 MAUI 项目中,我有一个 CollectionView 来显示一些元素。此集合适用于 Windows 和 Android。

在我的 .NET8 MAUI 项目中,我有一个 CollectionView 用于显示一些元素的集合。此集合适用于 Windows 和 Android。

<CollectionView
    Margin="{OnIdiom '20,0,20,0',
                        Desktop='20,0,20,0'}"
    HorizontalOptions="FillAndExpand"
    IsVisible="{Binding ShowArticles}"
    ItemsSource="{Binding NewestArticles}"
    SelectionChanged="CollectionView_SelectionChanged"
    SelectionMode="Single"
    VerticalOptions="FillAndExpand">
    <CollectionView.ItemsLayout>
        <LinearItemsLayout ItemSpacing="10" Orientation="Horizontal" />
    </CollectionView.ItemsLayout>
    <CollectionView.ItemTemplate>
        <DataTemplate x:DataType="artModel:NewestArticle">
            <Grid
                Margin="5,5,5,5"
                ColumnSpacing="10"
                HorizontalOptions="FillAndExpand"
                VerticalOptions="FillAndExpand"
                WidthRequest="300">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <Image
                    Grid.ColumnSpan="3"
                    Margin="0,0,0,15"
                    Aspect="AspectFill"
                    HeightRequest="200"
                    HorizontalOptions="FillAndExpand"
                    VerticalOptions="FillAndExpand"
                    WidthRequest="300">
                    <Image.Source>
                        <UriImageSource CacheValidity="10:00:00:00" Uri="{Binding FeaturedImage}" />
                    </Image.Source>
                    <Image.Clip>
                        <RoundRectangleGeometry CornerRadius="25" Rect="0,0,300,200" />
                    </Image.Clip>
                </Image>

                <VerticalStackLayout
                    Grid.Row="1"
                    HorizontalOptions="FillAndExpand"
                    VerticalOptions="FillAndExpand">
                    <components:LanguageLevelBadge
                        BgColor="{Binding BadgeColor}"
                        Text="{Binding BadgeText}"
                        VerticalOptions="StartAndExpand" />

                    <components:FlagBadge FlagUrl="{Binding FlagUrl}" />
                </VerticalStackLayout>

                <VerticalStackLayout
                    Grid.Row="1"
                    Grid.Column="1"
                    HorizontalOptions="FillAndExpand"
                    VerticalOptions="FillAndExpand">

                    <Label Style="{StaticResource PostTitle}" Text="{Binding Title}" />
                    <Label Style="{StaticResource PostExcerpt}" Text="{Binding Excerpt}" />
                    <Label Style="{StaticResource PostCategory}" Text="{Binding Category}" />
                </VerticalStackLayout>
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

Windows 和 Android 的结果如下

CollectionView render for Windows

iOS 的项目时 macOS ,渲染完全错误且不完整。

enter image description here

你知道这些平台是否存在问题吗 CollectionView ?我该如何解决这个问题?

更新/2

LanguageLevelBadge FlagBadge 两个相似的组件。这里 LanguageLevelBadge

<?xml version="1.0" encoding="utf-8" ?>
<ContentView
    x:Class="LanguageInUse.Components.LanguageLevelBadge"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Name="llb">

    <HorizontalStackLayout BindingContext="{x:Reference llb}">
        <Label
            Margin="2"
            Padding="5,5"
            BackgroundColor="{Binding BgColor}"
            HorizontalOptions="Center"
            Text="{Binding Text}"
            TextColor="{Binding Color}"
            VerticalOptions="Center" />
    </HorizontalStackLayout>
</ContentView>

以及背后的代码

public partial class LanguageLevelBadge : ContentView
{
    public static readonly BindableProperty LevelProperty = BindableProperty.Create(nameof(Level), typeof(LanguageLevel), typeof(LanguageLevelBadge));
    public static readonly BindableProperty BgColorProperty = BindableProperty.Create(nameof(BgColor), typeof(string), typeof(LanguageLevelBadge));
    public static readonly BindableProperty ColorProperty = BindableProperty.Create(nameof(Color), typeof(string), typeof(LanguageLevelBadge), "#FFFFFF");
    public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(LanguageLevelBadge));

    public string BgColor
    {
        get => GetValue(BgColorProperty) as string;
        set => SetValue(BgColorProperty, value);
    }

    public string Color
    {
        get => GetValue(ColorProperty) as string;
        set => SetValue(ColorProperty, value);
    }

    public string Text
    {
        get => GetValue(TextProperty) as string;
        set { 
            SetValue(TextProperty, value);
            BgColor = Level.GetColor();
        }
    }

    public LanguageLevel Level
    {
        get => (LanguageLevel)GetValue(LevelProperty);
        set => SetValue(LevelProperty, value);
    }

    public LanguageLevelBadge()
    {
        InitializeComponent();
    }
}

这是代码 FlagBadge

<?xml version="1.0" encoding="utf-8" ?>
<ContentView
    x:Class="LanguageInUse.Components.FlagBadge"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Name="fb">

    <Border
        Background="{Binding InternalFrameColor}"
        BindingContext="{x:Reference fb}"
        HorizontalOptions="FillAndExpand"
        MaximumHeightRequest="{Binding MaxHeight}"
        Stroke="{StaticResource Primary24}"
        StrokeShape="RoundRectangle 10"
        StrokeThickness="2"
        VerticalOptions="FillAndExpand">
        <Image
            x:Name="imageFlag"
            Aspect="AspectFill"
            MaximumHeightRequest="{Binding MaxHeight}"
            Source="{Binding FlagUrl}" />
    </Border>
</ContentView>

代码是

public partial class FlagBadge : ContentView
{
    #region Bindable properties

    public static readonly BindableProperty MaxHeightProperty = BindableProperty.Create(nameof(MaxHeight), typeof(double), typeof(WordCheck), (double)28);
    public static readonly BindableProperty FlagUrlProperty = BindableProperty.Create(nameof(FlagUrl), typeof(string), typeof(WordCheck));

    /// <summary>
    /// Gets or sets the maximum height.
    /// </summary>
    /// <value>The maximum height.</value>
    public double MaxHeight
    {
        get => (double)GetValue(MaxHeightProperty);
        set => SetValue(MaxHeightProperty, value);
    }

    /// <summary>
    /// Gets or sets the flag URL.
    /// </summary>
    /// <value>The flag URL.</value>
    public string FlagUrl
    {
        get => GetValue(FlagUrlProperty) as string;
        set => SetValue(FlagUrlProperty, value);
    }

    #endregion
    public FlagBadge()
    {
        InitializeComponent();
    }
}
帖子版权声明 1、本帖标题:iOS 中的 CollectionView 在 iOS 和 macOS 中无法正确呈现
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Henrik在本站《ios》版块原创发布, 转载请注明出处!
最新回复 (0)
返回
作者最近主题: