我有我的xamarin窗體應用程序從我的Azure數據庫返回一個觀察的集合稱爲Users
。一旦返回,它將清除ListView
並用返回的json集合替換它。如何指定要在我的Xamarain列表視圖中顯示的json字段?
儘管它沒有任何問題地返回我的數據,但問題在於,它似乎只顯示頂級信息而沒有進入id
和FirstName
。例如
Users.ReplaceRange(coffees);
清除listview並輸出返回的集合。什麼是產出是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
@Jason'用戶'是一個可觀察的集合,如下所示:'public ObservableRangeCollection Users {get; } =新ObservableRangeCollection ();' –
Yanayaya
請張貼代碼爲您的用戶類 – Jason