我的xamarin表单应用程序从我的Azure数据库中返回一个名为Users
的可观察集合。返回后,它将清除ListView
并将其替换为返回的json集合。
虽然它可以毫无问题地返回我的数据,但问题是它似乎只显示了最高级别的信息而没有进入id
和FirstName
。例如
Users.ReplaceRange(coffees);
清除列表视图并输出返回的集合。什么是输出CoffeeCups.users
我需要做的是显示FirstName
,这是CoffeeCup.users
的一层,例如
那么如何确保我的列表显示返回的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>
供参考:
users.cs
using System;
namespace CoffeeCups
{
public class users
{
public string id { get; set; }
public string FirstName { get; set; }
}
}
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(); ;
}
}
CoffeesViewModel.cs
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;
}
}
CoffeesPage.xaml.cs
protected override async void OnAppearing()
{
base.OnAppearing();
if (vm.Users.Count == 0)
vm.LoadCoffeesCommand.Execute(null);
else
{
vm.LoadCoffeesCommand.Execute(null);
}
}
答案 0 :(得分:0)
我找到了答案,因为我怀疑它实际上非常简单。如果我们查看ListView
,我们可以看到有一个名为IsGroupingEnabled
的选项设置为True
。
我现在知道这个选项实际上只是将任何返回的json集合组合成自己,有效地将结果捆绑到一个条目中,您可以公开与用户的交互。
将此选项切换为false(因为它不是我需要的东西)允许绑定的标签返回指定的内容。