Пример #1
0
        public static void Focus(UIElement uiElement, Dispatcher dispatcher, DispatcherPriority dispatcherPriority)
        {
            if (dispatcher == null || uiElement == null)
            {
                return;
            }

            try
            {
                dispatcher.BeginInvoke(new Action(() =>
                {
                    var keyboardHack = KeyboardHelper.Instance;
                    var oldValue     = keyboardHack.AlwaysShowFocusVisualInternal;

                    keyboardHack.AlwaysShowFocusVisualInternal = true;

                    try
                    {
                        Keyboard.Focus(uiElement);
                        keyboardHack.ShowFocusVisualInternal();
                    }
                    finally
                    {
                        keyboardHack.AlwaysShowFocusVisualInternal = oldValue;
                    }
                }), dispatcherPriority);
            }
            catch (Exception Ex)
            {
                LogMVVM.Exception("MVVM Exception: Keyboard focus dispatcher error.", Ex);
            }
        }
Пример #2
0
 // When this class is first accessed, we need to know what access level it has.
 public static bool CheckApplicationUIThreadAccess()
 {
     try
     {
         return(Application.Current.Dispatcher.CheckAccess());
     }
     catch (Exception Ex)
     {
         LogMVVM.Exception("MVVM Exception: Error checking application dispatcher access.", Ex);
         return(false);
     }
 }
Пример #3
0
 private BitmapSource getIcon(WindowMessageIcon icontype)
 {
     try
     {
         Icon icon = (Icon)typeof(SystemIcons).GetProperty(Convert.ToString(icontype), BindingFlags.Public | BindingFlags.Static).GetValue(null, null);
         return(Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()));
     }
     catch (Exception Ex)
     {
         LogMVVM.Exception("MVVM Exception: MessageBox Enhanced error when reading icon type to image.", Ex);
         return(null);
     }
 }
Пример #4
0
        public static ShutdownMode CurrentApplicationShutdownMode()
        {
            try
            {
                if (CheckApplicationUIThreadAccess())
                {
                    return(Application.Current.ShutdownMode);
                }

                return(ShutdownMode.OnLastWindowClose);
            }
            catch (Exception Ex)
            {
                LogMVVM.Exception("MVVM Exception: Error reading current application shutdown mode.", Ex);
                return(ShutdownMode.OnLastWindowClose);
            }
        }
Пример #5
0
        // Ugly, but necessary.
        public static void CloseDialog(Window window)
        {
            if (window == null)
            {
                return;
            }

            try
            {
                window.Dispatcher.Invoke((Action) delegate
                {
                    // HACK: There might be occations and I do not know how, but the window no longer is considered modal or what not.
                    bool isThisModal = false;
                    try
                    {
                        isThisModal = (bool)typeof(Window).GetField("_showingAsDialog", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(window);
                    }
                    catch (Exception Ex)
                    {
                        LogMVVM.Exception("MVVM Exception: Close dialog window error when checking if window is modal.", Ex);
                        isThisModal = false; // Just to be safe. I really do not need this here, but meh.
                    }

                    if (isThisModal == false)
                    {
                        window.Close();
                        return;
                    }

                    try
                    {
                        window.DialogResult = true;
                    }
                    catch (Exception Ex)
                    {
                        LogMVVM.Exception("MVVM Exception: Close dialog window error when setting dialog result.", Ex);
                        window.Close();
                    }
                });
            }
            catch (Exception Ex)
            {
                LogMVVM.Exception("MVVM Exception: Close dialog window error when using dispatcher.", Ex);
                window.Close();
            }
        }
Пример #6
0
        private void contentRendered(object sender, EventArgs e)
        {
            pDialogViewModel.ContentRendered(sender, e);

            if (pDialogViewModel.Data.RenderWait)
            {
                try
                {
                    Dispatcher dispatcher = this.Dispatcher;
                    if (dispatcher.CheckAccess())
                    {
                        dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() => { })).Wait();
                    }
                }
                catch (Exception Ex)
                {
                    LogMVVM.Exception("MVVM Exception: Dialog Window cotent render error. Possible dispatcher error.", Ex);
                }
            }
        }
