C# Xamarin Listview数据未显示?
我决定我需要一个自定义的C# Xamarin Listview数据未显示?,c#,listview,data-binding,xamarin,xamarin.forms,C#,Listview,Data Binding,Xamarin,Xamarin.forms,我决定我需要一个自定义的列表视图,用于所有平台,它可以在我的列表视图上显示多个数据字段。所以我决定从中签出代码并在我的模块中修改它。然而,我的ListView从未显示任何值/视图,我也不知道为什么 HorsePage.xaml: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://sc
列表视图
,用于所有平台,它可以在我的列表视图
上显示多个数据字段。所以我决定从中签出代码并在我的模块中修改它。然而,我的ListView
从未显示任何值/视图,我也不知道为什么
HorsePage.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Begijnhoeve.Pages.HorsesPage"
Title="Horse Overview">
</ContentPage>
var nativeListViewHorse = new NativeListViewHorse(); // CUSTOM RENDERER using a native control
nativeListViewHorse.VerticalOptions = LayoutOptions.FillAndExpand;
nativeListViewHorse.Items = DAHorse.DataAccess.DAHorse.GetList();
Content = new StackLayout
{
Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0),
Children = {
new Label {
XAlign = TextAlignment.Center,
Text = Device.OnPlatform("Custom UITableView+UICell","Custom ListView+Cell","Custom renderer todo")
},
nativeListViewHorse
}
};
namespace DAHorse.DataAccess
{
class DAHorse
{
private static object locker = new object();
private static SQLiteConnection db = DependencyService.Get<ISQLite_PCL>().GetConnection();
public static List<Horse> GetList()
{
var l = new List<Horse>();
l.Add(new Horse("HorseA", "1999"));
l.Add(new Horse("HorseB", "2000"));
l.Add(new Horse("HorseC", "2000"));
Debug.WriteLine("YAPYAPYAPYAP");
return l;
}
}
}
public class NativeListViewHorse : View
{
public static readonly BindableProperty ItemsProperty =
BindableProperty.Create("Items", typeof(IEnumerable<Horse>), typeof(NativeListViewHorse), new List<Horse>());
public IEnumerable<Horse> Items
{
get { return (IEnumerable<Horse>)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
public event EventHandler<SelectedItemChangedEventArgs> ItemSelected;
public void NotifyItemSelected(object item)
{
if (ItemSelected != null)
ItemSelected(this, new SelectedItemChangedEventArgs(item));
}
public NativeListViewHorse()
{
}
}
DAHorse.cs(数据访问层):
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Begijnhoeve.Pages.HorsesPage"
Title="Horse Overview">
</ContentPage>
var nativeListViewHorse = new NativeListViewHorse(); // CUSTOM RENDERER using a native control
nativeListViewHorse.VerticalOptions = LayoutOptions.FillAndExpand;
nativeListViewHorse.Items = DAHorse.DataAccess.DAHorse.GetList();
Content = new StackLayout
{
Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0),
Children = {
new Label {
XAlign = TextAlignment.Center,
Text = Device.OnPlatform("Custom UITableView+UICell","Custom ListView+Cell","Custom renderer todo")
},
nativeListViewHorse
}
};
namespace DAHorse.DataAccess
{
class DAHorse
{
private static object locker = new object();
private static SQLiteConnection db = DependencyService.Get<ISQLite_PCL>().GetConnection();
public static List<Horse> GetList()
{
var l = new List<Horse>();
l.Add(new Horse("HorseA", "1999"));
l.Add(new Horse("HorseB", "2000"));
l.Add(new Horse("HorseC", "2000"));
Debug.WriteLine("YAPYAPYAPYAP");
return l;
}
}
}
public class NativeListViewHorse : View
{
public static readonly BindableProperty ItemsProperty =
BindableProperty.Create("Items", typeof(IEnumerable<Horse>), typeof(NativeListViewHorse), new List<Horse>());
public IEnumerable<Horse> Items
{
get { return (IEnumerable<Horse>)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
public event EventHandler<SelectedItemChangedEventArgs> ItemSelected;
public void NotifyItemSelected(object item)
{
if (ItemSelected != null)
ItemSelected(this, new SelectedItemChangedEventArgs(item));
}
public NativeListViewHorse()
{
}
}
namespace DAHorse.DataAccess
{
达霍斯级
{
私有静态对象锁定器=新对象();
私有静态SQLiteConnection db=DependencyService.Get().GetConnection();
公共静态列表GetList()
{
var l=新列表();
l、 加上(新马(“HorseA”,“1999”));
l、 添加(新马(“HorseB”,“2000”));
l、 添加(新马(“HorseC”,“2000”));
Debug.WriteLine(“YAPYAPYAPYAP”);
返回l;
}
}
}
NativeListViewHorse.cs(自定义列表视图):
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Begijnhoeve.Pages.HorsesPage"
Title="Horse Overview">
</ContentPage>
var nativeListViewHorse = new NativeListViewHorse(); // CUSTOM RENDERER using a native control
nativeListViewHorse.VerticalOptions = LayoutOptions.FillAndExpand;
nativeListViewHorse.Items = DAHorse.DataAccess.DAHorse.GetList();
Content = new StackLayout
{
Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0),
Children = {
new Label {
XAlign = TextAlignment.Center,
Text = Device.OnPlatform("Custom UITableView+UICell","Custom ListView+Cell","Custom renderer todo")
},
nativeListViewHorse
}
};
namespace DAHorse.DataAccess
{
class DAHorse
{
private static object locker = new object();
private static SQLiteConnection db = DependencyService.Get<ISQLite_PCL>().GetConnection();
public static List<Horse> GetList()
{
var l = new List<Horse>();
l.Add(new Horse("HorseA", "1999"));
l.Add(new Horse("HorseB", "2000"));
l.Add(new Horse("HorseC", "2000"));
Debug.WriteLine("YAPYAPYAPYAP");
return l;
}
}
}
public class NativeListViewHorse : View
{
public static readonly BindableProperty ItemsProperty =
BindableProperty.Create("Items", typeof(IEnumerable<Horse>), typeof(NativeListViewHorse), new List<Horse>());
public IEnumerable<Horse> Items
{
get { return (IEnumerable<Horse>)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
public event EventHandler<SelectedItemChangedEventArgs> ItemSelected;
public void NotifyItemSelected(object item)
{
if (ItemSelected != null)
ItemSelected(this, new SelectedItemChangedEventArgs(item));
}
public NativeListViewHorse()
{
}
}
公共类NativeListViewHorse:视图
{
公共静态只读BindableProperty ItemsProperty=
创建(“Items”、typeof(IEnumerable)、typeof(NativeListViewHorse)、newlist());
公共数字项目
{
get{return(IEnumerable)GetValue(ItemsProperty);}
set{SetValue(ItemsProperty,value);}
}
已选择公共事件事件处理程序项;
已选择公用项目(对象项目)
{
如果(ItemSelected!=null)
所选项目(此,新SelectedItemChangedEventArgs(项目));
}
公共NativeListViewHorse()
{
}
}
当我调试填充内容的行时,我将获得以下数据:
为什么在变量中看到数据时没有显示任何内容?我做错什么了吗
致以亲切的问候,
是的,该链接中有许多不同的示例,因此很难确切知道您要遵循哪个示例。但您的NativeListView只是继承自View,实际上并没有做任何事情。视图只是一个空画布,您需要在其上放置其他元素(标签、按钮等)。@Jason我使用的是示例D,请参见:至于视图只是一个空画布。。我在D示例中找不到任何填充特定标签或任何内容的内容?iOS和Android项目下也有包含自定义平台代码的D文件夹。你需要这个我明白了。。我认为便携机上的代码就足够了。我猜这是一个糟糕的问题:有没有一种方法可以在不为每个平台编写单独代码的情况下构建这个多平台?我想坚持在我的可移植项目中保留所有内容。您可以创建自定义LIstView单元格模板,而无需为每个平台编写渲染代码。