locked
How to update GUI elements in a frame control from async task RRS feed

  • Question

  • Hello everyone,

    I'm currently developing an application on the RPI2 which reads a couple of sensors and reports the values to the GUI.

    My GUI looks a lot like the default windows 10 IOT app when you start the RPI2. So I have a appbar which has two appbartogglebuttons and each button loads a separate view in a frame control on the main page. One of those views is the home page of course and this needs to display a reading from a sensor that is updated every second.

    before I used a frame I added two simple textblocks to the main page and I was able to update those from an async task using progress/Iprogress methodology.

    _vijversturing.AnalogInputs[0].ValueString = new Progress<String>(s => txtWaterTemperatureValue.Text = s);
    

    The AnalogInput object has a property that allows me to bind it to a GUI element. What's more, this very same object has update interval setting (refresh rate). If I set this to 1 second, then the analog input will update the GUI element each second with it's latest value.

    If now were to add the textblock txtWaterTemperatureValue to a separate page that is being loaded in a frame on the main page then I can't access this textblock txtWaterTemperatureValue  due to it's protection level.

    Which technique do I need to use to access elements that are being displayed in a frame so that I can update them from an async task?

    Sunday, February 28, 2016 5:11 PM

Answers

  • Kabonacha,

    Is there a reason you are not using the normal binding capabilities in XAML?

    Regardless, any time you want to update the UI from a non-UI thread, you can use the CoreDispatcher's RunAsync() method:

    https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.core.coredispatcher.aspx

    So, where ever you need to update the UI, you can use Dispatcher.RunAsync():

    //In a UWP app, the global Dispatcher object is a Windows.UI.Core.CoreDispatcher
    //It gives you a way to run code on the main UI thread
    Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, 
      () => {
        //Your UI Update Code Goes Here
      }
      );
    

    Hope that helps,

    Bret

    Tuesday, March 1, 2016 4:02 PM

All replies

  • Kabonacha,

    Is there a reason you are not using the normal binding capabilities in XAML?

    Regardless, any time you want to update the UI from a non-UI thread, you can use the CoreDispatcher's RunAsync() method:

    https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.core.coredispatcher.aspx

    So, where ever you need to update the UI, you can use Dispatcher.RunAsync():

    //In a UWP app, the global Dispatcher object is a Windows.UI.Core.CoreDispatcher
    //It gives you a way to run code on the main UI thread
    Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, 
      () => {
        //Your UI Update Code Goes Here
      }
      );
    

    Hope that helps,

    Bret

    Tuesday, March 1, 2016 4:02 PM
  • Hey Bret,

    Sorry for my late response here.
    To be honest, I didn't even know that binding existed...

    I did some research and I think MVVM is the way to go, which basically comes down to binding.
    Never used it before, I'm new to all of this but found some great video on the Microsoft virtual academy!

    Thanks!

    Saturday, March 12, 2016 9:42 AM