简体   繁体   中英

how to dynamically change Textblock

I am newbie in wpf and c#. Trying to make real-time notifications, I have a textblock that contains the latest information from webpage.Every 5 minutes i receive new information,which should be displayed on textblock, I have succesfully received information, but when I set this as property of textblock, it doesnt change,it still shows first received info. My code:

    async void GetChanges()
    {
        Rootobject allEarthquakes = await Async();
        int count = allEarthquakes.features.Length;

        LastData.Text = "Последнее землетрясение: \nМагнитуда: " + allEarthquakes.features[count - 1].properties.mag + "\nРасположение: " + allEarthquakes.features[count - 1].properties.place + "\nВремя: " + DateTimeOffset.FromUnixTimeMilliseconds(allEarthquakes.features[count - 1].properties.time) + "\nГлубина: " + allEarthquakes.features[count - 1].geometry.coordinates[2] + "\nID: " + allEarthquakes.features[count - 1].id;


    }
    Task<Rootobject> Async()
    {
        string path = @"C:\data\data.json";
        string json;
        string url = @"https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson";
        using (WebClient client = new WebClient())
        {
            json = client.DownloadString(url);
        }
        Rootobject allEarthquakes = JsonConvert.DeserializeObject<Rootobject>(json);
        int count = allEarthquakes.features.Length;
        if (File.Exists(path))
        {
            File.Delete(path);
        }
        Directory.CreateDirectory(@"C:\data");
        using (StreamWriter stream = File.CreateText(path))
        {
            stream.Write(json);
        }
        return Task.Run(() =>
            allEarthquakes);
    }

Where, LastData is Textblock

If the TextBlock refreshes when you change tabs than it sounds to me like you are not implementing the INotifyPropertyChanged interface. Normally you would bind TextBlock.Text in xaml like so

<TextBlock Text={Binding UpdateText} />

And then have some string property UpdateText in your code-behind, see this MSDN article

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