C# 如何指定要在列表视图中显示的json字段?
我让我的xamarin表单应用程序从我的Azure数据库返回一个名为C# 如何指定要在列表视图中显示的json字段?,c#,json,azure,xamarin,C#,Json,Azure,Xamarin,我让我的xamarin表单应用程序从我的Azure数据库返回一个名为Users的可观察集合。返回后,它将清除列表视图,并将其替换为返回的json集合 虽然它返回我的数据没有任何问题,但问题是它似乎只显示顶级信息,而没有深入到id和FirstName。比如说 Users.ReplaceRange(咖啡)清除listview并输出返回的集合。输出的是咖啡杯。用户我需要做的是显示名字,它比咖啡杯低一层。例如,用户 那么,如何确保列表显示返回的json集合中的正确字段呢 以下是我的列表视图的代码供参
Users
的可观察集合。返回后,它将清除列表视图
,并将其替换为返回的json集合
虽然它返回我的数据没有任何问题,但问题是它似乎只显示顶级信息,而没有深入到id
和FirstName
。比如说
Users.ReplaceRange(咖啡)代码>清除listview并输出返回的集合。输出的是咖啡杯。用户
我需要做的是显示名字
,它比咖啡杯低一层。例如,用户
那么,如何确保列表显示返回的json集合中的正确字段呢
以下是我的列表视图的代码供参考
CoffeesPage.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"
xmlns:local="clr-namespace:CoffeeCups;assembly=CoffeeCups"
x:Class="CoffeeCups.CoffeesPage"
Title="Cups Of Coffee">
<ListView
Grid.Row="1"
IsGroupingEnabled="true"
HasUnevenRows ="true"
ItemsSource="{Binding Users}"
IsPullToRefreshEnabled="true"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
RefreshCommand="{Binding LoadCoffeesCommand}"
x:Name="ListViewCoffees">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding FirstName}"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
AzureServices.cs
public class AzureService
{
public MobileServiceClient Client { get; set; } = null;
IMobileServiceSyncTable<users> userTable;
public async Task Initialize()
{
var appUrl = "https://myurl.azurewebsites.net";
//Create our client
Client = new MobileServiceClient(appUrl);
//InitialzeDatabase for path
var path = "syncstore.db";
path = Path.Combine(MobileServiceClient.DefaultDatabasePath, path);
//setup our local sqlite store and intialize our table
var store = new MobileServiceSQLiteStore(path);
//Define table
store.DefineTable<users>();
//Initialize SyncContext
await Client.SyncContext.InitializeAsync(store);
//Get our sync table that will call out to azure
userTable = Client.GetSyncTable<users>();
}
public async Task SyncCoffee()
{
try
{
if (!CrossConnectivity.Current.IsConnected)
return;
await userTable.PullAsync("allUsers", userTable.CreateQuery());
await Client.SyncContext.PushAsync();
}
catch (Exception ex)
{
Debug.WriteLine("Unable to sync coffees, that is alright as we have offline capabilities: " + ex);
}
}
public async Task<IEnumerable<users>> GetUsers()
{
//Initialize & Sync
await Initialize();
await SyncCoffee();
return await userTable.OrderBy(c => c.FirstName).ToEnumerableAsync(); ;
}
}
CoffeesPage.xaml.cs
using System;
namespace CoffeeCups
{
public class users
{
public string id { get; set; }
public string FirstName { get; set; }
}
}
async Task ExecuteLoadCoffeesCommandAsync()
{
try
{
LoadingMessage = "Loading Coffees...";
IsBusy = true;
var coffees = await azureService.GetUsers();
Users.ReplaceRange(coffees);
}
catch (Exception ex)
{
Debug.WriteLine("OH NO!" + ex);
await Application.Current.MainPage.DisplayAlert("Sync Error", "Unable to sync coffees, you may be offline", "OK");
}
finally
{
IsBusy = false;
}
}
protected override async void OnAppearing()
{
base.OnAppearing();
if (vm.Users.Count == 0)
vm.LoadCoffeesCommand.Execute(null);
else
{
vm.LoadCoffeesCommand.Execute(null);
}
}
我找到了答案,正如我所怀疑的那样,这其实很简单。如果我们查看一下列表视图
,我们可以看到有一个名为IsGroupingEnabled
的选项,它被设置为True
我现在知道,这个选项实际上只是将任何返回的json集合分组到它自己中,有效地将结果捆绑到一个条目中,您可以公开该条目以便与用户交互
将此选项切换为false(因为这不是我需要的)允许绑定标签返回指定的内容。用户类型是什么?@JasonUsers
是一个可观察的集合,如下所示:public observerangecollection Users{get;}=new observerangecollection()代码>请为您的用户发布代码class@Jason我更新了我原来的问题,加入了一些代码块供参考。谢谢根据你发布的内容,你的装订应该按原样工作。您在哪里初始化用户,以及在初始绑定完成后是否在某处重新初始化用户?