有没有一种方法可以使滑块动态更改其值?

时间:2019-05-10 08:34:18

标签: c# xamarin xamarin.forms slider

我有一些从gps收集的数据,在计算了当前位置和这些gps数据之间的距离之后,我希望滑块在到达这一点时自动移动。

但是问题是,当我放置一个while循环来更新距离值时,我的UI中出现了一个错误。因此我删除了循环,但无法实时显示距离的新值。

首先,我计算了路线的第一个点与最后一个点之间的距离,并将此值作为滑块的最大值。 必须动态更改的滑块的值是到起点的距离。

private async  void Prog()
        {
            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 50;
            var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(1));
            double startpoint = distance_on_geoid(position.Latitude,position.Longitude,JSONData.GetGlcLatitude1()[0],JSONData.GetGlcLongitude1()[0]);
            double endpoint = distance_on_geoid(position.Latitude, position.Longitude, JSONData.GetGlcLatitude1()[JSONData.GetGlcLatitude1().Count-1], JSONData.GetGlcLongitude1()[JSONData.GetGlcLongitude1().Count-1]);
            double alertDistance = distance_on_geoid(JSONData.GetGlcLatitude1()[0], JSONData.GetGlcLongitude1()[0], JSONData.GetGlcLatitude1()[JSONData.GetGlcLatitude1().Count - 1], JSONData.GetGlcLongitude1()[JSONData.GetGlcLongitude1().Count - 1]);
            slider.Maximum = alertDistance;

            double startpoint_ = Convert.ToDouble(String.Format("{0:0.00}", startpoint));
            test.Text = startpoint_.ToString();
                if (startpoint_>=0.01)
                {
                    slider.IsVisible = true;
                    slider.Value = alertDistance - endpoint;

            }
                else
                {
                    slider.IsVisible = false;
                }

        }

(JSONData ...是我的路线的值)

我希望滑块接近终点时会自动移动。滑块必须仅在im在区域中时可见,否则必须不可见。

1 个答案:

答案 0 :(得分:0)

我现在看到了这个问题,它的发生是因为您正在通过后台线程在UI上进行更改,如下所示:

Device.BeginInvokeOnMainThread (() => 
{
 slider.IsVisible = true;
 slider.Value = alertDistance - endpoint;
}

始终确保您在MainThread的UI上进行了更改,而不是在某些后台线程上进行更改,例如,在使用async-await进行修改时

相关问题