public static void ShowMvvm(BindableBaseViewModel vMdl, Window view)
 {
     view.DataContext = vMdl;
     BindableBaseViewModel.CloseEvent(view, vMdl);
     Task.Run(() => vMdl.AutoExec()); //			await vMdl.AutoExec();
     view.Show();
 }
        public static void CloseEvent(Window view, BindableBaseViewModel vwMdl)
        {
            async void handler(object sender, EventArgs e)
            {
                await vwMdl.ClosingVM();

                if (_cancelClosing)
                {
                    return;
                }

                vwMdl.RequestClose -= handler;
                try
                {
                    if (view != null)
                    {
                        if (Application.Current.Dispatcher.CheckAccess()) // if on UI thread
                        {
                            view.Close();
                        }
                        else
                        {
                            await Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => view.Close()));
                        }
                    }
                }
                catch (Exception ex) { Trace.WriteLine(ex.Message, System.Reflection.MethodInfo.GetCurrentMethod().Name); if (Debugger.IsAttached)
                                       {
                                           Debugger.Break();
                                       }
                }
            } // When the ViewModel asks to be closed, close the window.

            vwMdl.RequestClose += handler;

            ////org: replaced by code cleanup on Jan 2019.
            //EventHandler handler = null; // When the ViewModel asks to be closed, close the window.
            //handler = async delegate
            //{
            //  await vwMdl.ClosingVM();
            //  if (_cancelClosing) return;

            //  vwMdl.RequestClose -= handler;
            //  try
            //  {
            //    if (view != null)
            //    {
            //      if (Application.Current.Dispatcher.CheckAccess()) // if on UI thread
            //        view.Close();
            //      else
            //        await Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => view.Close()));
            //    }
            //  }
            //  catch (Exception ex) { Trace.WriteLine(ex.Message, System.Reflection.MethodInfo.GetCurrentMethod().Name); if (Debugger.IsAttached) Debugger.Break(); }
            //};
            //vwMdl.RequestClose += handler;
        }
        public static bool?ShowModalMvvmAsync(BindableBaseViewModel vMdl, Window view, Window owner = null) // public static async Task<bool?> ShowModalMvvmAsync(BindableBaseViewModel vMdl, Window view, Window owner = null)
        {
            if (owner != null)
            {
                view.Owner = owner;
                view.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            }

            view.DataContext = vMdl;
            CloseEvent(view, vMdl);

#if brave
            Task.Run(async() => await autoexecAsyncSafe(vMdl)); //todo:
#else
            await vMdl.AutoExecAsync();                         //todo:
#endif

            return(view.ShowDialog());
        }
        public static bool?ShowModalMvvm(BindableBaseViewModel vMdl, Window view, Window owner = null)
        {
            if (owner != null)
            {
                view.Owner = owner;
                view.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            }

            view.DataContext = vMdl;
            BindableBaseViewModel.CloseEvent(view, vMdl);

#if brave
            autoexecSafe(vMdl);
#else
            autoexecSafe(vMdl);
#endif

            return(view.ShowDialog());
        }
 static async Task autoexecAsyncSafe(BindableBaseViewModel vMdl)
 {
     try
     {
         if (Application.Current.Dispatcher.CheckAccess()) // if on UI thread
         {
             await vMdl.AutoExecAsync();
         }
         else
         {
             await Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(async() => //todo: rejoin properly to the UI thread (Oct 2017)
                                                                                                    await vMdl.AutoExecAsync()));
         }
     }
     catch (Exception ex) { Trace.WriteLine(ex.Message, System.Reflection.MethodInfo.GetCurrentMethod().Name); if (Debugger.IsAttached)
                            {
                                Debugger.Break();
                            }
     }
 }