简体   繁体   中英

How to update a Label element from the selected picker? (Xamarin Forms)

I am currently working on a project that shows you real-time stock prices. I wanted to get a price for any symbol with a picker system that displays the price of the selected item in the picker by adding the selected item into the link of a download string request. The issue I am running into is that when I change the picker nothing happens.

C# code:

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;
        }
    }
}

And here is the Xamarin.Forms code:

<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>

Thanks in advance, Nathan T.

Give a name to your label:

<Label x:Name="lblPicker" TextColor="LimeGreen" BackgroundColor="Black" Text="----------------------- Select A Symbol -------------------------" FontSize="19" LineBreakMode="TailTruncation"></Label>

Then you can access this label in your code behind and assign the picker value to the 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;
        }

You can do this in viewmodel as well but for that you need to bind your picker's ItemSource property with ViewModel's property but till then you can you this way for code behind

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM