Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何防止WPF应用程序加载?_C#_.net_Wpf_Startup - Fatal编程技术网

C# 如何防止WPF应用程序加载?

C# 如何防止WPF应用程序加载?,c#,.net,wpf,startup,C#,.net,Wpf,Startup,我希望WPF应用程序只在某些条件下启动。我尝试了以下方法,但没有成功: public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { if (ConditionIsMet) // pseudo-code { base.OnStartup(e); } } } 但即使不满足

我希望WPF应用程序只在某些条件下启动。我尝试了以下方法,但没有成功:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        if (ConditionIsMet) // pseudo-code
        {
            base.OnStartup(e);
        }
    }
}
但即使不满足条件,应用程序也会正常运行

请尝试以下操作:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    if (MyCondition)
    {
        ShowSomeDialog("Hey, I Can't start because...");
        this.Shutdown();
    }
}
另一个解决方案

设计师

<Application x:Class="SingleInstanceWPF.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Startup="Application_Startup">
</Application>

也许我这样做真的很难,但我发现在启动时通过假设任何事情来对抗调用优先级/顺序都是徒劳的。我确实希望在应用程序启动期间引发的任何异常都能立即冒泡到最外层的异常处理程序,但即使在引发异常时,我的MVVM定位器对象也会自动实例化自己,因为它被定义为应用程序级资源

这意味着鸡在蛋之前到达,但在同一个蛋已经破裂之后

因此,解决办法是:

1) 从App.xaml中删除MVVM定位器

2) 创建应用程序启动事件

在顶部添加以下行:

3) 将启动绑定到App.xaml中的应用程序启动事件 e、 g

5) 然后,在找到资源后,立即打开主窗口,但前提是定位器已成功实例化

            Uri uri = new Uri("pack:Views/MainWindow.xaml", UriKind.RelativeOrAbsolute);
            Application.Current.StartupUri = uri;
如果定位器上的构造函数失败,步骤(4)将立即抛出异常,遗憾的是,这种情况一直发生在我身上

然后,步骤4中的异常处理如下(本例使用RadMessageBox,但请随意修复:


应用程序仍在尝试打开StartupUri。请尝试此操作-
    #region Handlers For Unhandled Exceptions
        // anything else to do on startup can go here and will fire after the base startup event of the application
        // First make sure anything after this is handled
        // Creates an instance of the class holding delegate methods that will handle unhandled exceptions.
        CustomExceptionHandler eh = new CustomExceptionHandler();

        AppDomain.CurrentDomain.UnhandledException +=
             new UnhandledExceptionEventHandler(eh.OnAppDomainException);
        // this ensures that any unhandled exceptions bubble up to a messagebox at least
        Dispatcher.CurrentDispatcher.UnhandledException += new DispatcherUnhandledExceptionEventHandler(eh.OnDispatcherUnhandledException);

        #endregion  Handlers For Unhandled Exceptions
 Startup="Application_Startup"   <<<<  this name is arbitrary but conventional AFAICT
            Resources.Add("Locator", new ViewModelLocator());
             //You can use FindResource and an exception will be thrown straightaway as I recall
            if (!(TryFindResource("Locator") == null))  

                throw new ResourceReferenceKeyNotFoundException("ViewModelLocator could not be created", "Locator");
            Uri uri = new Uri("pack:Views/MainWindow.xaml", UriKind.RelativeOrAbsolute);
            Application.Current.StartupUri = uri;
public void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        try
        {

                var result = this.ShowExceptionDialog(e.Exception);

        }
        catch
        {


            RadMessageBox.Show("Fatal Dispatcher Error - the application will now halt.", Properties.Resources.CaptionSysErrMsgDlg,
               MessageBoxButton.OK, MessageBoxImage.Stop, true);
        }

        finally
        {

            e.Handled = true;

            // TERMINATE WITH AN ERROR CODE -1!
            //Environment.Exit(-1);
        }
    }