I'm trying to update my UWP App's UI from a non-UI thread. I'm using Dispatcher.RunAsync to make the changes. I have tried changing:
ToggleSwitch.OnContentandOffContentImage.SourceCheckbox.IsCheckedCheckbox.Content
Nothing changes.
Currently, I'm reading values from the local settings cache (ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings), but I have tried using bools. No difference.
When debugging, Visual Studio indicates that the properties like Checkbox.IsChecked, etc. do change. However, in the app itself, no changes occur visually.
My current XAML code for the Checkboxes:
<CheckBox x:Name="checkBox1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="200,93,0,0"/>
<CheckBox x:Name="checkBox2" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="200,125,0,0"/>
<CheckBox x:Name="checkBox3" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="200,157,0,0"/>
The cs code:
public async void updateUI()
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
// change checkBox1 UI State
checkBox1.IsChecked = (Convert.ToBoolean(localSettings.Values["test1"])) ? true : false;
checkBox1.Content = (checkBox1.IsChecked.Value) ? "On" : "Off";
// change checkBox2 UI State
checkBox2.IsChecked = (Convert.ToBoolean(localSettings.Values["test2"])) ? true : false;
checkBox2.Content = (checkBox2.IsChecked.Value) ? "On" : "Off";
// change checkBox3 UI State
checkBox3.IsChecked = (Convert.ToBoolean(localSettings.Values["test3"])) ? true : false;
checkBox3.Content = (checkBox3.IsChecked.Value) ? "On" : "Off";
});
}
I call updateUI by simply invoking it, like this: updateUI(). And, no, there is no exception or error. Just nothing happens visually.
Any ideas on what I'm doing wrong??
Edit
I am unable to use Dispatcher.BeginInvoke as it is absent in a UWP Environment.
Edit 2
What I've noticed is that: if I put the same code (without the Dispatcher.RunAsync) in a UI event like a button click event, it runs perfectly. Can't one, somehow replicate this programmatically?
I have tried simply invoking an eventhandler method (like btnClickMe_Click) both with and without the Dispatcher.RunAsync code. However, that also does not cause any visual change whatsoever.