我还在学习中,请耐心等待。
我正在尝试在我的Xamarin.Form
中显示一个ListView
。到目前为止,这是我所拥有的,并且它工作得非常好:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
xmlns:local="clr-namespace:GasStations"
x:Class="GasStations.MainPage">
<StackLayout>
<ListView x:Name="ListView_Pets">
</ListView>
</StackLayout>
</ContentPage>
这是代码后端:
var obj = JsonConvert.DeserializeObject(coord);
List<String> storeList = new List<String>();
string store = string.Empty;
foreach (var item in ((JArray)obj))
{
store = item.Value<string>("name");
//desc = item.Value<string>("description"); /* This is the string I want to display */
storeList.Add(store);
}
ListView_Pets.ItemsSource = storeList;
它能够正常工作并在“ListView”中显示商店名称。您可以在此处查看截图。现在,除了在“ListView”中仅显示商店“Name”之外,我还想显示描述(以及其他任何值)。我认为我需要在每一行中使用标签,类似于“asp.net GridView ItemTemplate”。我尝试了这样的操作,但它无法构建:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
xmlns:local="clr-namespace:GasStations"
x:Class="GasStations.MainPage">
<StackLayout>
<ListView x:Name="ListView_Pets" ItemsSource="{Binding MyClass}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Label x:Name="Label_Name" Text="Name" />
<Label x:Name="Label_Desc" Text="Description" />
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
我可以在代码后端访问Name
和Description
(使用desc = item.Value<string>("description");
),但我不知道如何将这些值绑定到表单中的标签。
xaml
里有两个ViewCell
,而我没有注意到。 - fdkgfosfskjdlsjdlkfsf