【发布时间】:2019-03-04 11:35:24
【问题描述】:
首先我用应用程序扫描二维码,然后应用程序发出 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
这是我从这里得到的模型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;
请帮帮我,我在过去的 2 天里一直卡在这个问题上,我真的不知道接下来要做什么......
在 5.03 年编辑此消息
我想要实现的目标:在 1 个选择器中获取“srednica”,并且每当有人选择例如“20”以显示他在选择器中的笔划第二个对应于 json 中的“srednica”
我的问题:它向我显示“srednica”,但它没有显示我的笔划和 ID。我尝试使用选择器(注释掉 listview)但我收到 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:如果从 silowniki 中选择显示笔画,则选择 srednica “20”(model[thisSrednicaIndex].Silowniki .Stroke) 用于选择器 2nd"
或者我必须在页面代码后面循环执行?
【问题讨论】:
-
您的问题是什么?您的代码中是否有任何错误或异常?具体是什么或没有按预期工作?您发布了太多代码,任何人都无法通过肉眼有效地进行调试。
-
@Jason 已经更新,它根本没有显示在选择器中,这就是问题
-
您的 Picker 绑定到您的 VM 中都不存在的“dia”和“stroke”。有一个“Dia”属性。
标签: c# json rest xamarin.forms