如何在 ListView Xamarin 上显示 System.Collection
How to display System.Collection on ListView Xamarin
我有一个系统集合,想在 ListView
上显示两列 dateForecast 和 levelForecast来自 IEnumerable<AlertLevel>
.
这是我的 ListView
和 GoogleFormMap
:
<Grid>
<Label Text="Hydro Forecast Maritsa-Tundzha"
HorizontalOptions="CenterAndExpand"
FontSize="Large"
FontAttributes="Bold"
Margin="0,40,0,0"/>
<StackLayout>
<ListView x:Name="lstLevel">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label HorizontalOptions="StartAndExpand"
Text="{Binding DateLevel}"
Padding="10,0,0,0"/>
<Label HorizontalOptions="EndAndExpand"
Text="{Binding AlertLevel}"
Padding="10,0,0,0"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
<Grid Margin="0,70,0,10">
<Grid.RowDefinitions>
<RowDefinition Height="250" />
</Grid.RowDefinitions>
<StackLayout>
<local:CustomMap x:Name="customMap"
MapType="Street"
HasZoomEnabled="True"
HasScrollEnabled="False"/>
</StackLayout>
</Grid>
</Grid>
而我想用这个方法显示数据:
public void MarkerPressed()
{
MessagingCenter.Subscribe<object, IEnumerable<AlertLevel>>(this, "PinSelected", (sender, arg) =>
{
ListView lstLevel = (sender as ListView);
lstLevel.BindingContext = arg;
});
}
当我单击地图中的标记时,我收到此错误消息:
System.Reflection.TargetInvocationException has been thrown:
Exception has been thrown by the target of an invocation.
并且在 class CustomMapRenderer.cs
的 android 项目中将此方法发送给我 CustomMapRenderer.cs
:
CustomPin GetCustomPin(Marker annotation)
{
string id = annotation.Id.Substring(1);
int mapCode = int.Parse(id);
var result = DataBaseConnection(mapCode);
MessagingCenter.Send<object, IEnumerable<AlertLevel>>(this, "PinSelected", result);
var position = new Position(annotation.Position.Latitude, annotation.Position.Longitude);
foreach (var pin in customPins)
{
if (pin.Position == position)
{
return pin;
}
}
return null;
}
这是我的 android 项目中 CustomMapRenderer.cs
中的所有代码:
using System;
using System.Collections.Generic;
using System.Linq;
using Android.Content;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using Android.Widget;
using MaritsaTundzhaForecast;
using MaritsaTundzhaForecast.Models;
using MaritsaTundzhaForecast.Droid;
using MySqlConnector;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Xamarin.Forms.Maps.Android;
[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace MaritsaTundzhaForecast.Droid
{
public class CustomMapRenderer : MapRenderer, GoogleMap.IInfoWindowAdapter
{
List<CustomPin> customPins;
public CustomMapRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
NativeMap.InfoWindowClick -= OnInfoWindowClick;
}
if (e.NewElement != null)
{
var formsMap = (CustomMap)e.NewElement;
customPins = formsMap.CustomPins;
}
}
protected override void OnMapReady(GoogleMap map)
{
base.OnMapReady(map);
NativeMap.InfoWindowClick += OnInfoWindowClick;
NativeMap.SetInfoWindowAdapter(this);
}
protected override MarkerOptions CreateMarker(Pin pin)
{
var marker = new MarkerOptions();
marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
marker.SetTitle(pin.Label);
marker.SetSnippet(pin.Address);
//marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.green));
var custom = customPins.Where(x => x.Label == pin.Label && x.Address == pin.Address).FirstOrDefault();
if (custom != null)
{
if (custom.AlertLevel == 1)
{
marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.green));
}
if (custom.AlertLevel == 2)
{
marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.yellow));
}
if (custom.AlertLevel == 3)
{
marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.orange));
}
if (custom.AlertLevel == 4)
{
marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.red));
}
}
return marker;
}
void OnInfoWindowClick(object sender, GoogleMap.InfoWindowClickEventArgs e)
{
var customPin = GetCustomPin(e.Marker);
if (customPin == null)
{
throw new Exception("Custom pin not found");
}
if (!string.IsNullOrWhiteSpace(customPin.Url))
{
var url = Android.Net.Uri.Parse(customPin.Url);
var intent = new Intent(Intent.ActionView, url);
intent.AddFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intent);
}
}
public Android.Views.View GetInfoContents(Marker marker)
{
var inflater = Android.App.Application.Context.GetSystemService(Context.LayoutInflaterService) as Android.Views.LayoutInflater;
if (inflater != null)
{
Android.Views.View view;
var customPin = GetCustomPin(marker);
if (customPin == null)
{
throw new Exception("Custom pin not found");
}
if (customPin.Name.Equals("Xamarin"))
{
view = inflater.Inflate(Resource.Layout.XamarinMapInfoWindow, null);
}
else
{
view = inflater.Inflate(Resource.Layout.MapInfoWindow, null);
}
CustomPin pin = GetCustomPin(marker);
int CodeNum = pin.CodeNum;
int AlertLevel = pin.AlertLevel;
var infoTitle = view.FindViewById<TextView>(Resource.Id.InfoWindowTitle);
var infoSubtitle = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle);
var infoSubtitle2 = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle2);
var infoSubtitle3 = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle3);
if (infoTitle != null)
{
infoTitle.Text = marker.Title;
}
if (infoSubtitle != null)
{
infoSubtitle.Text = marker.Snippet;
}
if (infoSubtitle2 != null)
{
infoSubtitle2.Text = "Тревога: (1-4): " + AlertLevel;
}
if (infoSubtitle3 != null)
{
infoSubtitle3.Text = "Код на станция: " + CodeNum;
}
return view;
}
return null;
}
public Android.Views.View GetInfoWindow(Marker marker)
{
return null;
}
public IEnumerable<AlertLevel> DataBaseConnection(int mapCode)
{
string ConnectionString = "server=192.168.0.1;uid=userName;port=3387;pwd=password;database=dbName;";
MySqlConnection Conn = new MySqlConnection(ConnectionString);
var listAlert = new List<AlertLevel>();
try
{
Conn.Open();
//replace(2) with mapCode
string query = "CALL Get_Alert_levels_Station(" + mapCode + ");";
MySqlCommand myCommand = new MySqlCommand(query, Conn);
MySqlDataReader myReader;
myReader = myCommand.ExecuteReader();
try
{
while (myReader.Read())
{
var currentData = new AlertLevel()
{
dateForecast = myReader.GetDateTime(0),
levelForecast = myReader.GetInt32(1)
};
listAlert.Add(currentData);
}
}
finally
{
myReader.Close();
Conn.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("Database Connection", "Not Connected ..." + Environment.NewLine + ex.ToString(), "OK");
}
return listAlert;
}
CustomPin GetCustomPin(Marker annotation)
{
string id = annotation.Id.Substring(1);
int mapCode = int.Parse(id);
var result = DataBaseConnection(mapCode);
MessagingCenter.Send<object, IEnumerable<AlertLevel>>(this, "PinSelected", result);
var position = new Position(annotation.Position.Latitude, annotation.Position.Longitude);
foreach (var pin in customPins)
{
if (pin.Position == position)
{
return pin;
}
}
return null;
}
}
}
我能举个例子来解决这个错误吗?
使用BindingContext
属性的ListView
。
如果您的发件人是您的 ListView
那么:
ListView yourListView = (sender as ListView);
yourListView.BindingContext = yourCollection;
如果没有,则给您的 ListView
一个名称(在 xaml 中使用 x:Name
属性)然后访问 BindingContext
属性你的代码在后面。
如果您的方法位于您的 ViewModel
中,只需将您的 ListView
绑定到它即可:
<ListView x:Name="lstLevel" ItemsSource="{Binding yourCollectionName}" ItemTapped="RowItemTapped">
只需分配 ListView
的 ItemSource
MessagingCenter.Subscribe<object, IEnumerable<AlertLevel>>(this, "PinSelected", async (sender, arg) =>
{
lstLevel.ItemsSource = arg;
});
我有一个系统集合,想在 ListView
上显示两列 dateForecast 和 levelForecast来自 IEnumerable<AlertLevel>
.
这是我的 ListView
和 GoogleFormMap
:
<Grid>
<Label Text="Hydro Forecast Maritsa-Tundzha"
HorizontalOptions="CenterAndExpand"
FontSize="Large"
FontAttributes="Bold"
Margin="0,40,0,0"/>
<StackLayout>
<ListView x:Name="lstLevel">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label HorizontalOptions="StartAndExpand"
Text="{Binding DateLevel}"
Padding="10,0,0,0"/>
<Label HorizontalOptions="EndAndExpand"
Text="{Binding AlertLevel}"
Padding="10,0,0,0"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
<Grid Margin="0,70,0,10">
<Grid.RowDefinitions>
<RowDefinition Height="250" />
</Grid.RowDefinitions>
<StackLayout>
<local:CustomMap x:Name="customMap"
MapType="Street"
HasZoomEnabled="True"
HasScrollEnabled="False"/>
</StackLayout>
</Grid>
</Grid>
而我想用这个方法显示数据:
public void MarkerPressed()
{
MessagingCenter.Subscribe<object, IEnumerable<AlertLevel>>(this, "PinSelected", (sender, arg) =>
{
ListView lstLevel = (sender as ListView);
lstLevel.BindingContext = arg;
});
}
当我单击地图中的标记时,我收到此错误消息:
System.Reflection.TargetInvocationException has been thrown:
Exception has been thrown by the target of an invocation.
并且在 class CustomMapRenderer.cs
的 android 项目中将此方法发送给我 CustomMapRenderer.cs
:
CustomPin GetCustomPin(Marker annotation)
{
string id = annotation.Id.Substring(1);
int mapCode = int.Parse(id);
var result = DataBaseConnection(mapCode);
MessagingCenter.Send<object, IEnumerable<AlertLevel>>(this, "PinSelected", result);
var position = new Position(annotation.Position.Latitude, annotation.Position.Longitude);
foreach (var pin in customPins)
{
if (pin.Position == position)
{
return pin;
}
}
return null;
}
这是我的 android 项目中 CustomMapRenderer.cs
中的所有代码:
using System;
using System.Collections.Generic;
using System.Linq;
using Android.Content;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using Android.Widget;
using MaritsaTundzhaForecast;
using MaritsaTundzhaForecast.Models;
using MaritsaTundzhaForecast.Droid;
using MySqlConnector;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Xamarin.Forms.Maps.Android;
[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace MaritsaTundzhaForecast.Droid
{
public class CustomMapRenderer : MapRenderer, GoogleMap.IInfoWindowAdapter
{
List<CustomPin> customPins;
public CustomMapRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
NativeMap.InfoWindowClick -= OnInfoWindowClick;
}
if (e.NewElement != null)
{
var formsMap = (CustomMap)e.NewElement;
customPins = formsMap.CustomPins;
}
}
protected override void OnMapReady(GoogleMap map)
{
base.OnMapReady(map);
NativeMap.InfoWindowClick += OnInfoWindowClick;
NativeMap.SetInfoWindowAdapter(this);
}
protected override MarkerOptions CreateMarker(Pin pin)
{
var marker = new MarkerOptions();
marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
marker.SetTitle(pin.Label);
marker.SetSnippet(pin.Address);
//marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.green));
var custom = customPins.Where(x => x.Label == pin.Label && x.Address == pin.Address).FirstOrDefault();
if (custom != null)
{
if (custom.AlertLevel == 1)
{
marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.green));
}
if (custom.AlertLevel == 2)
{
marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.yellow));
}
if (custom.AlertLevel == 3)
{
marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.orange));
}
if (custom.AlertLevel == 4)
{
marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.red));
}
}
return marker;
}
void OnInfoWindowClick(object sender, GoogleMap.InfoWindowClickEventArgs e)
{
var customPin = GetCustomPin(e.Marker);
if (customPin == null)
{
throw new Exception("Custom pin not found");
}
if (!string.IsNullOrWhiteSpace(customPin.Url))
{
var url = Android.Net.Uri.Parse(customPin.Url);
var intent = new Intent(Intent.ActionView, url);
intent.AddFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intent);
}
}
public Android.Views.View GetInfoContents(Marker marker)
{
var inflater = Android.App.Application.Context.GetSystemService(Context.LayoutInflaterService) as Android.Views.LayoutInflater;
if (inflater != null)
{
Android.Views.View view;
var customPin = GetCustomPin(marker);
if (customPin == null)
{
throw new Exception("Custom pin not found");
}
if (customPin.Name.Equals("Xamarin"))
{
view = inflater.Inflate(Resource.Layout.XamarinMapInfoWindow, null);
}
else
{
view = inflater.Inflate(Resource.Layout.MapInfoWindow, null);
}
CustomPin pin = GetCustomPin(marker);
int CodeNum = pin.CodeNum;
int AlertLevel = pin.AlertLevel;
var infoTitle = view.FindViewById<TextView>(Resource.Id.InfoWindowTitle);
var infoSubtitle = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle);
var infoSubtitle2 = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle2);
var infoSubtitle3 = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle3);
if (infoTitle != null)
{
infoTitle.Text = marker.Title;
}
if (infoSubtitle != null)
{
infoSubtitle.Text = marker.Snippet;
}
if (infoSubtitle2 != null)
{
infoSubtitle2.Text = "Тревога: (1-4): " + AlertLevel;
}
if (infoSubtitle3 != null)
{
infoSubtitle3.Text = "Код на станция: " + CodeNum;
}
return view;
}
return null;
}
public Android.Views.View GetInfoWindow(Marker marker)
{
return null;
}
public IEnumerable<AlertLevel> DataBaseConnection(int mapCode)
{
string ConnectionString = "server=192.168.0.1;uid=userName;port=3387;pwd=password;database=dbName;";
MySqlConnection Conn = new MySqlConnection(ConnectionString);
var listAlert = new List<AlertLevel>();
try
{
Conn.Open();
//replace(2) with mapCode
string query = "CALL Get_Alert_levels_Station(" + mapCode + ");";
MySqlCommand myCommand = new MySqlCommand(query, Conn);
MySqlDataReader myReader;
myReader = myCommand.ExecuteReader();
try
{
while (myReader.Read())
{
var currentData = new AlertLevel()
{
dateForecast = myReader.GetDateTime(0),
levelForecast = myReader.GetInt32(1)
};
listAlert.Add(currentData);
}
}
finally
{
myReader.Close();
Conn.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("Database Connection", "Not Connected ..." + Environment.NewLine + ex.ToString(), "OK");
}
return listAlert;
}
CustomPin GetCustomPin(Marker annotation)
{
string id = annotation.Id.Substring(1);
int mapCode = int.Parse(id);
var result = DataBaseConnection(mapCode);
MessagingCenter.Send<object, IEnumerable<AlertLevel>>(this, "PinSelected", result);
var position = new Position(annotation.Position.Latitude, annotation.Position.Longitude);
foreach (var pin in customPins)
{
if (pin.Position == position)
{
return pin;
}
}
return null;
}
}
}
我能举个例子来解决这个错误吗?
使用BindingContext
属性的ListView
。
如果您的发件人是您的 ListView
那么:
ListView yourListView = (sender as ListView);
yourListView.BindingContext = yourCollection;
如果没有,则给您的 ListView
一个名称(在 xaml 中使用 x:Name
属性)然后访问 BindingContext
属性你的代码在后面。
如果您的方法位于您的 ViewModel
中,只需将您的 ListView
绑定到它即可:
<ListView x:Name="lstLevel" ItemsSource="{Binding yourCollectionName}" ItemTapped="RowItemTapped">
只需分配 ListView
ItemSource
MessagingCenter.Subscribe<object, IEnumerable<AlertLevel>>(this, "PinSelected", async (sender, arg) =>
{
lstLevel.ItemsSource = arg;
});