我在这里和 MSDN 论坛上都发现了这个问题和可能的解决方案,但我的问题有点不同。当我在 Runtime 类上调用 ctor 时,我得到了这个异常:
System.IO.FileNotFoundException:无法加载文件或程序集“INuiInstanceHelper,Version=1.0.0.10,Culture=neutral,PublicKeyToken=31bf3856ad364e35”或其依赖项之一。
在简单场景中构建运行时时我没有收到此错误(例如 WPF 中的 Loaded 事件。请参阅下面的简单场景代码)。
在我的真实场景中,当用户从打开文件对话框打开文件时,会调用我的运行时 ctor。这是从 ViewModel 中的 MenuItem 命令开始的,该命令使用包装打开文件对话框的“服务”。ViewModel 然后在指定文件路径后初始化运行时:
void ExecuteOpenCommand()
{
var path = this.openFileService.OpenFile();
if (!string.IsNullOrEmpty(path))
{
var model = ViewModelLoader.ReadMainModel(path);
var viewModel = ViewModelLoader.LoadViewModel(model);
this.MainViewModel = viewModel;
this.MainViewModel.NuiService = this.nuiService;
this.nuiService.Initialize();
}
}
如果 nuiService 之前从未被初始化过,它只会构造运行时:
bool initialized;
public void Initialize()
{
if (initialized)
{
return;
}
try
{
this.runtime = new Runtime();
}
catch (FileNotFoundException ex)
{
Trace.WriteLine(ex.ToString());
}
// ...
initialized = true;
}
但是,当我从头开始一个新的WPF 项目并构建运行时时,我没有收到此错误:
// does not throw exception
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Runtime r = new Runtime();
r.Initialize(RuntimeOptions.UseSkeletalTracking);
r.Uninitialize();
}
在谷歌搜索此异常时,我遇到了需要更新 DirectX、重新启动、更新 Windows 或将 INuiInstanceHelper 重新安装到 GAC 的解决方案。我看不出这些解决方案有什么帮助,因为我可以以不同的方式无错误地构建运行时。我认为我的问题必须与用户启动(例如从按钮单击)有关,但是当我将运行时构造函数移动到在应用程序启动附近执行的方法时(例如在我的 ViewModel 的 ctor 中实例化通过数据绑定声明)我仍然有这个问题。想法?