- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试在Win7 / XP上运行WPF应用程序时,我遇到了一个非常奇怪的问题。 WPF应用程序以.NET 4.0为目标,它引用了Caliburn.Micro 1.5.2和Autofac 3.1.0。
我将从问题的总结开始,然后再提供到目前为止的详细信息。
总览
在我的开发工作站中,我有Windows 8和Visual Studio2012。我正在使用Caliburn和Autofac,如this post中所述(基本上是this的简化版本)。
当我在开发机中构建并运行应用程序时,一切都会按预期进行。但是,当我使用二进制文件并在Windows 7 / XP计算机上执行它们时,出现以下错误,并且堆栈跟踪很长:
System.InvalidOperationException: IoC is not initialized
ShellViewModel
只是一个空的
Conductor<Screen>
。
ShellView
只有一个
TextBlock
。
App.xaml
遵循Caliburn的建议:
<Application x:Class="WpfApplication2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<local:Bootstrapper x:Key="bootstrapper" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
App.xaml.cs
代码已修改为捕获并显示异常:
public partial class App : Application
{
public App ()
{
// hook on error before app really starts
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
try
{
this.InitializeComponent();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
throw;
}
}
public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show(((Exception)e.ExceptionObject).ToString());
}
}
Bootstrapper
。可以这么说,我有一个与Caliburn和Autofac一起运行的应用程序,它与
Bootstrapper
相似,与所描述的
here
类似。对于示例,我创建了一个简化版本:
public class Bootstrapper : Bootstrapper<ShellViewModel>
{
private IContainer container;
protected IContainer Container
{
get { return this.container; }
}
protected override object GetInstance(Type serviceType, string key)
{
if (string.IsNullOrWhiteSpace(key))
{
if (container.IsRegistered(serviceType))
return container.Resolve(serviceType);
}
else
{
if (container.IsRegisteredWithName(key, serviceType))
container.ResolveNamed(key, serviceType);
}
throw new Exception(string.Format("Could not locate any instances of contract {0}.", key ?? serviceType.Name));
}
protected override IEnumerable<object> GetAllInstances(Type serviceType)
{
return this.Container.Resolve(typeof(IEnumerable<>).MakeGenericType(serviceType)) as IEnumerable<object>;
}
protected override void BuildUp(object instance)
{
this.Container.InjectProperties(instance);
}
protected override void Configure()
{
var builder = new ContainerBuilder();
builder.RegisterType<ShellViewModel>();
builder.RegisterAssemblyTypes(this.GetType().Assembly);
builder.RegisterType<WindowManager>()
.As<IWindowManager>()
.SingleInstance();
builder.RegisterType<EventAggregator>()
.As<IEventAggregator>()
.SingleInstance();
this.container = builder.Build();
}
}
Bootstrapper
的
GetInstance
方法中添加了一些断点,它们已被正确命中。
System.InvalidOperationException: IoC is not initialized.
at Caliburn.Micro.IoC.<.cctor>b__0(Type service, String key) in c:\Users\Rob\Documents\CodePlex\caliburnmicro\src\Caliburn.Micro.Silverlight\IoC.cs:line 13
at Caliburn.Micro.IoC.Get[T](String key) in c:\Users\Rob\Documents\CodePlex\caliburnmicro\src\Caliburn.Micro.Silverlight\IoC.cs:line 32
at Caliburn.Micro.BootstrapperBase.DisplayRootViewFor(Type viewModelType, IDictionary`2 settings) in c:\Users\Rob\Documents\CodePlex\caliburnmicro\src\Caliburn.Micro.Silverlight\Bootstrapper.cs:line 254
at Caliburn.Micro.BootstrapperBase.DisplayRootViewFor[TViewModel](IDictionary`2 settings) in c:\Users\Rob\Documents\CodePlex\caliburnmicro\src\Caliburn.Micro.Silverlight\Bootstrapper.cs:line 264
at Caliburn.Micro.Bootstrapper`1.OnStartup(Object sender, StartupEventArgs e) in c:\Users\Rob\Documents\CodePlex\caliburnmicro\src\Caliburn.Micro.Silverlight\Bootstrapper.cs:line 288
at System.Windows.Application.OnStartup(StartupEventArgs e)
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.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
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.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.MessageBox(HandleRef hWnd, String text, String caption, Int32 type)
at System.Windows.MessageBox.ShowCore(IntPtr owner, String messageBoxText, String caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options)
at System.Windows.MessageBox.Show(String messageBoxText)
at WpfApplication2.App..ctor() in c:\Users\Public\Projetos\Outros\WpfApplication3\WpfApplication2\App.xaml.cs:line 27
at WpfApplication2.App.Main() in c:\Users\Public\Projetos\Outros\WpfApplication3\WpfApplication2\obj\Debug\App.g.cs:line 0
IoC
类时的预期行为,正如我们在
Caliburn's source中看到的那样。但是,
IoC
在
Bootstrapper
调用
Configure
之后立即正确初始化。请参见
the source中的
BootstrapperBase.StartRuntime
方法。
Bootstrapper
中删除了所有内容,经过一些尝试,以下是触发问题的全部步骤:
public class Bootstrapper : Bootstrapper<ShellViewModel>
{
protected override void Configure()
{
var builder = new ContainerBuilder();
builder.RegisterType<ShellViewModel>();
}
}
builder.RegisterType<ShellViewModel>();
行中注释,则该应用程序有效。
public class Bootstrapper : Bootstrapper<ShellViewModel>
{
protected override void Configure()
{
MessageBox.Show("Configure");
}
}
MessageBox.Show
启动之前调用
Bootstrapper
。
MessageBox.Show
更改为NLog的日志记录例程,以将错误写入文件,从而可以跟踪真正的异常。真正的问题来自Autofac瞄准PCL,并且要使其能够在.NET 4.0 Client Profile中正常运行,我需要
install the update 4.0.3 in target machine。
最佳答案
好吧,我在XP机器上运行了您的代码,并在上次更新时遇到了问题,该问题是您使用MessageBox
类的,它在Windows 8机器上导致了您的问题,但是上一个代码是您在其中创建新容器的,注册您在Win 7 / xp机器上引起问题的ShellViewModel
并没有引起我任何问题(它编译并运行正常)。
现在我想知道为什么在MessageBox.Show()
方法中使用Configure
方法会导致该异常,而我却想出了原因,并且可以归结为:
Caliburn.Micro的(在此后为CM)Bootstrapper<TRootModel>
构造函数正在调用Start()
Start()
正在呼叫StartRuntime()
StartRuntime()
正在按此特定顺序进行呼叫:Configure()
然后IoC.Get = GetInstance
然后IoC.GetAllInstances = GetAllInstances;
然后IoC.BuildUp = BuildUp;
当StartRuntime()
调用Configure()
时,将执行您的代码,特别是MessageBox.Show()
,这是一项系统功能,要求(必须)每个消息框都具有一个所有者窗口,并且默认情况下,所有者是您当前的活动应用程序窗口。
现在在此阶段运行了一段系统代码,我不知道正在运行什么,但是由系统执行的那段代码调用了OnStartup()
方法覆盖,CM在引导程序中覆盖了该覆盖并使用它来显示您选择的TRootModel
的视图。
为了使Caliburn显示该TRootModel
的视图,它需要一个IWindowManager
实例,并使其使用(您已弄清楚)我们所钟爱的IoC
(如您在第3步中看到的那样),它没有尚未初始化,它仍然停留在该Configure()
方法上,并且没有继续进行。
摘要:具有容器配置的代码在我的Win XP计算机上可以正常工作,但是在MessageBox.Show()
覆盖中使用Configure()
方法的代码没有问题,我为您提供了详细的原因说明。
关于c# - 在Win7/XP上未初始化IoC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17622546/
我是 Spring 新手,这就是我想要做的事情: 我正在使用一个基于 Maven 的库,它有自己的 Spring 上下文和 Autowiring 字段。 它的bean配置文件是src/test/res
我在我的测试脚本中有以下列表初始化: newSequenceCore=["ls", "ns", "*", "cm", "*", "ov", "ov", "ov", "ov", "kd"] (代表要在控
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Class construction with initial values 当我查看 http://en.
我得到了成员变量“objectCount”的限定错误。编译器还返回“ISO C++ 禁止非常量静态成员的类内初始化”。这是主类: #include #include "Tree.h" using n
我有如下所示的a.h class A { public: void doSomething()=0; }; 然后我有如下所示的b.h #include "a.h" class b: publi
我需要解析 Firebase DataSnapshot (一个 JSON 对象)转换成一个数据类,其属性包括 enum 和 list。所以我更喜欢通过传递 DataSnapshot 来手动解析它进入二
我使用 JQuery 一段时间了,我总是使用以下代码来初始化我的 javascript: $(document).ready( function() { // Initalisation logic
这里是 Objective-C 菜鸟。 为什么会这样: NSString *myString = [NSString alloc]; [myString initWithFormat:@"%f", s
我无法让核心数据支持的 NSArrayController 在我的代码中正常工作。下面是我的代码: pageArrayController = [[NSArrayController alloc] i
我对这一切都很陌生,并且无法将其安装到我的后端代码中。它去哪里?在我的页脚下面有我所有的 JS? 比如,这是什么意思: Popup initialization code should be exec
这可能是一个简单的问题,但是嘿,我是初学者。 所以我创建了一个程序来计算一些东西,它目前正在控制台中运行。我决定向其中添加一个用户界面,因此我使用 NetBeans IDE 中的内置功能创建了一个 J
我有 2 个 Controller ,TEST1Controller 和 TEST2Controller 在TEST2Controller中,我有一个initialize()函数设置属性值。 如果我尝
据我所知, dependentObservable 在声明时会进行计算。但如果某些值尚不存在怎么办? 例如: var viewModel ={}; var dependentObservable1 =
我正在阅读 POODR 这本书,它使用旧语法进行默认值初始化。我想用新语法实现相同的功能。 class Gear attr_reader :chainring, :cog, :wheel de
我按照 polymer 教程的说明进行操作: https://www.polymer-project.org/3.0/start/install-3-0 (我跳过了可选部分) 但是,在我执行命令“po
很抱歉问到一个非常新手的Kotlin问题,但是我正在努力理解与构造函数和初始化有关的一些东西。 我有这个类和构造函数: class TestCaseBuilder constructor(
假设我们有一个包含 30 列和 30 行的网格。 生命游戏规则简而言之: 一个小区有八个相邻小区 当一个细胞拥有三个存活的相邻细胞时,该细胞就会存活 如果一个细胞恰好有两个或三个活的相邻细胞,那么它就
我是 MQTT 和 Android 开放附件“AOA” 的新手。在阅读教程时,我意识到,在尝试写入 ByteArrayOutputStream 类型的变量之前,应该写入 0 或 0x00首先到该变量。
我有 2 个 Controller ,TEST1Controller 和 TEST2Controller 在TEST2Controller中,我有一个initialize()函数设置属性值。 如果我尝
我有一个inotify /内核问题。我正在使用“inotify” Python项目进行观察,但是,我的问题仍然是固有的关于inotify内核实现的核心。 Python inotify项目处理递归ino
我是一名优秀的程序员,十分优秀!