C#一般类库项目中无法添加资源文件(ResourceDictionary)

背景:本人要实现工作流自定义活动中属性栏中的集合属性功能,如图:

1、首先添加三个引用:

    1.PresentationCore
    2.PresentationFramework
    3.WindowsBase

  若项目提示缺少其他引用,则按照提示添加即可,例如(System.Activity、System.Activities.Core.Presentation、       System.Activities.Presentation等等)

2、在类库的工程文件(xxxxxx.csproj)中添加代码

      <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}                          </ProjectTypeGuids>
      <WarningLevel>4</WarningLevel>
      <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

3、添加完资源字典之后,如果x:type报错,那么添加引用System.Xaml程序集即可。(此步骤本人未用,只是在上网查找的时候有这个解决方法) 

4、然后在类库项目中右键添加资源文件,

<ResourceDictionary x:Class="XXXX.Designer.EditorTemplates" 
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:PropertyEditing="clr-namespace:System.Activities.Presentation.PropertyEditing;assembly=System.Activities.Presentation">
    <DataTemplate x:Key="ArgumentDictionaryEditor">
        <DockPanel LastChildFill="True">
            <TextBlock Text="(集合)" TextTrimming="CharacterEllipsis" IsEnabled="False" />
    
            <PropertyEditing:EditModeSwitchButton TargetEditMode="Dialog" HorizontalAlignment="Right" DockPanel.Dock="Right" />
        </DockPanel>
    </DataTemplate>
    
</ResourceDictionary>

5、创建同名称的类文件,实现ResourceDictionary

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace XXXXX.Designer
{
    public partial class EditorTemplates : ResourceDictionary //注意是partial
    {
        public EditorTemplates()
            : base()
        {
            InitializeComponent();
        }
    }
}

6、创建类ArgumenCollectionEditor

using System.Activities.Presentation;
using System.Activities.Presentation.Converters;
using System.Activities.Presentation.Model;
using System.Activities.Presentation.PropertyEditing;
using System.Windows;
namespace XXXXXX.Designer
{
    public class ArgumenCollectionEditor : DialogPropertyValueEditor
    {
        private static DataTemplate EditorTemplate = (DataTemplate)((ResourceDictionary)new EditorTemplates())["ArgumentDictionaryEditor"];

        public ArgumenCollectionEditor()
        {
            base.InlineEditorTemplate = EditorTemplate;
        }

        public override void ShowDialog(PropertyValue propertyValue, IInputElement commandSource)
        {
            string propertyName = propertyValue.ParentProperty.PropertyName;
            ModelItem ownerActivity = new ModelPropertyEntryToOwnerActivityConverter().Convert(propertyValue.ParentProperty, typeof(ModelItem), false, null) as ModelItem;
            ShowDialog(propertyName, ownerActivity);
        }

        public static void ShowDialog(string propertyName, ModelItem ownerActivity)
        {
            DynamicArgumentDesignerOptions options = new DynamicArgumentDesignerOptions
            {
                Title = propertyName
            };
            ModelItem collection = ((ModelMemberCollection<ModelProperty, DependencyProperty>)ownerActivity.Properties)[propertyName].Collection;
            using (ModelEditingScope modelEditingScope = collection.BeginEdit(propertyName + "Editing"))
            {
                if (DynamicArgumentDialog.ShowDialog(ownerActivity, collection, ownerActivity.GetEditingContext(), ownerActivity.View, options))
                {
                    modelEditingScope.Complete();
                }
                else
                {
                    modelEditingScope.Revert();
                }
            }
        }
    }
}

7、对工作流自定义活动组件中的集合属性进行设置

 [Browsable(false)]
        [DefaultValue(null)]
        [Editor(typeof(ArgumenCollectionEditor), typeof(DialogPropertyValueEditor))]
        public List<string> Attachments
        {
            get;
            set;
        }

之后就会产生上面弹出图的效果。

大致思路是这样,如果有其他问题请留言一起讨论。。。。

猜你喜欢

转载自blog.csdn.net/ZXFC88/article/details/82492833