I have a property that RaisePropertyChanged(PropName, oldValue, true, true) when I don’t have any connection to the internet no more but it throws the exception that I’m on the wrong thread.
So I want to update the property form my ViewModel but how do I get the current thread in my ViewModel or what is you proposal for a solution?
My ViewModel-ctor
public MyViewModel()
{
// START LISTENING TO NETWORKSTATUS
NetworkInformation.NetworkStatusChanged += OnNetworkStatusChangedHandler;
}
NetworkChanged-callback method
private async void OnNetworkStatusChangedHandler(object sender)
{
ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
if (profile == null)
{
IsRefreshEnabled = false;
}
else
{
IsRefreshEnabled = true;
}
}
My Property
public const string IsRefreshEnabledPropertyName = "IsRefreshEnabled";
private bool _isRefreshEnabled = true;
public bool IsRefreshEnabled
{
get { return _isRefreshEnabled; }
set
{
if (_isRefreshEnabled == value) { return; }
var oldValue = _isRefreshEnabled;
_isRefreshEnabled = value;
RaisePropertyChanged(IsRefreshEnabledPropertyName, oldValue, value, true);
}
}
Thanks in advance!
You need to replace the RaisePropertyChanged call to:
That will cause the call to RaisePropertyChanged to run on the UI thread.
I’m assuming that your class derives from a Xaml control (to access the Dispatcher property on the control).