【问题标题】:how to bind ListView to Dictionary with a custom Value class如何使用自定义值类将 ListView 绑定到 Dictionary
【发布时间】:2022-01-21 13:07:43
【问题描述】:

更新:

我已经尝试过@Jason 他的建议,即在 1 个 API 调用中完成所有操作

它的代码:

public static async Task<Dictionary<string, Cost>> GetCoinsAndPrice()
    {
        using (HttpClient client = GetHttpClient())
        {
            try
            {
                string coinurl = $"{BASEURI}public/currency";

                
                string coinjson = await client.GetStringAsync(coinurl);
                if (coinjson != null)
                {
                    Dictionary<string, Coin> coin = JsonConvert.DeserializeObject< Dictionary < string, Coin >>(coinjson);
                    Debug.WriteLine("coinnn" + coin.Values);

                        string priceurl = $"{BASEURI}public/price/rate?from={coin.Keys}&to=BTC";
                        string pricejson = await client.GetStringAsync(priceurl);
                        if (pricejson != null)
                        {
                            return  JsonConvert.DeserializeObject<Dictionary<string, Cost>>(pricejson);
                        //JsonConvert.DeserializeObject<Dictionary<string, Cost>>(pricejson);

                      
                    }
                        return null;
                    
                }
                return null;
                
            }
            catch (Exception ex)
            {
                throw ex;

            }
        }

更新的 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"
    xmlns:user ="clr-namespace:ProjectCrypto.Views"
    x:Class="ProjectCrypto.Views.Overview">
    
    <ContentPage.Content>
        <StackLayout BackgroundColor="Gold">
            <Label FontAttributes="Bold" Text="Crypto coins" FontSize="Large" TextColor="Black" Margin="2,16"/>
            <StackLayout>
            <ListView x:Name="lvwOverview" RowHeight="100">
               
                <ListView.ItemTemplate ItemsSource="{Binding Cost}">
                
                <DataTemplate>
                    <ViewCell>
                        <Grid BackgroundColor="White">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="100"/>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            
                            <Label Grid.Column="1" Text="{Binding Key}" VerticalOptions="Center" />
                            <Label x:Name="lvwPrice" Grid.Column="2" Text="{Binding Value.price}" VerticalOptions="Center"/>
                            <Label Grid.Column="3" Text=">" HorizontalOptions="End" Margin="0,0,16,0" VerticalOptions="Center" TextColor="Black" FontAttributes="Bold"/>
                            
                        </Grid>
                    </ViewCell>
                </DataTemplate>
               
            </ListView.ItemTemplate>
            </ListView>
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

我现在遇到了如何使用 Binding 访问价格的问题。我正在尝试的一切都不起作用。

Dictionary&lt;string, Cost&gt;是我正在访问的,价格应该是成本。

成本等级:

 public class Cost
    {
        public string Name { get; set; }

        [JsonProperty(PropertyName = "currency")]
        public string currency { get; set; }

        [JsonProperty(PropertyName = "price")]
        public double price { get; set; }

        [JsonProperty(PropertyName = "timestamp")]
        public DateTime timestamp { get; set; }

    }
  
}

JSON 响应的成本结构如下:

    {
  "ETH": {
    "currency": "BTC",
    "price": "0.021084",
    "timestamp": "2021-06-02T17:52:36.731Z"
  }
}

硬币的 JSON 响应:

 {
  "BTC": {
    "full_name": "Bitcoin",
    "payin_enabled": true,
    "payout_enabled": true,
    "transfer_enabled": true,
    "precision_transfer": "0.00000001",
    "networks": [
      {
        "network": "btc",
        "protocol": "",
        "default": true,
        "payin_enabled": true,
        "payout_enabled": true,
        "precision_payout": "0.00000001",
        "payout_fee": "0.000900000000",
        "payout_is_payment_id": false,
        "payin_payment_id": false,
        "payin_confirmations": 1,
        "low_processing_time": "21.709",
        "high_processing_time": "3639.385",
        "avg_processing_time": "421.6391704545454"
      }
    ]
  },
  "USDT": {
    "full_name": "Tether",
    "payin_enabled": true,
    "payout_enabled": true,
    "transfer_enabled": true,
    "precision_transfer": "0.01",
    "networks": [
      {
        "network": "BTC",
        "protocol": "OMNI",
        "default": true,
        "payin_enabled": true,
        "payout_enabled": true,
        "precision_payout": "0.01",
        "payout_fee": "45.000000000000",
        "payout_is_payment_id": false,
        "payin_payment_id": false,
        "payin_confirmations": 2,
        "low_processing_time": "3.291",
        "high_processing_time": "1495.602",
        "avg_processing_time": "85.98873076923078"
      }
    ]
  }
}

【问题讨论】:

  • 我想因为你是 LoadData();在 Overview 和 OverviewViewCell 中都有?
  • OverviewViewCell 听起来像是网格中的一个单元格。所以你为网格中的每个单元格调用 LoadData()?
  • @Charles 可能就是这样!想知道如何在 overview.xaml.cs 中执行此操作,因为我无法在其中调用 lvwPrice 属性。或者我将如何解决它
  • MVVM 将是解决此问题的更好方法。谷歌xamarin forms mvvm listview。了解如何使用视图模型中的项目列表。
  • 我在对你上一个问题的评论中提到了这个

标签: c# xamarin xamarin.forms xamarin.android


【解决方案1】:

这是一个将ListView 绑定到Dictionary 的简单示例。您可以使用绑定表达式 Value.Property 绑定到 Dictionary 的 Value 的属性 - 例如,Value.CostValue.Name 以我的示例

<ListView x:Name="lv">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding Key}" Detail="{Binding Value.Cost}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

以及背后的代码

public partial class MainPage : ContentPage
{
    public class Test
    {
        public string Name { get; set; }
        public double Cost { get; set; }
    }

    public MainPage()
    {
        InitializeComponent();

        Dictionary<string, Test> dict = new Dictionary<string, Test>();

        dict.Add("1", new Test { Name = "One", Cost = 1.0 });
        dict.Add("2", new Test { Name = "Two", Cost = 2.0 });
        dict.Add("3", new Test { Name = "Three", Cost = 3.0 });
        dict.Add("10", new Test { Name = "Ten", Cost = 10.0 });

        lv.ItemsSource = dict;
    }

【讨论】:

    猜你喜欢
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    相关资源
    最近更新 更多