有一个简单的工作流类:
public sealed class CodeList : NativeActivity { public string CellType { get; set; } = "1";
public Dictionary<string, string> TypeDict2 { get; set; }
public CodeList() :base ()
{
TypeDict2 = new Dictionary<string, string>();
TypeDict2.Add("全部", "0");
//TypeDict2.Add("空", 1);
//TypeDict2.Add("rust", 2);
}
protected override void CacheMetadata(NativeActivityMetadata metadata)
{
base.CacheMetadata(metadata);
}
protected override void Execute(NativeActivityContext context)
{
}}
如果将其添加到工作流项目的xaml文件中,保存时抛出XamlObjectWriterException,提示检查是否有无效的内容、命名空间、引用或引用循环。
异常堆栈信息:
System.Xaml.XamlObjectWriterException: “向类型为“{clr-namespace:System.Collections.Generic;assembly=mscorlib}Dictionary({http://schemas.microsoft.com/winfx/2006/xaml}String, {http://schemas.microsoft.com/winfx/2006/xaml}String)”的字典中添加值引发了异常。”,行号为“41”,行位置为“31”。 ---> System.ArgumentException: 已添加了具有相同键的项。
在 System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
在 System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
在 System.Collections.Generic.Dictionary`2.System.Collections.IDictionary.Add(Object key, Object value)
在 MS.Internal.Xaml.Runtime.ClrObjectRuntime.AddToDictionary(Object collection, XamlType dictionaryType, Object value, XamlType valueXamlType, Object key)
--- 内部异常堆栈跟踪的结尾 ---
在 MS.Internal.Xaml.Runtime.ClrObjectRuntime.AddToDictionary(Object collection, XamlType dictionaryType, Object value, XamlType valueXamlType, Object key)
在 System.Xaml.XamlObjectWriter.Logic_AddToParentDictionary(ObjectWriterContext ctx, Object key, Object value)
在 System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentCollection(ObjectWriterContext ctx)
在 System.Xaml.XamlObjectWriter.WriteEndObject()
在 System.Activities.Presentation.Xaml.ErrorTolerantObjectWriter.XamlFragment.WriteTo(XamlWriter writer, Boolean parentHasError)
在 System.Activities.Presentation.Xaml.ErrorTolerantObjectWriter.XamlFragment.WriteTo(XamlWriter writer, Boolean parentHasError)
在 System.Activities.Presentation.Xaml.ErrorTolerantObjectWriter.CompleteLoad()
在 System.Xaml.XamlServices.Transform(XamlReader xamlReader, XamlWriter xamlWriter, Boolean closeWriter)
在 Microsoft.Activities.Presentation.Xaml.WorkflowDesignerXamlHelper.DeserializeString(String text, DeserializationMode mode, IList`1& loadErrors, Dictionary`2& sourceLocations)
在 Microsoft.Activities.Presentation.Xaml.WorkflowDesignerXamlHelper.DeserializeString(String text, IList`1& loadErrors, Dictionary`2& sourceLocations)
在 Microsoft.Activities.Presentation.Xaml.WorkflowDesignerXamlHelper.SerializeToString(Object obj, String fileName)
在 System.Activities.Presentation.WorkflowDesigner.WriteModelToText(String fileName)看异常堆栈,它似乎把Dictionary属性的项重新添加了一遍,为什么会这样我也不清楚。
而如果我把public Dictionary<string, string> TypeDict2 { get; set; }修改为public IDictionary<string, string> TypeDict2 { get; set; }则不会抛出异常。
而且绑定Dictionary泛型类,在XAML中绑定:
<ComboBox Name="cb" Grid.Column="1" ItemsSource="{Binding Path=ModelItem.TypeDict2}"
SelectedValuePath="Value"
SelectedValue="{Binding Path=ModelItem.CellType3}"
Validation.ErrorTemplate="{StaticResource vt2}"
DisplayMemberPath="Key">
</ComboBox>经过测试,对于Value,只有String类型可以正常绑定。对于值类型,自定义的类都不行,自定义的类重写了Equals方法。
一个变通的方法是,我们绑定ObservableCollection<CustomKV>,
public class CustomKV
{
public string Key { get; set; }
public int Value { get; set; }
}