字符串变量的值不会更改

本文关键字:变量 字符串 | 更新日期: 2023-09-27 17:57:24

我已经下载了一个股票价值的字符串表示(通过雅虎财经API)。但是,当我尝试将值分配给 String 变量时,它会保留其以前的值并且不会更改。这是代码。

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }
    String result = "Default";
    public void wb_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e)
    {
        result = e.Result;
        //I changed the application title to be sure the required string was in fact downloaded
        ApplicationTitle.Text = e.Result;
    }
    //When I Click a radio button, the tile is created and inialized
    private void radioButton1_Checked(object sender, RoutedEventArgs e)
    {
        WebClient wb = new WebClient();
        wb.DownloadStringAsync(new Uri("http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=a"));
        wb.DownloadStringCompleted += wb_DownloadStringCompleted;
        //string result = DownloadStringCompletedEventArgs.result;
        int newCount = 0;
        // Application Tile is always the first Tile, even if it is not pinned to Start.
        ShellTile TileToFind = ShellTile.ActiveTiles.First();
        // Application should always be found
        if (TileToFind != null)
        {
            // Set the properties to update for the Application Tile.
            // Empty strings for the text values and URIs will result in the property being cleared.
            StandardTileData NewTileData = new StandardTileData
            {
                Title = "Stocks",
                BackgroundImage = new Uri("google_icon.jpg", UriKind.Relative),
                Count = newCount,
                BackTitle = "Google Stock",
                BackBackgroundImage = new Uri("google_icon.jpg", UriKind.Relative),
                //This is where the problem is. The value of result is still "Default"
                BackContent = result.ToString()
            };
            // Update the Application Tile
            TileToFind.Update(NewTileData);
        }
    }
}

我正在尝试在动态磁贴中显示结果,它显示值"默认"而不是股票值"548.47"

字符串变量的值不会更改

下载完成后需要更新动态磁贴 - 换句话说,从wb_DownloadStringCompleted处理程序中更新。

public void wb_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e)
{
    //this can probably be a local variable
    result = e.Result;
    //update live tile here.
}

当前代码正在启动异步任务,然后立即更新磁贴。由于任务尚未完成,因此result仍设置为默认值。