gpt4 book ai didi

c# - 如何在 Xamarin.Forms 的 ListView 中获取所选项目的坐标?

转载 作者:太空狗 更新时间:2023-10-29 19:42:09 25 4
gpt4 key购买 nike

我想获取 ListView 中所选项目相对于屏幕(假设 ListView 填满整个屏幕)的坐标(矩形边界:x、y、宽度和高度),以便我可以创建一个对象该位置并为其设置动画以在我的 Xamarin.Forms 应用中显示所选项目的一些详细信息。

xaml 中的 ListView :

<ListView ItemTapped="ItemTapped"
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0.5, 0.5, 1.0, 1.0">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell Height="50">
<AbsoluteLayout>
<Label Text="{Binding Info}"
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0.1, 0.5, 0.7, 0.5"/>
</AbsoluteLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

ItemTapped 事件的 C# 代码:

void ItemTapped(object sender, EventArgs args)
{
var listView = (ListView)sender; // the listview
var selectedItem = args.Item; // the selected item

// need to get selected item coordinates for the animation
var selectedItemBounds = ...

...
}

最终我想在 Xamarin.Forms 中使用 ListView 创建这样的东西( ListView 中的对象数量各不相同):

sample listview animation

最佳答案

我创建了一个依赖项,您可以使用它来获取 VisualElement 在 iOS 和 Android 中的绝对位置。我将它用于类似的目的。我们用它来确定在 ListView 中点击时要显示的弹出窗口的位置。完美运行:

依赖:

public interface ILocationFetcher
{
System.Drawing.PointF GetCoordinates(global::Xamarin.Forms.VisualElement view);
}

iOS 实现:

 class LocationFetcher : ILocationFetcher
{
public System.Drawing.PointF GetCoordinates(global::Xamarin.Forms.VisualElement element)
{
var renderer = Platform.GetRenderer(element);
var nativeView = renderer.NativeView;
var rect = nativeView.Superview.ConvertPointToView(nativeView.Frame.Location, null);
return new System.Drawing.PointF((int)Math.Round(rect.X), (int)Math.Round(rect.Y));
}
}

Android 实现:

class LocationFetcher : ILocationFetcher
{
public System.Drawing.PointF GetCoordinates(global::Xamarin.Forms.VisualElement element)
{
var renderer = Platform.GetRenderer(element);
var nativeView = renderer.View;
var location = new int[2];
var density = nativeView.Context.Resources.DisplayMetrics.Density;

nativeView.GetLocationOnScreen(location);
return new System.Drawing.PointF(location[0] / density, location[1] / density);
}
}

感谢@Emil,我们还有一个 UWP 实现:

public System.Drawing.PointF GetCoordinates(global::Xamarin.Forms.VisualElement element)
{
var renderer = Xamarin.Forms.Platform.UWP.Platform.GetRenderer(element);
var nativeView = renderer.GetNativeElement();
var element_Visual_Relative = nativeView.TransformToVisual(Window.Current.Content);
Point point = element_Visual_Relative.TransformPoint(new Point(0, 0));
return new System.Drawing.PointF((int)Math.Round(point.X), (int)Math.Round(point.Y));
}

使用示例:

var locationFetcher = DependencyService.Get<ILocationFetcher>();
var location = locationFetcher.GetCoordinates(myVisualElement);

请务必使用依赖属性在 android 和 ios 中正确注册依赖项(请参阅 https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/)。否则 DependencyService.Get 将返回 null。

关于c# - 如何在 Xamarin.Forms 的 ListView 中获取所选项目的坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49263876/

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