Xamarin.forms 如何将数据绑定到TabbedPage中的多个ListView
我有以下几点:Xamarin.forms 如何将数据绑定到TabbedPage中的多个ListView,xamarin.forms,Xamarin.forms,我有以下几点: 1) 我有一个包含多个选项卡的选项卡页。 2) 每个选项卡都包含一个包含ListView的页面 3) 每个选项卡中每个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 Sy
1) 我有一个包含多个选项卡的选项卡页。
2) 每个选项卡都包含一个包含ListView的页面
3) 每个选项卡中每个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
比如,在TabbedPage.xaml.cs的代码后面
我开始调用Json Web服务
var client=new System.Net.Http.HttpClient();
var response=wait client.GetAsync(“Http://Rest 网络服务);
字符串GeneralJson=wait response.Content.ReadAsStringAsync();
GeneralClassforJsonList ObjGeneralList=新的GeneralClassforJsonList();
if(GeneralJson!=“”)
{
ObjGeneralList=JsonConvert.DeserializeObject(generalList);
}
var Product=ObjectGeneralList.Result.Products
var Service=ObjectGeneralList.Result.Services
我想知道如何分别将数据绑定到每个ListView
在产品和服务页面中:
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?
}
}
在TabbedPage中,它有2个或更多选项卡。
1)
我在模型中创建了一个静态类,以字符串形式存储Json结果
公共静态类ProductServiceJson
{
公共静态字符串StrProductServiceJson;
}
2)
在TabbedPage.xaml.cs中(代码隐藏)
公共部分类主页面:选项卡页
{
公共主页()
{
初始化组件();
Init();
}
私有void Init()
{
//-这里的代码不能执行。
//我开始调用restapi
var response=wait client.GetAsync(“Http://Rest 网络服务);
字符串GeneralJson=wait response.Content.ReadAsStringAsync();
ProductServiceJson.StrProductServiceJson=GeneralJson;
}
}
3)
公共部分类产品:ContentPage
{
公共产品()
{
初始化组件();
SetUpListData();
}
私有void SetUpListData()
{
//--代码
//为什么这里的代码会被执行,而不是tabbedPage.xaml.cs中的代码?
}
}
问题:
1) 为什么TabbedPage.xaml.cs中的代码不执行,而在Product.xaml.cs中执行(Product.xaml的代码隐藏)?2) 当在Product.xaml.cs中执行代码时,StrProductService为空或null,因为web服务在TabbedPage.xaml.cs中调用
3) 是否将执行Service.xaml.cs中的代码 4) TabbedPage的执行顺序是什么,我的意思是哪个选项卡将第一次执行,如果有6个选项卡
TIA以下是您可以做到的方法。我没有测试代码,只是写了一些想法
class ObjGeneral
{
public string Name{get;set;}
}
public class MyViewModel
{
public List<ObjGeneral> MyList{get;set;}
}
类对象通用
{
公共字符串名称{get;set;}
}
公共类MyViewModel
{
公共列表MyList{get;set;}
}
然后在页面代码中
var myViewModel=new MyViewModel();
//populate your list
myViewModel.MyList=JsonConvert.DeserializeObject<List<ObjGeneral>>(generalList);
BindingContext=myViewModel;
var myViewModel=new myViewModel();
//填充您的列表
myViewModel.MyList=JsonConvert.DeserializeObject(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>
我不明白问题是什么。您有2个选项:1.对父选项卡页使用一个模型,您的选项卡可以访问它。2.将相同的模型传递给每个选项卡页。模型应该需要JSON响应。将模型中的JSON解析为对象列表后,使用ItemsSource填充列表。这就是你想要的吗?@Yuri,我的问题是如何根据你的方法1和方法2,将使用JsonConvert inTabbedPage的代码反序列化Json结果的数据(如我的代码中所示)传递到每个选项卡。你能指给我看吗?请参考我的方法1。如何处理TabbedPage及其包含的页面中的代码执行?我不明白你在问什么?你解决了你想做的事情了吗?或者你想澄清这个问题?