【问题标题】:Embedding DLLs and referencing from XAML嵌入 DLL 并从 XAML 引用
【发布时间】:2013-03-12 15:45:52
【问题描述】:

我有一个使用 GMAP.Net 和扩展 WPF 工具包的 WPF 应用程序。我直接在我的 XAML 中引用这些库中的控件。我想将这些 dll 添加到我的项目中并将它们标记为嵌入式资源。

当我不再使用 dll 作为参考时,如何继续从 XAML 中引用控件?

编辑:显示的代码不多。这在我使用 gmap 作为参考时有效。

xmlns:WindowsPresentation="clr-namespace:GMap.NET.WindowsPresentation;assembly=GMap.NET.WindowsPresentation"

删除 gmap 作为参考并添加为嵌入式资源后出现错误消息:

Error 1 The name "GMapControl" does not exist in the namespace "clr-namespace:GMap.NET.WindowsPresentation;assembly=GMap.NET.WindowsPresentation". Windows\MapWindow.xaml

xmlns intelisense/dropdown 中不存在 gmap 引用

【问题讨论】:

  • 您尝试过任何方法吗?展示你的努力..
  • 我已经从我的引用列表中删除了 dll,并将它们作为嵌入式资源添加到我的项目中。现在我的项目无法编译,因为我在 XAML 中的 xmlns 查找无法解析。

标签: c# wpf xaml


【解决方案1】:

保留引用的 dll,在构建最终版本时不要复制它们。另外,您是否向 AssemblyResolve 添加了代码以提取嵌入式资源?我的代码使用压缩程序集。

        AppDomain.CurrentDomain.AssemblyResolve += ( sender, args ) =>
        {
            try
            {
                String resourceName = String.Format( "Program.Libs.{0}.dll.gz", new AssemblyName( args.Name ).Name );
                using (var stream = new GZipStream( Assembly.GetExecutingAssembly().GetManifestResourceStream( resourceName ), CompressionMode.Decompress ))
                using (var outstream = new MemoryStream())
                {
                    CopyTo( stream, outstream );
                    return Assembly.Load( outstream.GetBuffer() );
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine( e.ToString() );
                return null;                    
            }
        };

    public static long CopyTo( Stream source, Stream destination )
    {
        byte[] buffer = new byte[ 2048 ];
        int bytesRead;
        long totalBytes = 0;
        while ((bytesRead = source.Read( buffer, 0, buffer.Length )) > 0)
        {
            destination.Write( buffer, 0, bytesRead );
            totalBytes += bytesRead;
        }
        return totalBytes;
    }

【讨论】:

  • gzip 背后的原因是什么?我以前见过这个
  • 只是让主 exe 更小。 GZip 可以很好地压缩,大约 50% 左右。
猜你喜欢
  • 2012-03-14
  • 1970-01-01
  • 2015-03-09
  • 1970-01-01
  • 2013-09-09
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 2023-03-28
相关资源
最近更新 更多