集合属性初始化问题 - 设计时间 [英] Collection property initialisation issue - design time
问题描述
我创建了一个属性,它是一个对象集合,集合的自定义 UITypeEditor
,以及 TypeConverter
对象。所有似乎都工作正常,除非我进入并在设计师中编辑第二次或以后的集合,集合的新值未初始化。
我认为我的代码的重要部分是:
集合属性:
I have created a property which is a collection of objects, with a custom UITypeEditor
for the collection, and a TypeConverter
for the objects. All seems to be working OK except if I go in and edit the collection in the designer for a 2nd or subsequent time, the new values of the collection are not being initialised.
I think the important bits of my code are:
The collection property:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor(typeof(FileTypeEditor), typeof(UITypeEditor))]
public FileTypes FileTypes { get; internal set; } = new FileTypes();
收集项目:
The collection item:
[Serializable]
[TypeConverter(typeof(FileTypeConverter))]
public class FileType {
internal FileType() { }
public FileType(string description, string filter) {
Description = description; Filter = filter;
}
// ...
< br $>
TypeConverter
The TypeConverter
class FileTypeConverter : TypeConverter {
static Regex srch = new Regex(@"^(.*)\s\((.*)\)$", RegexOptions.Compiled);
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
if (destinationType == typeof(string) || destinationType == typeof(InstanceDescriptor))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
if (value is string) {
try {
Match m = srch.Match((string)value);
return new FileType(m.Groups[1].Value, m.Groups[2].Value);
}
catch {
return new FileType();
}
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
if (value is FileType) {
FileType ft = (FileType)value;
if (destinationType == typeof(string)) {
return $"{ft.Description} ({ft.Filter})";
}
if (destinationType == typeof(InstanceDescriptor)) {
return new InstanceDescriptor(typeof(FileType).GetConstructor(new[] { typeof(string), typeof(string) }), new object[] { ft.Description, ft.Filter });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
UITypeEditor
The UITypeEditor
public class FileTypeEditor : UITypeEditor {
IWindowsFormsEditorService _editorService;
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
if (provider != null) {
_editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
if (_editorService != null) {
using (FileTypeEditorForm frm = new FileTypeEditorForm((FileTypes)value, _editorService)) {
if (_editorService.ShowDialog(frm) == DialogResult.OK) {
return frm.FileTypes;
}
}
}
}
return value;
}
}
第一次编辑属性后生成的初始化代码:
The generated initialisation code after the property is edited the first time:
this.fileOpenDialog1.FileTypes.AddRange(new CommonItemDialog.FileType[] {
new CommonItemDialog.FileType("All Files", "*.*"),
new CommonItemDialog.FileType("Audio Files", "*.m4a;*.mp3;*.wav;*.wma")});
这一切都很完美。唯一的问题是,如果我去更改集合 - 添加新项目或更改项目,此初始化不会更改以反映已编辑的集合。
什么是我在这里失踪了?我的智慧结束了。
我尝试过的事情:
搜索谷歌,阅读吨文章(包括但不限于CP here [ ^ ]和此处 [ ^ ]) - 我只是看不出我是什么缺少这里。
Which is all perfect. This only issue is if I go and change the collection - add a new item, or change an item, this initialisation does not change to reflect the edited collection.
What am I missing here? I'm at my wit's end.
What I have tried:
Searching google, reading tonnes of articles (including, but not limited to CP here[^] and here[^]) - I just can't see what I'm missing here.
推荐答案
,RegexOptions.Compiled);
public 覆盖 bool CanConvertFrom(ITypeDescriptorContext context,Type sourceType){
if (sourceType == typeof ( string ) )
return true ;
return base .CanConvertFrom(context,sourceType);
}
public 覆盖 bool CanConvertTo(ITypeDescriptorContext context,Type destinationType){
if (destinationType == typeof ( string )|| destinationType == typeof (InstanceDescriptor))
返回 true ;
return base .CanConvertTo(context,destinationType);
}
public 覆盖 object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture, object value ){
if ( value string ){
try {
匹配m = srch.Match((字符串) value );
return new FileType(m.Groups [ 1 ]。值,m。组[ 2 ]。值);
}
catch {
return new FileType();
}
}
返回 base .ConvertFrom(context,culture , value );
}
public 覆盖 object ConvertTo(ITypeDescriptorContext context,CultureInfo culture, object value ,输入destinationType){
if ( value FileType){
FileType ft =(FileType) value ;
if (destinationType == typeof ( string )){
return
", RegexOptions.Compiled); public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) return true; return base.CanConvertFrom(context, sourceType); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(string) || destinationType == typeof(InstanceDescriptor)) return true; return base.CanConvertTo(context, destinationType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { try { Match m = srch.Match((string)value); return new FileType(m.Groups[1].Value, m.Groups[2].Value); } catch { return new FileType(); } } return base.ConvertFrom(context, culture, value); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (value is FileType) { FileType ft = (FileType)value; if (destinationType == typeof(string)) { return
{ft.Description}({ft.Filter});
}
if (destinationType == typeof (InstanceDescriptor)){
return new InstanceDescriptor( typeof ( FileType).GetConstructor( new [] { typeof ( string ), typeof ( string )}), new object [] {ft.Description,ft.Filter});
}
}
返回 base .ConvertTo(context,culture , value ,destinationType);
}
}
"{ft.Description} ({ft.Filter})"; } if (destinationType == typeof(InstanceDescriptor)) { return new InstanceDescriptor(typeof(FileType).GetConstructor(new[] { typeof(string), typeof(string) }), new object[] { ft.Description, ft.Filter }); } } return base.ConvertTo(context, culture, value, destinationType); } }
UITypeEditor
The UITypeEditor
public class FileTypeEditor : UITypeEditor {
IWindowsFormsEditorService _editorService;
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
if (provider != null) {
_editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
if (_editorService != null) {
using (FileTypeEditorForm frm = new FileTypeEditorForm((FileTypes)value, _editorService)) {
if (_editorService.ShowDialog(frm) == DialogResult.OK) {
return frm.FileTypes;
}
}
}
}
return value;
}
}
第一次编辑属性后生成的初始化代码:
The generated initialisation code after the property is edited the first time:
this.fileOpenDialog1.FileTypes.AddRange(new CommonItemDialog.FileType[] {
new CommonItemDialog.FileType("All Files", "*.*"),
new CommonItemDialog.FileType("Audio Files", "*.m4a;*.mp3;*.wav;*.wma")});
这一切都很完美。唯一的问题是,如果我去更改集合 - 添加新项目或更改项目,此初始化不会更改以反映已编辑的集合。
什么是我在这里失踪了?我的智慧结束了。
我尝试过的事情:
搜索谷歌,阅读吨文章(包括但不限于CP here [ ^ ]和此处 [ ^ ]) - 我只是看不出我是什么在这里失踪。
Which is all perfect. This only issue is if I go and change the collection - add a new item, or change an item, this initialisation does not change to reflect the edited collection.
What am I missing here? I'm at my wit's end.
What I have tried:
Searching google, reading tonnes of articles (including, but not limited to CP here[^] and here[^]) - I just can't see what I'm missing here.
这篇关于集合属性初始化问题 - 设计时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!