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:
In MainWindow.xaml.cs:
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?
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®ion=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?