Пример #7
0
        // If your Hex string is not well formed, this will return black.
        public static Brush HexConverter(string hexCode)
        {
            Brush brush = Brushes.Black;

            if (string.IsNullOrEmpty(hexCode) || ValidateHexCode(hexCode) == false)
            {
                return(brush);
            }

            var converter = new BrushConverter();

            try
            {
                brush = (Brush)converter.ConvertFromString(hexCode);
            }
            catch (Exception Ex)
            {
                LogMVVM.Exception("MVVM Exception: HexConverter failed toparse color from hex code: " + hexCode + ".", Ex);
                brush = Brushes.Black;
            }

            return(brush);
        }
Пример #8
0
        public WindowsMessage(WindowsMessageViewModel windowsMessageViewModel)
        {
            try
            {
                this.DataContext = windowsMessageViewModel;
                InitializeComponent();

                switch (windowsMessageViewModel.Data.MessageButtons)
                {
                case WindowMessageButtons.AcceptDecline:
                case WindowMessageButtons.ContinueCancel:
                case WindowMessageButtons.OkCancel:
                case WindowMessageButtons.YesNo:
                    if (windowsMessageViewModel.Data.MessageButtonFocus == WindowButtonFocus.Center)
                    {
                        windowsMessageViewModel.Data.MessageButtonFocus = WindowButtonFocus.Left;
                    }
                    break;

                case WindowMessageButtons.Default:
                case WindowMessageButtons.Exit:
                case WindowMessageButtons.Ok:
                    if (windowsMessageViewModel.Data.MessageButtonFocus == WindowButtonFocus.Left ||
                        windowsMessageViewModel.Data.MessageButtonFocus == WindowButtonFocus.Right)
                    {
                        windowsMessageViewModel.Data.MessageButtonFocus = WindowButtonFocus.Center;
                    }
                    break;

                case WindowMessageButtons.YesNoCancel:
                    // TODO(DB): Determine if there needs to be logic here.
                    break;

                case WindowMessageButtons.Misc:
                    // TODO(DB): Determine if there needs to be logic here.
                    break;

                default:
                    // TODO(DB): Determine if there needs to be logic here.
                    break;
                }

                switch (windowsMessageViewModel.Data.MessageButtonFocus)
                {
                case WindowButtonFocus.Left:
                    windowsMessageViewModel.Data.FocusUIElement = leftButton;
                    break;

                case WindowButtonFocus.Center:
                    windowsMessageViewModel.Data.FocusUIElement = centerButton;
                    break;

                case WindowButtonFocus.Right:
                    windowsMessageViewModel.Data.FocusUIElement = rightButton;
                    break;
                }
            }
            catch (Exception Ex)
            {
                LogMVVM.Exception("MVVM Exception: MessageBox Enhanced user control construct error.", Ex);
            }
        }
