예제 #1
0
        public static async Task EnsureFocusAsync(Hyperlink element, FocusState focusState, UInt32 Attempts = 1)
        {
            bool gotFocus = false;

            while (Attempts > 0 && !gotFocus)
            {
                Attempts--;

                //TestServicesExtensions.EnsureForegroundWindow();

                using (var eventTester = new EventTester <Hyperlink, RoutedEventArgs>(element, "GotFocus"))
                    using (var eventTesterFocusMgr = EventTester <object, FocusManagerGotFocusEventArgs> .FromStaticEvent <FocusManager>("GotFocus"))
                    {
                        UIExecutor.Execute(() =>
                        {
                            if (element.FocusState == focusState && FocusManager.GetFocusedElement(element.XamlRoot).Equals(element))
                            {
                                // The element is already focused with the expected focus state
                                Log.Comment("Focus was already set on this element");
                                gotFocus = true;
                            }
                            else
                            {
                                Log.Comment("Setting focus to the element...");
                                element.Focus(focusState);
                            }
                        });

                        await Task.Delay(100);

                        //eventTester.WaitForNoThrow(TimeSpan.FromMilliseconds(4000));
                        //eventTesterFocusMgr.WaitForNoThrow(TimeSpan.FromMilliseconds(4000));
                        gotFocus = gotFocus == false ? eventTester.HasFired : true;
                    }

                TestServices.WindowHelper.WaitForIdle();
            }

            //if (!gotFocus)
            //{
            //	TestServices.Utilities.CaptureScreen("FocusTestHelper");
            //}
            Verify.IsTrue(gotFocus);
        }
예제 #2
0
        private void PropertiesChanged()
        {
            this.Inlines.Clear();

            if (ContextMenu != null && Popup != null)
            {
                ContextMenu.Loaded += ContextMenu_Loaded;
                Popup.Loaded       += Popup_Loaded;
            }

            ReadLinksAndText(FormatText, (text, isLink) =>
            {
                text = text.Trim();
                if (!isLink)
                {
                    var run   = new Run(text);
                    run.Style = RunStyle;
                    this.Inlines.Add(run);
                }
                else
                {
                    var resolvedPropertyObject = DataItem.GetType().GetProperty(text).GetValue(DataItem, null);
                    var link         = new Hyperlink(new Run(resolvedPropertyObject.ToString()));
                    link.NavigateUri = new Uri("about:none");
                    link.Style       = HyperlinkStyle;

                    link.RequestNavigate += (s, e) =>
                    {
                        // Take focus now so that we get return focus when the user leaves.
                        link.Focus();
                        var dpiX = Window.GetWindow(this).DpiX();
                        var dpiY = Window.GetWindow(this).DpiY();

                        if (resolvedPropertyObject is IOptionViewModel)
                        {
                            ContextMenu2.Opacity     = 0;
                            ContextMenu2.ItemsSource = GetContextMenuFromOptionViewModel((IOptionViewModel)resolvedPropertyObject).OrderBy(menu => menu.DisplayName);
                            ContextMenu2.UpdateLayout();
                            ContextMenu2.IsOpen = true;
                            ContextMenu2.Dispatcher.BeginInvoke((Action)(() =>
                            {
                                ContextMenu2.Opacity = 1;
                                ContextMenu2.HorizontalOffset = -1 * (ContextMenu2.RenderSize.Width / dpiX) / 2;
                                ContextMenu2.VerticalOffset = -1 * (ContextMenu2.RenderSize.Height / dpiY) / 2;
                                ContextMenu2.Focus();
                            }),
                                                                System.Windows.Threading.DispatcherPriority.DataBind, null);
                        }
                        else
                        {
                            Popup.PreviewKeyDown += (_, ee) =>
                            {
                                if (ee.Key == Key.Escape)
                                {
                                    Popup.IsOpen = false;
                                }
                            };
                            Popup.Opacity     = 0;
                            Popup.DataContext = resolvedPropertyObject;
                            Popup.UpdateLayout();
                            Popup.Child.UpdateLayout();
                            Popup.IsOpen = true;
                            Popup.Dispatcher.BeginInvoke((Action)(() =>
                            {
                                Popup.Opacity = 1;
                                Popup.HorizontalOffset = -1 * (Popup.Child.RenderSize.Width / dpiX) / 2;
                                Popup.VerticalOffset = -1 * (Popup.Child.RenderSize.Height / dpiY) / 2;
                                Keyboard.Focus(Popup.Child.FindVisualChild <Control>());
                            }),
                                                         System.Windows.Threading.DispatcherPriority.DataBind, null);
                        }
                    };
                    this.Inlines.Add(link);
                }
                this.Inlines.Add(new Run(" "));
            });
        }