Example #1
0
        protected static MetroWindow GetMetroWindow(object context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (!DialogParticipation.IsRegistered(context))
            {
                throw new InvalidOperationException(
                          "Context is not registered. Consider using DialogParticipation.Register in XAML to bind in the DataContext.");
            }

            var association = DialogParticipation.GetAssociation(context);
            var metroWindow = association.Invoke(() => Window.GetWindow(association) as MetroWindow);

            if (metroWindow == null)
            {
                throw new InvalidOperationException("Control is not inside a MetroWindow.");
            }
            return(metroWindow);
        }
        private static async Task <MetroWindow> GetMetroWindowAsync(object context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (!DialogParticipation.IsRegistered(context))
            {
                var tcs = new TaskCompletionSource <DependencyObject>();
                ContextRegistrationChangedEventHandler handler = null;
                handler = (c, o) =>
                {
                    if (c == context)
                    {
                        DialogParticipation.ContextRegistrationChanged -= handler;
                        tcs.TrySetResult(o);
                    }
                };
                DialogParticipation.ContextRegistrationChanged += handler;

                var task = tcs.Task;
                if (await task.WithTimeout(TimeSpan.FromSeconds(2)))
                {
                    return(AssociationToWindow(task.Result));
                }
                else
                {
                    throw new InvalidOperationException(
                              "Context is not registered. Consider using DialogParticipation.Register in XAML to bind in the DataContext.");
                }
            }

            var association = DialogParticipation.GetAssociation(context);

            return(AssociationToWindow(association));
        }