C# 如何在屏幕上显示数据并刷新此数据?
我使用Xamarin.Forms.Maps并显示我的位置和范围内的汽车位置。我每5秒向数据库发送一次获取坐标信息的请求。如何在地图上显示管脚,并在5秒钟后用地图中的新坐标刷新管脚? 我的代码在这里,但不起作用C# 如何在屏幕上显示数据并刷新此数据?,c#,xamarin,xamarin.forms,xamarin.android,maps,C#,Xamarin,Xamarin.forms,Xamarin.android,Maps,我使用Xamarin.Forms.Maps并显示我的位置和范围内的汽车位置。我每5秒向数据库发送一次获取坐标信息的请求。如何在地图上显示管脚,并在5秒钟后用地图中的新坐标刷新管脚? 我的代码在这里,但不起作用 using System; using System.Collections.Generic; using System.Globalization; using System.Threading; using System.Threading.Task
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
namespace CustomRenderer
{
public partial class MapPage : ContentPage
{
private System.Timers.Timer aTimer;
public MapPage()
{
InitializeComponent();
SetTimer();
}
public void RotateCars()
{
customMap.CustomPins = new List<CustomPin>();
double latitudeDoublecar15;
double longitudeDoublecar15;
var message = new Database();
CustomPin pin;
int timestamp = 0;//this timestamo is for cars moving simulation.
int tmpID15 = 10;//this is temp ID of the car
message.GetCarsApplyPeriodically(Convert.ToString(latitudeDoublecar15), Convert.ToString(longitudeDoublecar15), tmpID15, timestamp);
foreach (var b in Database.car)
{
pin = new CustomPin
{
Label = b.model,
Position = new Position(Convert.ToDouble(b.Latitude, CultureInfo.InvariantCulture), Convert.ToDouble(b.Longitude, CultureInfo.InvariantCulture)),
Type = PinType.Place
};
customMap.Pins.Add(pin);
customMap.CustomPins.Add(pin);
}
if (timestamp < 11)
{
timestamp++;
}
else
{
timestamp = 0;
}
CustomPin myPin = new CustomPin
{
Type = PinType.Place,
Label = DataManager.carsInRange.Count.ToString(),
Position = new Position(latitudeDoublecar15, longitudeDoublecar15),
Address = "my"
};
customMap.Pins.Add(myPin);
customMap.CustomPins.Add(myPin);
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(latitudeDoublecar15, longitudeDoublecar15), Distance.FromKilometers(0.9)));
Content = customMap;
Task.Delay(5000);
customMap.Pins.Clear();
customMap.CustomPins.Clear();
RotateCars();
}
}
private void SetTimer()
{
// Create a timer with a five second interval.
aTimer = new System.Timers.Timer(5000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += OnTimedEvent;
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
RotateCars();
}
在Xamarin中,最好使用Device.StartTimer
Device.StartTimer(TimeSpan.FromSeconds(5),()=> {
RotateCars();
return true;
});
customMap.CustomPins是实现IPropertyChanged的属性?并尝试使用可观察集合而不是列表。这没有帮助
Device.StartTimer(TimeSpan.FromSeconds(5),()=> {
RotateCars();
return true;
});