C# ViewCell ForceUpdateSize有时不工作
我使用的是Xamarin表单中的自定义ViewCell,如下所示:C# ViewCell ForceUpdateSize有时不工作,c#,.net,xamarin,xamarin.android,xamarin.forms,C#,.net,Xamarin,Xamarin.android,Xamarin.forms,我使用的是Xamarin表单中的自定义ViewCell,如下所示: public class NativeCell : ViewCell { public double Height { get { return base.Height; } set { base.Height = value; ForceUpdateSize()
public class NativeCell : ViewCell
{
public double Height
{
get
{
return base.Height;
}
set
{
base.Height = value;
ForceUpdateSize();
}
}
}
我还创建了一个方法来设置ViewCell的colapse的动画,该方法按预期工作。但是,使用await附带的覆盖,每个循环迭代将持续100毫秒而不是1毫秒:
async public void Colapse()
{
for (double i = Height; i >= 0; i--)
{
Height = i;
await Task.Delay(1);
}
}
所以我做了另一种方法,用秒表来等待:
async public void Colapse()
{
var Timer = new Stopwatch();
for (double i = Height; i >= 0; i--)
{
Height = i;
Timer.Start();
while(Timer.Elapsed.TotalMilliseconds < 1)
{
}
Debug.WriteLine("-->" + Timer.Elapsed.TotalMilliseconds);
Timer.Reset();
}
}
async public void Colapse()
{
var Timer=新秒表();
对于(双i=高度;i>=0;i--)
{
高度=i;
Timer.Start();
while(Timer.appeased.total毫秒<1)
{
}
Debug.WriteLine(“-->”+Timer.appeased.total毫秒);
Timer.Reset();
}
}
最后一种方法报告等待时间现在通常为1.1ms-1.3ms。这很好。然而,高度协会并没有做任何事情。或者至少ForceUpdateSize没有触发。我们需要解决一些问题
int currentHeight;
public void Colapse()
{
currentHeight = (int)this.View.Height;
Device.StartTimer(new TimeSpan(100), timerTick);
}
private bool timerTick()
{
if (currentHeight <= 0)
return false;
else
{
Height = currentHeight--;
ForceUpdateSize();
return true;
}
}
int当前高度;
公共无效Colapse()
{
currentHeight=(int)this.View.Height;
装置。启动定时器(新的时间跨度(100),时间时钟);
}
私人布尔蒂默蒂克()
{
如果(currentHeight您是否尝试设置HeightRequest而不是ForceUpdateSize@YuriSViewCell没有HeightRequest属性。我相信这个问题与Wait将控制权返回给调用者这一事实有关。因此我还尝试在Device.beginUnuithRead中包装高度,但仍然不起作用。它只是立即跳到0er循环已完成。ViewCell中有什么?什么是布局和子项?@YuriS A StackLayout。在此循环之前,我已将其HeightRequest设置为0。是否在listview中设置Rowheight?