【问题标题】:How to update a Label element from the selected picker? (Xamarin Forms)如何从选定的选择器更新标签元素? (Xamarin 形式)
【发布时间】:2021-05-01 15:37:10
【问题描述】:

我目前正在开展一个向您显示实时股票价格的项目。我想使用选择器系统获取任何符号的价格,该系统通过将所选项目添加到下载字符串请求的链接中来显示选择器中所选项目的价格。我遇到的问题是,当我更改选择器时没有任何反应。

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。

【问题讨论】:

  • 如果您希望绑定动态更新 UI,则需要实现 INotifyPropertyChanged。或者,您可以直接在代码中设置标签文本

标签: c# xamarin xamarin.forms


【解决方案1】:

为您的标签命名:

<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 的属性绑定,但在此之前您可以通过这种方式来处理后面的代码

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-12
    • 2023-01-29
    • 1970-01-01
    相关资源
    最近更新 更多