我需要使用下載的 JSON 中的資料填充自定義 ListView。
這是我創建的自定義 ListView:
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<StackLayout Orientation="Vertical" Spacing="5" VerticalOptions="Center">
<StackLayout Orientation="Horizontal">
<Label Text="{Binding TradeDate}" Padding="25,0,0,0" FontSize="19" TextColor="Black"></Label>
<Label Text="{Binding TradeGain}" Padding="0,0,25,0" FontSize="19" FontAttributes="Bold" TextColor="Green" HorizontalOptions="EndAndExpand"></Label>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding TradeQty}" Padding="25,0,0,0" FontSize="16"></Label>
<Label Text="{Binding TradeEff}" Padding="0,0,25,0" FontSize="16" HorizontalOptions="EndAndExpand"></Label>
</StackLayout>
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
然后我將 BindingContext 設定為具有 ItemSource 的類:
<ContentPage.BindingContext>
<local:TradesView>
<x:Arguments>
<x:String>cacca</x:String>
</x:Arguments>
</local:TradesView>
</ContentPage.BindingContext>
這是 BindingContext 中的 TradeView 類:
不幸的是,我無法從這里下載 JSON,因為它需要身份驗證,這當然是由另一個 ContentPage 處理的。
public class TradesView
{
public ObservableCollection<Trade> Trades { get; set; }
public TradesView(string trades)
{
Trades = new ObservableCollection<Trade>();
JArray jArray = JArray.Parse(trades);
foreach (JObject trade in jArray){
Trades.Add(new Trade(trade["date"].ToString(), trade["gain"].ToString(), trade["depth"].ToString(), "0"));
}
}
}
問題是,現在我不知道如何繼續,因為似乎無法將變數作為引數傳遞給 TradeView 建構式,該建構式應該處理 JSON 并用我需要的內容填充 ObservableCollection。
我只是認為我的程式是錯誤的,我應該放棄我所做的很多事情,但是我真的不知道如何解決這個問題。
uj5u.com熱心網友回復:
有幾種方法可以解決這個問題。這是一個,它使用“工廠方法” ( Create) 創建包含您的視圖的頁面,然后設定該頁面的Items屬性。
如果只有一個頁面包含該視圖,這是有道理的。
用法:
var names = new List<string> { "One", "Two", "Three", "Four" };
MainPage = SelfBoundPageWithCollection.Create(names);
SelfBoundPageWithCollection.xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestXFUWP.SelfBoundPageWithCollection">
<ContentPage.Content>
<StackLayout>
<CollectionView ItemsSource="{Binding Items}">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding Name}" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
SelfBoundPageWithCollection.xaml.cs:
public partial class SelfBoundPageWithCollection : ContentPage
{
public SelfBoundPageWithCollection()
{
InitializeComponent();
BindingContext = this;
}
public ObservableCollection<ItemModel> Items {
get => _items;
set {
_items = value;
OnPropertyChanged();
}
}
private ObservableCollection<ItemModel> _items;
public static SelfBoundPageWithCollection Create(List<string> names)
{
var it = new SelfBoundPageWithCollection();
it.FillItems(names);
return it;
}
// Can call this directly later, to replace Items collection.
public void FillItems(List<string> names)
{
var items = new ObservableCollection<ItemModel>();
foreach (var name in names) {
items.Add(new ItemModel(name));
}
Items = items;
}
}
或者有一個單獨的“ViewModel”(MVVM),這樣做:
用法:
var names = new List<string> { "One", "Two", "Three", "Four" };
MainPage = PageWithCollection.Create(names);
PageWithCollection.xaml.cs:
public partial class PageWithCollection : ContentPage
{
public PageWithCollection()
{
InitializeComponent();
BindingContext = new ViewModelWithItems();
}
public static PageWithCollection Create(List<string> names)
{
var it = new PageWithCollection();
var vm = (ViewModelWithItems)it.BindingContext;
vm.FillItems(names);
return it;
}
}
ViewModelWithItems.cs:
public class ViewModelWithItems : Xamarin.Forms.BindableObject
{
public ViewModelWithItems()
{
}
public ObservableCollection<ItemModel> Items {
get => _items;
set {
_items = value;
OnPropertyChanged();
}
}
private ObservableCollection<ItemModel> _items;
public void FillItems(List<string> names)
{
var items = new ObservableCollection<ItemModel>();
foreach (var name in names) {
items.Add(new ItemModel(name));
}
Items = items;
}
}
上面兩個都是指這個模型:
專案模型.cs:
public class ItemModel
{
public ItemModel(string name)
{
Name = name;
}
public string Name { get; set; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/386823.html
標籤:列表显示 沙马林 xamarin.forms
