【发布时间】: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<string, Cost>是我正在访问的,价格应该是成本。
成本等级:
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