是什么阻止我的 wpf 应用程序找到它已经使用的资源
What is preventing my wpf application from finding a resource that it has already used
我有一个多项目应用程序。在启动项目中(目前)只有对 Application_Startup 的调用。 xaml 是这样定义的;
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Application_Startup">
<Application.Resources>
</Application.Resources>
</Application>
并且 application_startup 事件本身是这样定义的;
Private Sub Application_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
Dim controller As New LoginController(New LoginService)
_CanLaunchMainApplication = controller.StartLoginDialog
If _CanLaunchMainApplication Then
Else
Application.Current.Shutdown
End If
End Sub
当我启动应用程序时,会显示一个登录对话框,目前 return 为 false。我在到达的 if 语句的 Application.Current.Shutdown 行设置了一个断点。在按下继续时,我希望应用程序关闭,而是抛出一个 IOException 类型的未处理异常,说明它无法找到资源 'appplication_startup',这让我觉得有点奇怪,因为这正是我们恰好所在的地方在代码的执行中。
谁能解释为什么会这样?
编辑
这是相关的堆栈跟踪。
at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
at System.IO.Packaging.PackagePart.GetStream()
at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties)
at System.Windows.Application.DoStartup()
at System.Windows.Application.<.ctor>b__1(Object unused)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
您似乎将事件处理程序的名称放在 StartupUri property instead of the Startup 属性 上。
Startup 需要应用程序对象本身的事件处理程序的名称,而 StartupUri 需要资源的 Uri(例如 Window) 将在应用程序启动完成后显示。
将事件处理程序的名称放入Startup
。如果您在 Startup 事件处理程序中创建 main window,则根本不需要使用 StartupUri,如 Startup [=] 文档页面上的示例所示26=]
我有一个多项目应用程序。在启动项目中(目前)只有对 Application_Startup 的调用。 xaml 是这样定义的;
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Application_Startup">
<Application.Resources>
</Application.Resources>
</Application>
并且 application_startup 事件本身是这样定义的;
Private Sub Application_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
Dim controller As New LoginController(New LoginService)
_CanLaunchMainApplication = controller.StartLoginDialog
If _CanLaunchMainApplication Then
Else
Application.Current.Shutdown
End If
End Sub
当我启动应用程序时,会显示一个登录对话框,目前 return 为 false。我在到达的 if 语句的 Application.Current.Shutdown 行设置了一个断点。在按下继续时,我希望应用程序关闭,而是抛出一个 IOException 类型的未处理异常,说明它无法找到资源 'appplication_startup',这让我觉得有点奇怪,因为这正是我们恰好所在的地方在代码的执行中。
谁能解释为什么会这样?
编辑
这是相关的堆栈跟踪。
at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
at System.IO.Packaging.PackagePart.GetStream()
at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties)
at System.Windows.Application.DoStartup()
at System.Windows.Application.<.ctor>b__1(Object unused)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
您似乎将事件处理程序的名称放在 StartupUri property instead of the Startup 属性 上。
Startup 需要应用程序对象本身的事件处理程序的名称,而 StartupUri 需要资源的 Uri(例如 Window) 将在应用程序启动完成后显示。
将事件处理程序的名称放入Startup
。如果您在 Startup 事件处理程序中创建 main window,则根本不需要使用 StartupUri,如 Startup [=] 文档页面上的示例所示26=]