圆角框架在每个平台上呈现不同。

huangapple go评论128阅读模式
英文:

Rounded frame renders differently for each platform

问题

我开始使用MAUI创建应用程序,因为我以前使用Xamarin并且非常喜欢它。现在,我有一个简单的ListView,我想在其中显示一个包含名称、描述和字典标志的模型。在ListView.ItemTemplate中,我有一个DataTemplate和一个ViewCell

<ListView x:Name="listDictionaries" ItemsSource="{Binding Dictionaries}"
          SelectionMode="Single" HasUnevenRows="True" 
          CachingStrategy="RecycleElement"
          HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid RowSpacing="10" ColumnSpacing="10" 
                      HorizontalOptions="FillAndExpand" 
                      VerticalOptions="FillAndExpand">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <Frame BorderColor="White"
                        CornerRadius="50"
                        HeightRequest="50"
                        WidthRequest="50"
                        IsClippedToBounds="True"
                        HorizontalOptions="Center"
                        VerticalOptions="Center"
                        Grid.RowSpan="2" Grid.Column="0" Grid.Row="0">
                        <Image Source="uk.png"
                            Aspect="AspectFill"
                            HeightRequest="50"
                            WidthRequest="50" />
                    </Frame>
                    <Frame BorderColor="White"
                        CornerRadius="30"
                        HeightRequest="30"
                        WidthRequest="30"
                        IsClippedToBounds="True"
                        HorizontalOptions="EndAndExpand"
                        VerticalOptions="EndAndExpand"
                        TranslationX="-15"
                        Grid.RowSpan="2" Grid.Column="0" Grid.Row="0">
                        <Image Source="it.png"
                            Aspect="AspectFill"
                            HeightRequest="30"
                            WidthRequest="30" />
                    </Frame>

                    <Label Text="{Binding Name}"
                    FontSize="18"
                    FontAttributes="Bold"
                    Grid.Column="1" />
                    <Label Text="{Binding Description}"
                    FontSize="12"
                    Grid.Column="1" Grid.Row="1" />
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我的想法是在一个圆形框架中显示一个大的国旗SVG图像,然后在主图像的右下方显示另一个较小的国旗SVG图像。为此,我使用了TranslationX

在Android上,页面的渲染是我期望的(如下面的截图中间部分),iOS上是封闭的,Windows上完全错误。

是否可能在每个平台上获得相同的渲染效果?

英文:

I'm starting to create apps with MAUI because I used to work with Xamarin and I loved it. Now, I have a simple ListView where I want to display a model that contains a name, description and flags for a dictionary. In the ListView.ItemTemplate I have a DataTemplate and a ViewCell.

&lt;ListView x:Name=&quot;listDictionaries&quot; ItemsSource=&quot;{Binding Dictionaries}&quot;
SelectionMode=&quot;Single&quot; HasUnevenRows=&quot;True&quot; 
CachingStrategy=&quot;RecycleElement&quot;
HorizontalOptions=&quot;FillAndExpand&quot; VerticalOptions=&quot;FillAndExpand&quot;&gt;
&lt;ListView.ItemTemplate&gt;
&lt;DataTemplate&gt;
&lt;ViewCell&gt;
&lt;Grid RowSpacing=&quot;10&quot; ColumnSpacing=&quot;10&quot; 
HorizontalOptions=&quot;FillAndExpand&quot; 
VerticalOptions=&quot;FillAndExpand&quot;&gt;
&lt;Grid.RowDefinitions&gt;
&lt;RowDefinition Height=&quot;*&quot; /&gt;
&lt;RowDefinition Height=&quot;*&quot; /&gt;
&lt;/Grid.RowDefinitions&gt;
&lt;Grid.ColumnDefinitions&gt;
&lt;ColumnDefinition Width=&quot;100&quot; /&gt;
&lt;ColumnDefinition Width=&quot;*&quot; /&gt;
&lt;/Grid.ColumnDefinitions&gt;
&lt;Frame BorderColor=&quot;White&quot;
CornerRadius=&quot;50&quot;
HeightRequest=&quot;50&quot;
WidthRequest=&quot;50&quot;
IsClippedToBounds=&quot;True&quot;
HorizontalOptions=&quot;Center&quot;
VerticalOptions=&quot;Center&quot;
Grid.RowSpan=&quot;2&quot; Grid.Column=&quot;0&quot; Grid.Row=&quot;0&quot;&gt;
&lt;Image Source=&quot;uk.png&quot;
Aspect=&quot;AspectFill&quot;
HeightRequest=&quot;50&quot;
WidthRequest=&quot;50&quot; /&gt;
&lt;/Frame&gt;
&lt;Frame BorderColor=&quot;White&quot;
CornerRadius=&quot;30&quot;
HeightRequest=&quot;30&quot;
WidthRequest=&quot;30&quot;
IsClippedToBounds=&quot;True&quot;
HorizontalOptions=&quot;EndAndExpand&quot;
VerticalOptions=&quot;EndAndExpand&quot;
TranslationX=&quot;-15&quot;
Grid.RowSpan=&quot;2&quot; Grid.Column=&quot;0&quot; Grid.Row=&quot;0&quot;&gt;
&lt;Image Source=&quot;it.png&quot;
Aspect=&quot;AspectFill&quot;
HeightRequest=&quot;30&quot;
WidthRequest=&quot;30&quot; /&gt;
&lt;/Frame&gt;
&lt;Label Text=&quot;{Binding Name}&quot;
FontSize=&quot;18&quot;
FontAttributes=&quot;Bold&quot;
Grid.Column=&quot;1&quot; /&gt;
&lt;Label Text=&quot;{Binding Description}&quot;
FontSize=&quot;12&quot;
Grid.Column=&quot;1&quot; Grid.Row=&quot;1&quot; /&gt;
&lt;/Grid&gt;
&lt;/ViewCell&gt;
&lt;/DataTemplate&gt;
&lt;/ListView.ItemTemplate&gt;
&lt;/ListView&gt;

