字符串变量的值不会改变

时间:2014-04-08 08:34:29

标签: c# variables

我已经下载了股票价值的字符串表示(Via Yahoo finance 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”

1 个答案:

答案 0 :(得分:0)

您需要在下载完成后更新实时图块 - 换句话说,从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仍设置为默认值。