FlexLayout与DataTemplate对齐

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

FlexLayout with DataTemplate alignment

问题

我的问题是这样的。我想使项目在我的弹性布局内对齐,就像这样(谈论图片):

[![enter image description here](https://i.stack.imgur.com/joNkJ.png)](https://i.stack.imgur.com/joNkJ.png)

但我不知道为什么我的代码不起作用。它在处理其他元素时表现正常,但当我将它放在<datatemplate>内时,它就无法正常工作。有没有人知道吗?

这是代码片段:

```xml
<CollectionView.ItemTemplate>
	<DataTemplate x:DataType="model:ImagesJson">
		<FlexLayout Wrap="Wrap" Direction="Row" JustifyContent="SpaceEvenly">
			<VerticalStackLayout FlexLayout.Basis="40%">
				<Image Source="{Binding MainImage}" Aspect="AspectFit" HeightRequest="150"/>
			</VerticalStackLayout>
		</FlexLayout>
	</DataTemplate>
</CollectionView.ItemTemplate>

当前的行为是它将所有图像叠放在一起,而不是并排放置:

FlexLayout与DataTemplate对齐


<details>
<summary>英文:</summary>

My problem is the following. I would like to make the items align like this inside my flex layout (talking about the pictures):

[![enter image description here](https://i.stack.imgur.com/joNkJ.png)](https://i.stack.imgur.com/joNkJ.png)

But I don&#39;t know why is my code not working. It works fine with other elements, but it just doesn&#39;t work properly when I put this inside &lt;datatemplate&gt;. Is there anybody who would know?

Here is the code snippet:

```xml
&lt;CollectionView.ItemTemplate&gt;
	&lt;DataTemplate x:DataType=&quot;model:ImagesJson&quot;&gt;
		&lt;FlexLayout Wrap=&quot;Wrap&quot; Direction=&quot;Row&quot; JustifyContent=&quot;SpaceEvenly&quot;&gt;
			&lt;VerticalStackLayout FlexLayout.Basis=&quot;40%&quot;&gt;
				&lt;Image Source=&quot;{Binding MainImage}&quot; Aspect=&quot;AspectFit&quot; HeightRequest=&quot;150&quot;/&gt;
			&lt;/VerticalStackLayout&gt;
		&lt;/FlexLayout&gt;
	&lt;/DataTemplate&gt;
&lt;/CollectionView.ItemTemplate&gt;

The current behavior is that it puts all images on top of each other instead of next to each other:

FlexLayout与DataTemplate对齐

答案1

得分: 1

你可能需要使用BindableLayout来实现,而不是使用CollectionView:

<FlexLayout
    Wrap="Wrap"
    Direction="Row"
    JustifyContent="SpaceEvenly"
    BindableLayout.ItemsSource="{Binding YourItemsSource}">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <Image
                Source="{Binding MainImage}" 
                Aspect="AspectFit" HeightRequest="150"/>
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</FlexLayout>

你当前的设置过于复杂,可能会导致性能问题,除了你已经遇到的问题外。通常情况下,嵌套这么多布局不是一个好主意。

英文:

You're probably looking for something like this using a BindableLayout instead of a CollectionView:

&lt;FlexLayout
    Wrap=&quot;Wrap&quot;
    Direction=&quot;Row&quot;
    JustifyContent=&quot;SpaceEvenly&quot;
    BindableLayout.ItemsSource=&quot;{Binding YourItemsSource}&quot;&gt;
    &lt;BindableLayout.ItemTemplate&gt;
        &lt;DataTemplate&gt;
            &lt;Image
                Source=&quot;{Binding MainImage}&quot; 
                Aspect=&quot;AspectFit&quot; HeightRequest=&quot;150&quot;/&gt;
        &lt;/DataTemplate&gt;
    &lt;/BindableLayout.ItemTemplate&gt;
&lt;/FlexLayout&gt;

Your current setup is overly complicated and will likely suffer from poor performance apart from the issue you're running into already now. Nesting so many layouts usually isn't a great idea.

huangapple
  • 本文由 发表于 2023年4月6日 21:54:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950346.html
匿名

发表评论

匿名网友

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

确定