圆角框架在每个平台上呈现不同。

My idea is to display a big flag SVG image in a round frame and another flag SVG image smaller on the bottom right of the main image. For that, I use TranslationX.

The render of the page in Android is what I expect (in the middle in the screenshot below), iOS is closed and Windows is completely wrong.

Is it possible to obtain the same render for every platform?

答案1

得分: 2

谢谢 Jason,问题是 FrameMAUI 中已被弃用。

所以,使用以下代码,渲染在 Windows 和 Android 上是正确的,但在 iOS 上不正确。为了修复 iOS,你必须将图像包装在一个网格中。

<Border HeightRequest="50"
		WidthRequest="50"
		HorizontalOptions="Center"
		VerticalOptions="Center"
		StrokeShape="RoundRectangle 50"
		Grid.RowSpan="2" Grid.Column="0" Grid.Row="0">
	<Grid>
		<Image Source="uk.png"
				Aspect="AspectFill"
				HeightRequest="50"
				WidthRequest="50" />
	</Grid>
</Border>
<Border HeightRequest="30"
		WidthRequest="30"
		HorizontalOptions="End"
		VerticalOptions="End"
		TranslationX="-15"
		StrokeShape="RoundRectangle 30"
		Grid.RowSpan="2" Grid.Column="0" Grid.Row="0">
	<Grid>
		<Image Source="it.png"
				Aspect="AspectFill"
				HeightRequest="30"
				WidthRequest="30" />
	</Grid>
</Border>

iOS 的解决方法

要解决 iOS 的问题,必须将 Image 包装在一个 Grid 中。

圆角框架在每个平台上呈现不同。

英文:

Thank you to Jason, the problem was that Frame is deprecated in MAUI.

So, with the following code, the render is correct for Windows and Android but not for iOS. In order to fix iOS, you have to wrap the image in a grid.

&lt;Border HeightRequest=&quot;50&quot;
WidthRequest=&quot;50&quot;
HorizontalOptions=&quot;Center&quot;
VerticalOptions=&quot;Center&quot;
StrokeShape=&quot;RoundRectangle 50&quot;
Grid.RowSpan=&quot;2&quot; Grid.Column=&quot;0&quot; Grid.Row=&quot;0&quot;&gt;
&lt;Grid&gt;
&lt;Image Source=&quot;uk.png&quot;
Aspect=&quot;AspectFill&quot;
HeightRequest=&quot;50&quot;
WidthRequest=&quot;50&quot; /&gt;
&lt;/Grid&gt;
&lt;/Border&gt;
&lt;Border HeightRequest=&quot;30&quot;
WidthRequest=&quot;30&quot;
HorizontalOptions=&quot;End&quot;
VerticalOptions=&quot;End&quot;
TranslationX=&quot;-15&quot;
StrokeShape=&quot;RoundRectangle 30&quot;
Grid.RowSpan=&quot;2&quot; Grid.Column=&quot;0&quot; Grid.Row=&quot;0&quot;&gt;
&lt;Grid&gt;
&lt;Image Source=&quot;it.png&quot;
Aspect=&quot;AspectFill&quot;
HeightRequest=&quot;30&quot;
WidthRequest=&quot;30&quot; /&gt;
&lt;/Grid&gt;
&lt;/Border&gt;

Workaround for iOS

To fix the issue with iOS, the Image has to be wrapped in a Grid.

圆角框架在每个平台上呈现不同。

huangapple
  • 本文由 发表于 2023年7月14日 03:20:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定