组装依赖性问题

问题描述 投票:0回答:1

状况。

我有一些树状项目(ZShared、ZSearcher和ZClient),它们将相互引用。

ZShared是一个普通的DLL程序集,包含一些样式和资源,ZSearcher也是一个DLL程序集,包含一些WPF控件。

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='设置属性'System.Windows.ResourceDictionary.Source'引发了一个异常。行号'10',行位'18'。 Source=PresentationFramework StackTrace: 在 System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) 在 System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)。 在System.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)。 在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() in C:\Users\nnnn\Desktop\WorkSpaceVS\TestApplication\ZSearcher\SearcherWindow.xaml:1行。

内在异常1:FileNotFoundException.无法加载文件或程序集'ZShared, Culture=neutral'或一个依赖项。无法加载文件或程序集'ZShared, Culture=neutral'或其依赖关系之一。系统无法找到指定的文件。

复制问题。

创建一个 .NET-Framework C# 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-Framework C# DLL 集合体项目(ZSearcher)。这个程序集包含一个 Window 而一旦 Class:

Searcher.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-Framework WPF C# 应用程序。这个应用程序只包含MainWindow。

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>

MainWindow.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:\Users\nn\Desktop\WorkSpaceVS\TestApplication\ZSearcher\bin\Debug\ZSearcher.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程序集作为独立的程序工作?

我怎样才能使这个 ZSearcher 集合体作为一个独立的程序工作?

c# wpf dll .net-assembly
1个回答
1
投票

当 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;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.