Xamarin Listview 只添加 1 个项目我的列表上有 2 个项目
Xamarin Listview only adding 1 item my List has 2 items on it
我有一个列表视图和一个通过蓝牙连接将项目获取到无线电设备的方法,当该方法结束时,我的项目列表中有两个项目,但我的列表视图上只显示一个项目,而且该项目不可见直到我出于某种原因点击屏幕。
关于如何在 ListView 中同时显示我的两个项目的任何帮助?
我认为这与我处理 EventRead 和 EventStopped 的方式有关,也许它没有时间更新
public partial class ReadAnyPage : ContentPage
{
IRadioPlatform _platform;
DropdownTypes dropdownTypes;
Task<List<ChoiceConnectEndpoint>> _readAnyComplete;
public event EventHandler OnStopped;
public event EventHandler<Itron.Mobile.Radio.Communication.SRead.HandledEventArgs<ChoiceConnectEndpoint>> readEndpoints;
private List<JsonDict> itemsList = new List<JsonDict>();
string bleName, blePort;
public ReadAnyPage()
{
InitializeComponent();
BindingContext = this; //Added this
_platform = DependencyService.Get<IRadioPlatform>();
dropdownTypes = new DropdownTypes();
bleName = Preferences.Get("ble_name", "");
blePort = Preferences.Get("ble_port", "");
CancellationTokenSource tokenSource = new CancellationTokenSource();
CancellationToken token = tokenSource.Token;
listView.ItemsSource = itemsList;
ReadAnyNear(tokenSource, token);
}
private void ReadAnyNear(CancellationTokenSource tokenSource, CancellationToken token)
{
var loading = UserDialogs.Instance.Loading("Reading Endpoints...", onCancel: tokenSource.Cancel, "\n\n" + Resx.Resources.Cancel, true, MaskType.Black);
try
{
// Set new IMR
IMRRadioService imrRadioService = new IMRRadioService(_platform);
imrRadioService.Name = bleName;
imrRadioService.Port = blePort;string
readEndpoints += EventRead;
OnStopped += EventStopped;
_readAnyComplete = imrRadioService.ReadAny(token, readEndpoints, OnStopped);
}
catch (Exception ex)
{
DisplayAlert("Error", dropdownTypes.OnConvertErrorEX(ex.Message), "ok", "cancel");
}
loading.Hide();
}
private void EventRead(object sender, EventArgs e)
{
Console.WriteLine("READ READ");
try
{
Itron.Mobile.Radio.Communication.SRead.HandledEventArgs<ChoiceConnectEndpoint> item = (Itron.Mobile.Radio.Communication.SRead.HandledEventArgs<ChoiceConnectEndpoint>)e;
JsonDict jsonDict = new JsonDict();
jsonDict.RadioNumber = item.Data.EndpointId.ToString();
jsonDict.MeterType = item.Data.MarketType.ToString();
jsonDict.Read = ((Endpoint100G)item.Data).Reading.Read.ToString();
jsonDict.ModuleType = item.Data.EndpointTypeName;
jsonDict.ServiceType = item.Data.MarketType.ToString();
itemsList.Add(jsonDict);
Console.WriteLine("Count:" + itemsList.Count);
listView.ItemsSource = itemsList;
}
catch (Exception ex)
{
Console.WriteLine("Error:" + ex.Message);
}
}
private void EventStopped(object sender, EventArgs e)
{
Console.WriteLine("STOPPED");
}
private async void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
}
private void ListView_Refreshing(object sender, EventArgs e)
{
OnRefreshDevicesList();
}
private void OnRefreshDevicesList()
{
listView.EndRefresh();
}
}
感谢@Jason 解决
private List<JsonDict> itemsList = new List<JsonDict>();
对此
private ObservableCollection<JsonDict> itemsList = new ObservableCollection<JsonDict>();
我有一个列表视图和一个通过蓝牙连接将项目获取到无线电设备的方法,当该方法结束时,我的项目列表中有两个项目,但我的列表视图上只显示一个项目,而且该项目不可见直到我出于某种原因点击屏幕。
关于如何在 ListView 中同时显示我的两个项目的任何帮助?
我认为这与我处理 EventRead 和 EventStopped 的方式有关,也许它没有时间更新
public partial class ReadAnyPage : ContentPage
{
IRadioPlatform _platform;
DropdownTypes dropdownTypes;
Task<List<ChoiceConnectEndpoint>> _readAnyComplete;
public event EventHandler OnStopped;
public event EventHandler<Itron.Mobile.Radio.Communication.SRead.HandledEventArgs<ChoiceConnectEndpoint>> readEndpoints;
private List<JsonDict> itemsList = new List<JsonDict>();
string bleName, blePort;
public ReadAnyPage()
{
InitializeComponent();
BindingContext = this; //Added this
_platform = DependencyService.Get<IRadioPlatform>();
dropdownTypes = new DropdownTypes();
bleName = Preferences.Get("ble_name", "");
blePort = Preferences.Get("ble_port", "");
CancellationTokenSource tokenSource = new CancellationTokenSource();
CancellationToken token = tokenSource.Token;
listView.ItemsSource = itemsList;
ReadAnyNear(tokenSource, token);
}
private void ReadAnyNear(CancellationTokenSource tokenSource, CancellationToken token)
{
var loading = UserDialogs.Instance.Loading("Reading Endpoints...", onCancel: tokenSource.Cancel, "\n\n" + Resx.Resources.Cancel, true, MaskType.Black);
try
{
// Set new IMR
IMRRadioService imrRadioService = new IMRRadioService(_platform);
imrRadioService.Name = bleName;
imrRadioService.Port = blePort;string
readEndpoints += EventRead;
OnStopped += EventStopped;
_readAnyComplete = imrRadioService.ReadAny(token, readEndpoints, OnStopped);
}
catch (Exception ex)
{
DisplayAlert("Error", dropdownTypes.OnConvertErrorEX(ex.Message), "ok", "cancel");
}
loading.Hide();
}
private void EventRead(object sender, EventArgs e)
{
Console.WriteLine("READ READ");
try
{
Itron.Mobile.Radio.Communication.SRead.HandledEventArgs<ChoiceConnectEndpoint> item = (Itron.Mobile.Radio.Communication.SRead.HandledEventArgs<ChoiceConnectEndpoint>)e;
JsonDict jsonDict = new JsonDict();
jsonDict.RadioNumber = item.Data.EndpointId.ToString();
jsonDict.MeterType = item.Data.MarketType.ToString();
jsonDict.Read = ((Endpoint100G)item.Data).Reading.Read.ToString();
jsonDict.ModuleType = item.Data.EndpointTypeName;
jsonDict.ServiceType = item.Data.MarketType.ToString();
itemsList.Add(jsonDict);
Console.WriteLine("Count:" + itemsList.Count);
listView.ItemsSource = itemsList;
}
catch (Exception ex)
{
Console.WriteLine("Error:" + ex.Message);
}
}
private void EventStopped(object sender, EventArgs e)
{
Console.WriteLine("STOPPED");
}
private async void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
}
private void ListView_Refreshing(object sender, EventArgs e)
{
OnRefreshDevicesList();
}
private void OnRefreshDevicesList()
{
listView.EndRefresh();
}
}
感谢@Jason 解决
private List<JsonDict> itemsList = new List<JsonDict>();
对此
private ObservableCollection<JsonDict> itemsList = new ObservableCollection<JsonDict>();