c# - Prism 程序集引用失败 : System. Windows.Interactivity

标签 c# .net wpf visual-studio prism

我的 C#/WPF Prism (v4.0) 应用在加载/解析 Prism 库附带的 System.Windows.Interactivity dll 时一直存在问题。我已经连续工作了三天,试图调试/解决这个问题。我已经学到了很多关于 .Net 程序集解析的知识,但到目前为止我的问题还没有成功,所以我想我应该求助于 StackOverflow 社区以寻求帮助。 :)

我有一个模块作为更大的 Prism 应用程序的一部分运行,它需要引用 System.Windows.Interactivity 以添加行为。因此我有一个指定命名空间的 XAML 用户控件,如下所示:

<UserControl x:Class="MyApp.Modules.SourcesModule.myView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">

然后我尝试在该 UserControl 的子元素上设置行为,如下所示:

<ListBox Name="myListBox">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding SomeCommandOrOther}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ListBox>

奇怪的是,该项目构建良好,当输入关联的代码隐藏文件时,我什至可以为 System.Windows.Interactivity 命名空间中的对象获取 Intellisense 自动完成

但是,仅在运行时,我在上面的 ListBox 元素上得到了 XamlParseException。

Could not load file or assembly 'System.Windows.Interactivity, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. 

InnerException 是 System.IO.FileNotFoundException 类型

"Could not load file or assembly 'System.Windows.Interactivity, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.":"System.Windows.Interactivity, PublicKeyToken=31bf3856ad364e35"

...正如我从阅读程序集解决方案中了解到的那样,这通常表明解决强命名程序集时出现问题,而不仅仅是无法在磁盘上找到 dll(如异常类型所暗示的那样)。

Fusion 日志信息如下,包括对有问题的程序集进行部分绑定(bind)的警告:

=== Pre-bind state information ===
LOG: User = aricebo-array\me
LOG: DisplayName = System.Windows.Interactivity, PublicKeyToken=31bf3856ad364e35
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: System.Windows.Interactivity, PublicKeyToken=31bf3856ad364e35 | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/me/Documents/Development Projects/Desktop apps/Prism/MyApp/Src/MyApp/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/me/Documents/Development Projects/Desktop apps/Prism/MyApp/Src/MyApp/bin/Debug/System.Windows.Interactivity.DLL.
LOG: Attempting download of new URL file:///C:/Users/me/Documents/Development Projects/Desktop apps/Prism/MyApp/Src/MyApp/bin/Debug/System.Windows.Interactivity/System.Windows.Interactivity.DLL.
LOG: Attempting download of new URL file:///C:/Users/me/Documents/Development Projects/Desktop apps/Prism/MyApp/Src/MyApp/bin/Debug/System.Windows.Interactivity.EXE.
LOG: Attempting download of new URL file:///C:/Users/me/Documents/Development Projects/Desktop apps/Prism/MyApp/Src/MyApp/bin/Debug/System.Windows.Interactivity/System.Windows.Interactivity.EXE.

有趣的是,如果我使用 IL 反汇编器 (ildasm.exe) 查看我构建的项目,System.Windows.Interactivity 并未作为引用的程序集之一列在 list 中,尽管其他引用的 Prism dll 显示在那里美好的。例如:

.assembly extern Microsoft.Practices.Prism
{
  .publickeytoken = (31 BF 38 56 AD 36 4E 35 )                         // 1.8V.6N5
  .ver 4:0:0:0
}
.assembly extern Microsoft.Practices.Unity
{
  .publickeytoken = (31 BF 38 56 AD 36 4E 35 )                         // 1.8V.6N5
  .ver 2:0:414:0
}

此问题类似于另一个 StackOverflow 问题中提到的问题:Referencing the correct System.Windows.Interactivity dll from Prism application .但是,我按照那里提到的规定解决方案(即使用 System.Windows.Interactivity 的 Prism 版本)无济于事。只是为了好玩,我也尝试过使用 Expression Blend 3 和 4 SDK 附带的 System.Windows.Interactivity dll(当然是分开的),但也没有成功。

我加载 System.Windows.Interactivity dll 的方式与加载 Prism 库附带的所有其他 dll 的方式没有任何不同(它们都位于我的/lib 文件夹中解决方案,我使用 Visual Studio 2010 中的“添加引用”>“浏览”菜单添加了它们,并指向磁盘上的那些 dll,这些 dll 一起位于一个目录中。)

任何有关下一步转向的线索都将不胜感激!非常感谢。

最佳答案

终于找到答案了!

当引用 Prism 模块中的 DLL 时,.NET 程序集解析机制会在 hosting 应用程序(即具有 Shell 和 Bootstrapper 的应用程序)的/bin 文件夹中查找引用的程序集,而不是而不是在模块的 bin 目录中(假设您已经像我一样在解决方案中将模块设置为单独的项目)。

巧合的是,我的托管应用程序碰巧引用了所有其他 Prism DLL,因此当模块引用这些 DLL 时,它只是在托管应用程序的 bin 目录中找到它们。但是,我的托管应用程序从未引用过 System.Windows.Interactivity,因此仅将它添加到我的模块意味着在托管应用程序/bin 目录中找不到这样的 DLL。 DLL 实际上被复制到模块的/bin 目录,但程序集解析与 Prism 应用程序一起工作的方式的一些怪癖意味着该应用程序从不检查该文件夹中的程序集。

因此,简而言之:Prism 应用程序中引用的程序集显然需要驻留在托管应用程序的/bin 文件夹中。

我将研究一些使用配置的方法,使这项工作更干净、更智能,但问题的核心至少已经被发现。

关于c# - Prism 程序集引用失败 : System. Windows.Interactivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9650778/

相关文章:

c# - GetType() == typeOf(string) 或者 var 是 string ...?

c# - 字符串列表解析

.net - asp.net 中 DataBinding 的最佳实践以实现可维护性

c# - .NET 字符串真的应该被认为是不可变的吗?

c# - 在 FolderBrowserDialog 对话框中禁用右键单击 - wpf?

c# - 从同步方法中的异步方法返回结果

c# - SmtpClient - 操作超时

c# - Linq检查数据表中是否存在数据行

wpf - VS2010 的 MVVM visual studio 项目模板?

wpf - 强制数据绑定(bind) WPF ListBox 更新的更好方法?