c# - 如何从选定的选择器更新标签元素?(Xamarin 形式)
问题描述
我目前正在开展一个向您显示实时股票价格的项目。我想使用选择器系统获取任何符号的价格,该系统通过将所选项目添加到下载字符串请求的链接中来显示选择器中所选项目的价格。我遇到的问题是,当我更改选择器时,什么也没有发生。
C#代码:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Stock_WatchList
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class sybmolSelector : ContentPage
{
public string price { get; set; }
public sybmolSelector()
{
InitializeComponent();
BindingContext = this;
}
private void SymbolSelector_SelectedIndexChanged(object sender, EventArgs e)
{
Picker picker = sender as Picker;
var selectedItem = picker.SelectedItem;
var link = "https://sandbox.iexapis.com/stable/stock/" + selectedItem + "/price?token=Tsk_57bebc1d051340dbaad8656ab0027e90";
var client1 = new WebClient();
string a = client1.DownloadString(link);
price = "$" + a;
}
}
}
这是 Xamarin.Forms 代码:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Stock_WatchList.sybmolSelector"
BackgroundColor="Black"
NavigationPage.IconColor="LimeGreen">
<ContentPage.Content>
<StackLayout>
<Label TextColor="LimeGreen" BackgroundColor="Black" Text="----------------------- Select A Symbol -------------------------" FontSize="19" LineBreakMode="TailTruncation"></Label>
<Picker x:Name="Picker" SelectedIndexChanged="SymbolSelector_SelectedIndexChanged" TextColor="LimeGreen" BackgroundColor="#111111">
<Picker.Items>
<x:String>AAPL</x:String>
<x:String>NIO</x:String>
<x:String>AAPL</x:String>
<x:String>NIO</x:String>
<x:String>AAPL</x:String>
<x:String>NIO</x:String>
<x:String>AAPL</x:String>
<x:String>NIO</x:String>
<x:String>AAPL</x:String>
<x:String>NIO</x:String>
<x:String>AAPL</x:String>
<x:String>NIO</x:String>
<x:String>AAPL</x:String>
<x:String>NIO</x:String>
<x:String>AAPL</x:String>
<x:String>NIO</x:String>
</Picker.Items>
</Picker>
<Label Margin="5" Text="{Binding selectedItem}" HorizontalOptions="Start" TextColor="White" FontSize="40"/>
<Label Margin="5,0,5,5" Text="{Binding price}" HorizontalOptions="Start" TextColor="White" FontSize="50"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
在此先感谢,内森 T。
解决方案
为您的标签命名:
<Label x:Name="lblPicker" TextColor="LimeGreen" BackgroundColor="Black" Text="----------------------- Select A Symbol -------------------------" FontSize="19" LineBreakMode="TailTruncation"></Label>
然后您可以在后面的代码中访问此标签并将选择器值分配给标签:
private void SymbolSelector_SelectedIndexChanged(object sender, EventArgs e)
{
Picker picker = sender as Picker;
var selectedItem = picker.SelectedItem;
var link = "https://sandbox.iexapis.com/stable/stock/" + selectedItem + "/price?token=Tsk_57bebc1d051340dbaad8656ab0027e90";
var client1 = new WebClient();
string a = client1.DownloadString(link);
lblPicker.Text = "$" + a;
}
您也可以在 viewmodel 中执行此操作,但为此您需要将选择器的 ItemSource 属性与 ViewModel 的属性绑定,但在此之前您可以通过这种方式获取代码
推荐阅读
- date - 带有“T”和“Z”字符的日期字符串
- powerapps - 使用 Flow Automate 进行整数转换
- json - 如何从 1 个 S3 文件读取和写入多个 json 对象到 dynamodb python 3.8
- python - Django中子类用户的访问属性
- python - 使用 Python 烧瓶在网页上显示带有超链接的 Excel 数据表
- python - 在图像上应用两个阈值以检测边缘
- python - 如何阻止 pygame 中的精灵离开屏幕边缘?
- okhttp - gRPC 在 Android 下是否支持 TLS 1.3?
- azure - Azure 应用程序网关斜杠终止 uri 路径
- java - 无法在 Spring Boot 中设置 Strict-Transport-Security 标头