コード例 #1
0
        /// <summary>
        /// Shows a dialog to the user in a tool popup
        /// </summary>
        /// <param name="title">Title of the dialog window</param>
        /// <param name="datacontext">View model to be rendered. To return a value from a button clicked
        /// as a dialog result, a nullable bool property called DialogResult must exist on the view model
        /// and raise the OnPropertyChanged event to fire from the view. Otherwise, the only way to close
        /// the dialog will be the close button in the corner. The view model can also have a Title
        /// property of type string to display a title in the top bar.</param>
        /// <returns>A nullable boolean based on what action the user took if the view model handles it.
        /// (See the datacontext parameter for more info.) If the view model does not handle it. False
        /// will be returned it.</returns>
        public bool?ShowDialog(object datacontext)
        {
            var win = new EmptyWindow();

            win.DataContext = win.Content = datacontext;  // Set the content to show and the datacontext to bind to

            //Set the owner of the dialog box so that it's always in front
            var currentWindow = Application.Current.Windows.OfType <Window>().FirstOrDefault(x => x.IsActive);

            if (currentWindow != null)
            {
                win.Owner = currentWindow;
                win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            }

            return(win.ShowDialog());
        }
コード例 #2
0
        public Window FindWindowByViewModel <T>(bool init = true)
        {
            var    type     = typeof(T);
            string typeName = type.ToString();

            string[] strArray        = typeName.Split('.');
            var      defaultViewName = strArray.Last().Replace("ViewModel", "Window");

            var shortViewName = defaultViewName;

            var shortViewName2 = defaultViewName;

            if (ChangeTracker.Is34Mode)
            {
                shortViewName2 = "Mode3x4." + defaultViewName;
            }
            else if (ChangeTracker.IsLandscapeMode)
            {
                shortViewName2 = "Landscape." + defaultViewName;
            }

            var viewName    = typeName.Replace("ViewModels", "Views").Replace(strArray.Last(), shortViewName2);
            var defaultName = typeName.Replace("ViewModels", "Views").Replace(strArray.Last(), defaultViewName);

            Window window = null;

            if (ConfigurationManager.AppSettings["disable_views"] != null)
            {
                window = new EmptyWindow();
            }
            else
            {
                var newtype = type.Assembly.GetType(viewName);
                if (newtype != null)
                {
                    var control = Activator.CreateInstance(newtype);
                    window = (Window)control;
                }
                else
                {
                    var  path        = Path.Combine(Directory.GetCurrentDirectory(), ViewsNamespace + ".dll");
                    var  assembly    = Assembly.LoadFrom(path);
                    Type defaulttype = null;
                    if (ChangeTracker.Is34Mode)
                    {
                        defaulttype = assembly.GetType(ViewsNamespace + ".Views.Mode3x4." + shortViewName);
                    }
                    if (ChangeTracker.IsLandscapeMode)
                    {
                        defaulttype = assembly.GetType(ViewsNamespace + ".Views.Landscape." + shortViewName);
                    }
                    if (defaulttype == null)
                    {
                        defaulttype = assembly.GetType(ViewsNamespace + ".Views." + shortViewName);
                    }
                    if (defaulttype == null)
                    {
                        var defaultpath = Path.Combine(Directory.GetCurrentDirectory(), DefaultLayout + ".dll");
                        Log.Debug(defaultpath);
                        var defaultassembly = Assembly.LoadFrom(defaultpath);

                        if (ChangeTracker.Is34Mode)
                        {
                            defaulttype = defaultassembly.GetType(DefaultLayout + ".Views.Mode3x4." + shortViewName);
                        }
                        else if (ChangeTracker.IsLandscapeMode)
                        {
                            defaulttype = defaultassembly.GetType(DefaultLayout + ".Views.Landscape." + shortViewName);
                        }
                        if (defaulttype == null)
                        {
                            defaulttype = defaultassembly.GetType(DefaultLayout + ".Views." + shortViewName);
                        }
                    }
                    if (defaulttype == null)
                    {
                        defaulttype = type.Assembly.GetType(defaultName, true);
                    }
                    var control = Activator.CreateInstance(defaulttype);
                    window = (Window)control;
                }
            }

            var vievModel = Activator.CreateInstance(typeof(T));

            window.DataContext = vievModel;
            if (vievModel is IBaseViewModel)
            {
                ((IBaseViewModel)vievModel).ViewWindow = window;
            }

            if (init && ConfigurationManager.AppSettings["disable_views"] == null)
            {
                window.Visibility = Visibility.Collapsed;
                window.Show();
                window.Hide();
                window.Visibility = Visibility.Visible;
            }
            if (Debugger.IsAttached)
            {
                window.Topmost = false;
            }
            return(window);
        }