[英]Json array to Picker in Xamarin Forms
首先,我用該應用程序掃描了二維碼,然后該應用程序發出了api請求,並且我從服務器中獲得了json響應。
我想從我的api中獲取一個json並將其顯示在xaml的選擇器中,讓我向您展示我的json
[{"srednica":"20","silowniki":[{"stroke":"25","id":"1222","price":"118.00"},{"stroke":"40","id":"1224","price":"121.60"},{"stroke":"75","id":"1542","price":"130.00"},{"stroke":"230","id":"1545","price":"167.20"},{"stroke":"275","id":"1546","price":"178.00"}]},{"srednica":"16","silowniki":[{"stroke":"125","id":"1223","price":"116.00"},{"stroke":"150","id":"1225","price":"119.00"},{"stroke":"80","id":"1537","price":"110.60"},{"stroke":"120","id":"1538","price":"115.40"}]},{"srednica":"25","silowniki":[{"stroke":"25","id":"1247","price":"126.75"},{"stroke":"180","id":"1556","price":"168.60"},{"stroke":"185","id":"1557","price":"169.95"},{"stroke":"220","id":"1558","price":"179.40"}]},{"srednica":"12","silowniki":[{"stroke":"40","id":"1373","price":"99.00"},{"stroke":"150","id":"1533","price":"110.00"},{"stroke":"200","id":"1534","price":"115.00"}]},{"srednica":"10","silowniki":[{"stroke":"10","id":"1384","price":"92.00"},{"stroke":"30","id":"1577","price":"94.00"}]}]
其中“ 20”是直徑,並且必須是唯一值,必須在第一個選擇器中使用,而在第二個選擇器中,我需要將沖程,id和價格設置為相應的直徑,例如:
我選擇了直徑= 20,在第二個選擇器中,我從該直徑中獲得了所有筆划((loop)dia [“ 20”] [i] .stroke <-這就是我在javascript中的處理方式。)。
這是我從這里獲得的模型https://app.quicktype.io/
public class Diameter : INotifyPropertyChanged
{
[JsonProperty("srednica")]
public string Srednica { get; set; }
[JsonProperty("silowniki")]
public List<Silowniki> Silowniki { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Silowniki
{
[JsonProperty("stroke")]
public string Stroke { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("price")]
public string Price { get; set; }
}
我的視圖模型
public class ScannedViewModel : INotifyPropertyChanged
{
public ObservableCollection<Silowniki> Silowniki { get; set; }
public ObservableCollection<Diameter> Dia { get; set; }
public Diameter SelectedDiameter { get; set; }
public Diameter SelectedSilowniki { get; set; }
public ScannedViewModel()
{
Silowniki = new ObservableCollection<Silowniki>();
Dia = new ObservableCollection<Diameter>();
}
我的json響應在這里:
var responseText = streamReader.ReadToEnd();
if (response.StatusCode == HttpStatusCode.OK)
{
return responseText;
這是我將json添加到直徑的方法,我懷疑它是正確的方法..
ScannerPage.OnScanResult += (result) =>
{
ScannerPage.IsScanning = false;
Device.BeginInvokeOnMainThread(async () =>
{
await Navigation.PopAsync();
dynamic jsonRespone = await ConnectWithOauth.GetRequest(result.Text);
var test = JsonConvert.DeserializeObject<List<Diameter>>(jsonRespone);
ScannedProducts nextPage = new ScannedProducts(test);
//nextPage.BindingContext = viewModel;
Console.WriteLine("Mydia pomyslnie init");
await Navigation.PushAsync(nextPage);
最后是我的xaml。
<Picker Title="Test"
x:Name="picker"
ItemDisplayBinding="{Binding Silowniki.Id}">
</Picker>
<Picker Title="test"
x:Name="kupa"
ItemDisplayBinding="{Binding Silowniki.Stroke}">
</Picker>
<ListView
x:Name="ListaProduktow"
CachingStrategy="RecycleElement"
RowHeight="60"
ItemSelected="OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ContentView>
<Label Text="{Binding Srednica}">
</Label>
</ContentView>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我在“ ScannedProducts”頁面上綁定到選擇器:
public ScannedProducts(dynamic test)
{
InitializeComponent();
picker.ItemsSource = test;
kupa.ItemsSource = test;
BindingContext = test;
請幫助我,我在過去的兩天里一直停留在此,我真的不知道下一步該怎么做...
編輯了此消息5.03
我要實現的目標:在1個選擇器中獲取“ srednica”,並且每當有人選擇“ 20”以在選擇器2中向他顯示筆畫時,對應於json中的“ srednica”
我的問題:它向我顯示“ srednica”,但未向我顯示筆觸和ID。我嘗試使用選擇器(注釋列表視圖),但收到Java錯誤“ Java.Lang.NullPointerException:嘗試調用虛擬方法'java。 lang.String java.lang.Object.toString()'“
我知道JSON已正確反序列化到模型中,因為我已經檢查過了,元素在那里,我想我不知道如何在選擇器中顯示它。
告訴我要做我想實現的目標需要做什么?
編輯:
如果我在Page.cs中這樣做:
kupa.ItemsSource = model[0].Silowniki;
在我的xaml中:
<Picker Title="test"
x:Name="kupa"
ItemDisplayBinding="{Binding Stroke}">
</Picker>
它確實顯示了筆畫,但是僅顯示了元素0 :)我該怎么做:“選擇器1:如果選擇的srednica” 20“是從silowniki中顯示的筆畫(模型[thisSrednicaIndex] .Silowniki.Stroke)用於第二個拾取器
還是我必須在頁面代碼后面循環執行?
不幸的是,您不了解一些基本的知識,並且經常在多天內學習它們是正常的。
我要注意的第一件事是您不了解數據綁定的工作方式,因為您根本沒有在視圖模型中綁定到的字段。 可能這是一個完整的答案,但是可能還存在其他問題,您必須學習這些東西,沒有人可以為您做到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.