程序集依赖项问题



>情况:

我有树项目(ZShared,ZSearcher和ZClient(,它们将相互引用。

ZShared 是一个常见的 DLL 程序集,包含一些样式和资源。

ZSearcher也是一个带有一些WPF控件的DLL程序集。

理论上,ZClient 可以是用于测试目的的任何东西(WPF 应用程序、Winforms、Excel 等(,我已将其制作为 WPF 应用程序。

问题:

当我在 ZSearcher 中引用 ZShared 时,它会生成两个 DLL 文件:ZShared.DLL 和 ZSearcher.DLL

当在 ZClient 中引用 ZSearcher 时,只有 ZSearcher 被复制到 ZClient 文件夹中。这也可以通过引用ZShared来解决。

但我希望ZSearcher作为一个独立的应用程序工作。就像引用ZSearcher时一样,依赖项应该自动跟随。

因此,我认为也许使用反射而不是引用可以解决问题。但是反思也会出现完全相同的问题。

System.Windows.Markup.XamlParseException HResult=0x80131501
Message='Set property 'System.Windows.ResourceDictionary.Source' 抛出 是个例外。行号"10"和行位置"18"。
Source=PresentationFramework
StackTrace: at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri( at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri( at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream( at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator( at ZSearcher.SearcherWindow.InitializeComponent(( 在 C:\Usersn\Desktop\WorkSpaceVS\TestApplication\ZSearcher\SearcherWindow.xaml:line 1

内部异常 1:文件未找到异常:无法加载文件或 程序集"ZShared, Culture=neutral" 或其依赖项之一。这 系统找不到指定的文件。

转载问题:

创建.NET-FrameworkC#DLL程序集项目 (ZShared(。此程序集仅包含一个ResourceDictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ZSolidColorBrushRed" Color="Red"/>
<Style x:Key="ZButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="Green"/>
</Style>
</ResourceDictionary>

创建另一个.NET-FrameworkC#DLL程序集项目 (ZSearcher(。此程序集包含一个Window,一次Class

搜索者.cs类:

namespace ZSearcher
{
public static class Searcher
{
public static object Search(string param)
{
var window = new SearcherWindow();
window.ShowDialog();
return null;
}
}
}

SearcherWindow.xaml:

<Window x:Class="ZSearcher.SearcherWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="500"
Title="ZSearcher">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://Application:,,,/ZShared;component/ZResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<StackPanel Margin="20">
<TextBlock Text="SolidColorBrush test" Foreground="{DynamicResource ZSolidColorBrushRed}"/>
<Button Content="Button style test" Style="{DynamicResource ZButtonStyle}"/>
</StackPanel>
</Grid>
</Window>

参考 ZSearcher 项目中的 ZShared.DLL。

创建.NET-FrameworkWPFC#应用。此应用程序仅包含主窗口。

MainWindow.xaml:

<Window x:Class="ZClient.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ZClient" Width="400" Height="200">
<Grid>
<Button Content="Open searcher" Click="OpenSearcher_Click" Width="100" Height="30"/>
</Grid>
</Window>

主窗口.cs

using System.Reflection;
using System.Windows;
using ZSearcher;
namespace ZClient
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void OpenSearcher_Click(object sender, RoutedEventArgs e)
{
//Referenced tester
var referenceTest = Searcher.Search("Test");
//Reflection tester
var test = Test("Search");
}
private static object Test(string methodName)
{
var assembly = Assembly.LoadFrom(@"C:UsersnnDesktopWorkSpaceVSTestApplicationZSearcherbinDebugZSearcher.DLL");
var type = assembly.GetType("ZSearcher.Searcher");
if (type == null) return null;
var methodInfo = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static);
if (methodInfo == null) return null;
var parametersArray = new object[] { "Test" };
return methodInfo.Invoke(null, parametersArray);
}
}
}

问题:

如何使这个ZSearcher组件作为一个独立的工作?

当 MSBuild 生成解决方案时,它需要在 ZSearcher 和 ZShared 之间有一个代码级引用,以便正确检测依赖项并将其复制到 ZClient bin 文件夹。

有些人会创建一个虚拟代码引用来解决这个问题。

using ZShared;
namespace ZSearcher
{
public static class Searcher
{
static Searcher()
{
// Reference something from ZShared here...
}
public static object Search(string param)
{
var window = new SearcherWindow();
window.ShowDialog();
return null;
}
}
}

最新更新