【问题标题】:How to get symbol and price to show individually in a listview on xamarin c#如何在 xamarin c# 的列表视图中单独显示符号和价格
【发布时间】:2021-07-18 13:23:42
【问题描述】:

如何在 xamrain c# 中获取一个标签中的符号和另一个标签中的价格 我需要帮助的是如何使符号和价格在所有不同硬币的列表视图中一起出现。您可以在 GetPrice 下的链接中看到不同的符号和不同的价格。我只想将它们分组并将它们分隔在列表视图中。请帮帮我。

这是我的 MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Crypto_Portfolio.MainPage">
<StackLayout>
    <StackLayout Orientation="Horizontal" VerticalOptions="Start">
        <ListView x:Name="priceList">
          <ListView.ItemTemplate>
            <DataTemplate>
               <ViewCell>
                  <StackLayout>
                    <Label Text="{Binding symbol}" />
                    <Label Text="{Binding price}" />
                  </StackLayout>
               </ViewCell>
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
    <StackLayout Orientation="Horizontal" VerticalOptions="End">
        <Button Clicked="priceClick" Text="Fetch Price" HorizontalOptions="CenterAndExpand" VerticalOptions="End"/>
    </StackLayout>
</StackLayout>

</ContentPage>

这是我的 MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Http;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Xamarin.Forms;

namespace Crypto_Portfolio
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            GetPrice();
        }

        public class Crypto
        {
            public string symbol { get; set; }
            public string price { get; set; }
        }

        private async void GetPrice()
        {
            HttpClient client = new HttpClient();
            var response = await client.GetStringAsync("https://api.binance.com/api/v3/ticker/price");
            var cryptoconverted = JsonConvert.DeserializeObject<List<Crypto>>(response);
            priceList.ItemsSource = cryptoconverted;
        }

        void priceClick(object sender, EventArgs e)
        {
            GetPrice();
        }


    }
}


【问题讨论】:

  • 你的代码中没有ListView,只有一个标签。您希望我们为您编写所有这些内容吗?
  • “使符号和价格在所有不同硬币的列表视图中一起出现”到底是什么意思。 ?
  • @Jason 抱歉,我是新手。我不知道该怎么做。
  • @Cfun 您可以在 GetPrice 下的链接中看到不同的符号和不同的价格。我只想将它们分组并在列表视图中分开。
  • Xamarin 网站上有很多优秀的教程,包括多个完整的示例应用程序。我建议您花时间查看可用的内容

标签: c# json xamarin


【解决方案1】:

首先,您需要将 web 服务中的 json 反序列化 转换为 C# 类。您可以使用json2csharp.com 到此

public class Stock
{
    public string symbol { get; set; }
    public string price { get; set; }
}

然后在你的 GetPrice 方法中

var response = await client.GetStringAsync("https://api.binance.com/api/v3/ticker/price");
var stocks = JsonConvert.DeserializeObject<List<Stock>>(response);

接下来,将 ListView 添加到您的 XAML

<ListView x:Name="stockList">
  <ListView.ItemTemplate>
    <DataTemplate>
       <ViewCell>
          <StackLayout>
            <Label Text="{Binding symbol}" />
            <Label Text="{Binding price}" />
          </StackLayout>
       </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>
   

最后,将您的数据分配给GetPrice中的ListView

stockList.ItemsSource = stocks 

【讨论】:

  • 老实说,我在 15 分钟内从你那里学到的东西比我在学校的老师还多。老实说,我非常感谢您的所有帮助,先生。
  • 非常感谢杰森!
  • @TejasSethi 看来Jason的回复对你有帮助,请记得将此回复标记为答案,对其他社区有好处。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
  • 1970-01-01
  • 1970-01-01
  • 2016-02-09
  • 1970-01-01
相关资源
最近更新 更多