Fixed issue with not being on dispatcher thread

This commit is contained in:
Westin Miller
2017-10-31 04:43:50 -07:00
parent 178957642c
commit d30d16b855

View File

@@ -1,4 +1,5 @@
using System.ComponentModel; using System.ComponentModel;
using System.Threading;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using Torch.Server.Managers; using Torch.Server.Managers;
@@ -18,7 +19,7 @@ namespace Torch.Server.Views.Entities
DataContextChanged += OnDataContextChanged; DataContextChanged += OnDataContextChanged;
} }
private void OnDataContextChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e) private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{ {
if (e.OldValue is ViewModel vmo) if (e.OldValue is ViewModel vmo)
{ {
@@ -43,6 +44,12 @@ namespace Torch.Server.Views.Entities
private void RefreshControl() private void RefreshControl()
{ {
if (Dispatcher.Thread != Thread.CurrentThread)
{
Dispatcher.InvokeAsync(RefreshControl);
return;
}
_currentControl = DataContext is EntityControlViewModel ecvm _currentControl = DataContext is EntityControlViewModel ecvm
? TorchBase.Instance?.Managers.GetManager<EntityControlManager>()?.CreateControl(ecvm) ? TorchBase.Instance?.Managers.GetManager<EntityControlManager>()?.CreateControl(ecvm)
: null; : null;
@@ -52,6 +59,11 @@ namespace Torch.Server.Views.Entities
private void RefreshVisibility() private void RefreshVisibility()
{ {
if (Dispatcher.Thread != Thread.CurrentThread)
{
Dispatcher.InvokeAsync(RefreshVisibility);
return;
}
Visibility = (DataContext is EntityControlViewModel ecvm) && !ecvm.Hide && _currentControl != null Visibility = (DataContext is EntityControlViewModel ecvm) && !ecvm.Hide && _currentControl != null
? Visibility.Visible ? Visibility.Visible
: Visibility.Collapsed; : Visibility.Collapsed;