Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# How to implement an async method for real-time stock data retrieval...

jv1597

Coder
I'm trying to get a real-time stock data retrieval method working, but I haven't found any information on how to get the method to work with C#. Here's what I have so far:

In MainWindow.xaml:
Code:
<Grid x:Name="g" Width="Auto" Height="Auto" VerticalAlignment="Center">

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>
    
    <StackPanel Width="Auto" Height="Auto" Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBox x:Name="txtbox" Header="Please enter symbol to lookup:" PlaceholderText=" Type symbol here... " InputScope="Text" TextChanged="{x:Bind textChanged}" />
    </StackPanel>

    <Button x:Name="b" Grid.Row="1" Content="Submit" Click="symbolSearch" HorizontalAlignment="Center" VerticalAlignment="Center"></Button>

    <StackPanel Width="Auto" Height="Auto" Grid.Row="2" Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Frame x:Name="f" Content="{x:Bind Address}" />
    </StackPanel>

</Grid>

In MainWindow.xaml.cs:
Code:
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.ComponentModel;
using System.Runtime.CompilerServices;



namespace Realtime_Stock_Data_Lab
{
    public sealed partial class MainWindow : Window
    {
        public event PropertyChangedEventHandler PropertyChanged;
        
        private string _symbol;
        private string _address;

        public MainWindow()
        {
            this.InitializeComponent();
        }

        public void textChanged(object sender, TextChangedEventArgs e)
        {
            TextBox t = sender as TextBox;
            _symbol = t.Text;
            NotifyPropertyChanged(nameof(_symbol));
        }

        public void symbolSearch(object sender, RoutedEventArgs e)
        {
            f.Content = _symbol;
        }

        public async Task<int> getStockData(string sender, RoutedEventArgs e)
        {
            try
            {
                var httpClient = new HttpClient();
                httpClient.BaseAddress = new Uri("https://yfapi.net/");
                httpClient.DefaultRequestHeaders.Add("X-API-KEY", "<your API key>");
                httpClient.DefaultRequestHeaders.Add("accept", "application/json");

                var response = await httpClient.GetAsync("v11/finance/quoteSummary/" + _symbol + "?lang=en&region=US&modules=defaultKeyStatistics%2CassetProfile");
                var responseBody = await response.Content.ReadAsStringAsync();

                var data = (JObject)JsonConvert.DeserializeObject(responseBody);
                var address = data.SelectToken("quoteSummary.result[0].assetProfile.address1").Value<string>();
            }
            catch
            {
                Console.WriteLine("Failed to get symbol: " + _symbol);
            }

            return 1;
        }

        public string Address
        {
            get { return _address; }
            set
            {
                if (_address == value) return;
                _address = value;
                NotifyPropertyChanged(nameof(_address));
            }
        }

        private void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

I would like to know how to use the "getStockData" async method from xaml. As you can see at this time I have the content property of the frame element binding to "Address" as its binding source, but I need a way to call the async method to bring up the real-time data. Does anyone have any ideas?
 

New Threads

Buy us a coffee!

Back
Top Bottom