【问题标题】:Can I force the use of an assembly name on xmlns:local in xaml?我可以在 xaml 中强制在 xmlns:local 上使用程序集名称吗?
【发布时间】:2012-06-29 15:35:10
【问题描述】:

在 .xaml 活动中,对同一程序集中的类型的引用使用命名空间 'xmlns:local' 而没有程序集引用,如下所示:

xmlns:local="clr-namespace:Foo.Bar"

由于没有程序集,我加载 xaml 的单元测试失败,我的代码在第三个程序集(Azure 工作者角色)中也无法加载它,因为它不知道命名空间属于哪个程序集。

我是否可以强制 xmlns 像这样引用程序集(以及 xaml 文件中的所有其他 xmlns):

xmlns:local="clr-namespace:Foo.Bar;assembly=Foo"

如果我手动添加;assembly=Foo,它可以工作,但是当我更改某些内容时,Visual Studio 会不断删除程序集字符串。

【问题讨论】:

  • 那么,您是从单元测试中的文本加载活动吗?您可能必须将其分开,以便您的单元测试可以处理已加载的程序集。

标签: visual-studio-2010 xaml workflow-foundation-4


【解决方案1】:

不,你不能强制 LocalAssembly。

您可以只引用程序集并以这种方式使用活动。或者,如果您正在加载 .xaml 文件,您可以使用 XamlXmlReader 指定 LocalAssembly。

我在Microsoft.Activities.Extensions 的最新版本中添加了一个名为 XamlHelper 的类,它可以为您执行此操作,但它是这样工作的

    /// <summary>
    /// Loads a XAML or XAMLX file
    /// </summary>
    /// <param name="xamlFile">
    /// The xaml file. 
    /// </param>
    /// <param name="localAssembly">
    /// The local assembly. 
    /// </param>
    /// <returns>
    /// The activity or root activity of a WorkflowService 
    /// </returns>
    public static Activity Load(string xamlFile, Assembly localAssembly)
    {
        Contract.Requires(localAssembly != null);
        if (localAssembly == null)
        {
            throw new ArgumentNullException("localAssembly");
        }

        var readerSettings = new XamlXmlReaderSettings
            {
                LocalAssembly = localAssembly, 
                AllowProtectedMembersOnRoot = true
            };

        var xamlType = GetXamlType(xamlFile);
        switch (xamlType)
        {
            case WorkflowXamlFileType.Activity:
                using (var reader = new XamlXmlReader(xamlFile, readerSettings))
                {
                    return ActivityXamlServices.Load(reader);
                }

            case WorkflowXamlFileType.WorkflowService:
                using (var reader = new XamlXmlReader(xamlFile, readerSettings))
                {
                    return ((WorkflowService)XamlServices.Load(reader)).GetWorkflowRoot();
                }

            default:
                throw new ArgumentException("Invalid file extension on xamlFile");
        }
    }

【讨论】:

    猜你喜欢
    • 2012-05-16
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多