在Xamarin表单中获取图像在视图单元格listview中的位置

时间:2019-06-09 16:37:13

标签: xamarin xamarin.forms

我有一个带有视图单元格的列表视图。在列表视图中我有一个图像(使用比率的绝对布局),我将其用于某些动画。要做动画,我需要x和y坐标。我将图像扩展为

class MyImage : Image
{
   public void AnimateImage(double value)
   {
      this.LayoutTo(new Rectangle(this.X, this.Y - (value), 20, value), 
   }
}

我需要在加载时获取x和y坐标(不使用任何事件)。通过此代码无法获取正确的x,y坐标。借助可绑定属性,价值不断提高。

1 个答案:

答案 0 :(得分:1)

Through this code am not getting the correct x,y coordinates.是什么意思?怎么了?

我使用您的代码,它可以在我这边工作。

我在ViewCell中使用图像创建了一个ListView,并且按绝对布局是一个布局:

 <ListView  x:Name="listView" RowHeight="200">

        <ListView.ItemsSource>
            <x:Array Type="{x:Type x:String}">
                <x:String>mono</x:String>
                <x:String>monodroid</x:String>
                <x:String>monotouch</x:String>
                <x:String>monorail</x:String>
            </x:Array>
        </ListView.ItemsSource>

        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <AbsoluteLayout AbsoluteLayout.LayoutBounds="0,0,500,100" AbsoluteLayout.LayoutFlags="WidthProportional" Padding="5,0,0,0">

                        <local:MyImage Source="Images"
                         AbsoluteLayout.LayoutBounds=".5,1,.1,.5" AbsoluteLayout.LayoutFlags="All" />

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

在后面的代码中,我使用您的代码,动画效果很好:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }
}

public class MyImage : Image{

    public MyImage() {

        NSTimer.CreateScheduledTimer(3, true,  (obj) =>
        {
            AnimateImage(30);
        });
    }

    public void AnimateImage(double value)
    {
        Console.WriteLine(this.X);
        Console.WriteLine(this.Y);

        this.LayoutTo(new Rectangle(this.X, this.Y - (value), 20, value), 500);
    }
}

图像的Y每3秒减小50,图像的高度变为50。

我和你有不同的事情吗?

这是一个gif:

gif

更新

如我在评论中所述,在调用动画之前要加一点延迟:

public MyImage() {

    Task.Delay(100).ContinueWith(t => AnimateImage(30));

}