如何将数据绑定到TabbedPage中的多个listViews

时间:2017-06-28 15:24:02

标签: xamarin.forms

我有以下内容:

1)我有一个包含多个标签的TabbedPage 2)每个选项卡包含一个具有ListView的页面 3)每个Tab中每个ListView的数据基于REST服务的单个Json结果

问题:

我应该在哪里调用和使用Json REST Web服务以及如何将数据绑定到每个ListView?

say, at Code Behind of TabbedPage.xaml.cs

I start to call the Json Web service

var client = new System.Net.Http.HttpClient();  

var response = await client.GetAsync("Http://Rest WebService"); 

string GeneralJson = await response.Content.ReadAsStringAsync();  

GeneralClassforJsonList ObjGeneralList = new GeneralClassForJsonList();  

if (GeneralJson != "")  
 {  

     ObjGeneralList = JsonConvert.DeserializeObject<GeneralClassforJsonList>(generalList);  
}  


  var Product = ObjectGeneralList.Result.Products

  var Service = ObjectGeneralList.Result.Services

我想知道如何将数据分别绑定到每个列表视图 在产品和服务页面中:

listviewProduct.ItemsSource =产品

listviewService.ItemsSource =服务

更新(1):方法1

In TabbedPage, it has 2 or more tab.

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"          
    xmlns:local ="clr-namespace:SembIWIS.View"  
       x:Class="myApp.MainPage">

  <local:ProductPage>
  </local:ProductPage>

  <local:ServicePage>
  </local:ServicePage>

</TabbedPage>


1) 
I created a static Class in Model to store Json Result in string 

public static class ProductServiceJson
    {
        public static string StrProductServiceJson;
    }

2) 
In TabbedPage.xaml.cs (Code Behind)

 public partial class MainPage : TabbedPage
 {
    public MainPage()
    {
      InitializeComponent();
      Init();
    }

    private void Init()
    {
       //- code here not execute.

       //I start to call the REST api 

      var response = await client.GetAsync("Http://Rest WebService"); 
      string GeneralJson = await response.Content.ReadAsStringAsync();  

      ProductServiceJson.StrProductServiceJson = GeneralJson;

     }
 }

3) 
public partial class Product : ContentPage
 {
    public Product()
    {
        InitializeComponent();
        SetUpListData();
    }


    private void SetUpListData()
    {
       //-- code 
      // how come code here will be executed but not code in tabbedPage.xaml.cs?

    }
}

问题:

1)为什么TabbedPage.xaml.cs中的代码不能执行,而是Product.xaml.cs中的代码(Product.xaml后面的代码)?

2)当在Product.xaml.cs中执行代码时,由于TabbedPage.xaml.cs中的Web服务调用,因此StrProductService为空或null

3)Service.xaml.cs中的代码会被执行吗?

4)TabbedPage的执行顺序是什么,我的意思是哪个Tab将首次执行,如果有6个标签?

TIA

1 个答案:

答案 0 :(得分:0)

以下是如何做到这一点。我没有测试那段代码,只是写信给你一个想法。

class ObjGeneral 
{
    public string Name{get;set;}
}
public class MyViewModel
{
    public List<ObjGeneral> MyList{get;set;}
}

然后在页面代码中

var myViewModel=new MyViewModel();
//populate your list
myViewModel.MyList=JsonConvert.DeserializeObject<List<ObjGeneral>>(generalList);
BindingContext=myViewModel;

然后在xaml

 <ListView x:Name="toggleListView" ItemsSource="{Binding MyList}">
     <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <Label x:Name="lblPlaceName" Text="{Binding Name}" />
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
  </ListView>