Пример #9
0
        public DialogBaseWindow(UserControl userControl, DialogViewModel dialogViewModel)
        {
            try
            {
                pDialogViewModel = dialogViewModel;
                this.DataContext = dialogViewModel;

                InitializeComponent();

                try
                {
                    // These are some conditions that are needed for everything to work correctly.
                    if (userControl == null || userControl.DataContext != dialogViewModel)
                    {
                        // NOTE: This will cause the following suppressed error that only shows in debugger output:
                        // 'System.InvalidOperationException' in PresentationFramework.dll
                        // This happens since the window is already closed before a Show() or ShowDialog() method can actually do anything.
                        this.Close();
                        return;
                    }
                }
                catch (Exception Ex)
                {
                    LogMVVM.Exception("MVVM Exception: Dialog Window user control and data context check error.", Ex);
                }

                try
                {
                    if (dialogViewModel.Data.WindowIconURI.Length > 0)
                    {
                        Icon = BitmapFrame.Create(new Uri(dialogViewModel.Data.WindowIconURI, UriKind.RelativeOrAbsolute));
                    }
                }
                catch (Exception Ex)
                {
                    LogMVVM.Exception("MVVM Exception: Dialog Window title bar icon creation error.", Ex);
                }

                // General Window properties. Not bound since some are not technically content and one is not able to be bound.
                WindowStartupLocation = dialogViewModel.Data.DialogStartupLocation; // Cannot be bound since it is a DependencyProperty
                WindowStyle           = dialogViewModel.Data.DialogWindowStyle;
                Topmost    = dialogViewModel.Data.Topmost;
                Title      = dialogViewModel.Data.WindowTitle;
                Background = dialogViewModel.Data.Background;

                // Adds the user control to the window.
                this.dialogBaseWindowGrid.Children.Add(userControl);

                // If there is a specified control to focus, this will process that.
                FocusElementControl(dialogViewModel.Data.FocusUIElement);

                Loaded          += contentLoaded;
                ContentRendered += contentRendered;
                Closing         += windowClosing;
                dialogViewModel.Events.CloseDialogHandler += closeDialog;
                dialogViewModel.Events.SendMessageHandler += openWindowMessage;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(this, "Window load error: " + Environment.NewLine + Convert.ToString(Ex), "Error...", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Пример #10
0
        // Primary method for opening a dialog.
        private static WindowMessageResult openDialogWork(Window dialogWindow, DialogViewModel dialogWindowViewModel, Window parentWindow, ShutdownMode shutdownMode)
        {
            bool applicationUIThreadAccess = CheckApplicationUIThreadAccess();

            if (applicationUIThreadAccess && shutdownMode != CurrentApplicationShutdownMode())
            {
                // Param shutdownMode can used to prevent the Application.Current from going null. It can be turned off by setting ApplicationExplicitShutdown.
                // This should not throw. Possible investigation should be made to check on this.
                Application.Current.ShutdownMode = shutdownMode;
            }

            // We need to find a dispatcher to display this message.
            Dispatcher dispatcher = null;

            if (parentWindow == null && applicationUIThreadAccess == false)
            {
                return(WindowMessageResult.Undefined);
            }
            else if (parentWindow == null && applicationUIThreadAccess)
            {
                try
                {
                    dialogWindow.Owner = Application.Current.Windows.OfType <Window>().FirstOrDefault(x => x.IsActive);
                }
                catch (Exception Ex)
                {
                    LogMVVM.Exception("MVVM Exception: Dialog Service error when trying to get window owner.", Ex);
                    // This should not throw. Possible investigation should be made to check on this.
                    if (Application.Current.Windows.Count > 0)
                    {
                        dialogWindow.Owner = Application.Current.Windows[0];
                    }
                    else
                    {
                        dialogWindow.Owner = null;
                    }
                }


                if (dialogWindow.Owner == null)
                {
                    dispatcher = Application.Current.Dispatcher;
                }
                else
                {
                    dispatcher = dialogWindow.Owner.Dispatcher;
                }
            }
            else // This should ONLY happen if parentWindow is NOT null.
            {
                dialogWindow.Owner = parentWindow;
                dispatcher         = parentWindow.Dispatcher;
            }

            try
            {
                if (dispatcher.CheckAccess() == false)
                {
                    return(WindowMessageResult.Undefined);
                }

                WindowMessageResult result = WindowMessageResult.Undefined;
                dispatcher.Invoke((Action) delegate
                {
                    dialogWindow.ShowDialog();

                    result = dialogWindowViewModel.Result;
                });

                return(result);
            }
            catch (Exception Ex)
            {
                LogMVVM.Exception("MVVM Exception: Dialog service error when opening dialog window.", Ex);
                return(WindowMessageResult.Undefined);
            }
        }