【发布时间】:2017-03-01 10:50:15
【问题描述】:
我的 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);
}
}
【问题讨论】:
-
用户的类型是什么?
-
@Jason
Users是一个 Observable 集合,如下所示:public ObservableRangeCollection<users> Users { get; } = new ObservableRangeCollection<users>(); -
请发布您的用户类的代码
-
@Jason 我已经更新了我的原始问题以包含一些代码块以供参考。谢谢
-
根据您发布的内容,您的绑定应该按原样工作。您在哪里初始化用户,您是否可能在初始绑定完成后重新初始化它?