2017-03-01 49 views
0

我有我的xamarin窗體應用程序從我的Azure數據庫返回一個觀察的集合稱爲Users。一旦返回,它將清除ListView並用返回的json集合替換它。如何指定要在我的Xamarain列表視圖中顯示的json字段?

儘管它沒有任何問題地返回我的數據,但問題在於,它似乎只顯示頂級信息而沒有進入idFirstName。例如

01 Users.ReplaceRange(coffees);清除listview並輸出返回的集合。什麼是產出是CoffeeCups.users我需要做的是顯示FirstName這是一層由CoffeeCup.users例如

02

因此,如何能確保我的名單從我返回的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

用戶類型是什麼? – Jason

+0

@Jason'用戶'是一個可觀察的集合,如下所示:'public ObservableRangeCollection Users {get; } =新ObservableRangeCollection ();' – Yanayaya

+0

請張貼代碼爲您的用戶類 – Jason

回答

0

我找到了答案,因爲我懷疑它實際上是很簡單的東西。如果我們看一看在ListView我們可以看到,有一個名爲IsGroupingEnabled被設置爲True的選項。

我現在知道,這個選項會實際上只是集團的任何返回的JSON收集到自身用途不同捆綁的結果到一個單一的入口,你可以公開與用戶的互動。

切換這個選項爲false(因爲它不是我需要的)允許綁定的標貼返回指定什麼。

相關問題