using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Markup; using Tango.Core.DI; using Tango.FSE.Common.Notifications; using Tango.FSE.Common.Threading; namespace Tango.FSE.Procedures { public class DialogController : IDialogController { private String _xaml; private FrameworkElement _rootElement; private DialogControllerVM _dialogVM; [TangoInject] private INotificationProvider NotificationProvider { get; set; } [TangoInject] private IDispatcherProvider DispatcherProvider { get; set; } internal DialogController(String xaml) { _xaml = xaml; _dialogVM = new DialogControllerVM(); TangoIOC.Default.Inject(this); } internal void Init() { TaskCompletionSource completion = new TaskCompletionSource(); DispatcherProvider.Invoke(() => { FrameworkElement rootElement; var stream = new MemoryStream(); var writer = new StreamWriter(stream); writer.Write(_xaml); writer.Flush(); stream.Position = 0; rootElement = (FrameworkElement)XamlReader.Load(stream); _rootElement = rootElement; stream.Dispose(); completion.SetResult(true); }); completion.Task.GetAwaiter().GetResult(); } public void Show() { TaskCompletionSource completion = new TaskCompletionSource(); DispatcherProvider.Invoke(async () => { await NotificationProvider.ShowDialog(_dialogVM, _rootElement); completion.SetResult(true); }); completion.Task.GetAwaiter().GetResult(); } public void Close() { _dialogVM?.Close(true); } public T FindControl(String name) where T : DependencyObject { if (Thread.CurrentThread == Application.Current.Dispatcher.Thread) { return _rootElement.FindChild(name) as T; } else { bool completed = false; T child = null; DispatcherProvider.Invoke(() => { child = _rootElement.FindChild(name) as T; completed = true; }); while (!completed) { Thread.Sleep(10); } return child; } } } }