簡體   English   中英

BinaryEditor 類不適用於 .net 6 預覽中的 PropertyGrid 控件

[英]BinaryEditor class doesn't work for PropertyGrid control in .net 6 preview

[問題描述] 選擇要顯示的 PropertyGrid 控件的對象后,如果用戶嘗試單擊並編輯其任何 Byte[] 屬性,則會出現如下所示的錯誤消息,並且假定 BinaryEditor 未出現:

在資源“System.Windows.Forms.Design.BorderSidesEditorresources”System.ComponentMode.Design.CollectionEditor.resources“System.Windows.Forms.Design.FormatControl.resources”中找不到資源“System.ComponentModel.Design.BinaryEditorresources” System.Windows.Forms.Desiqn.LinkAreaEditorresources",System.Wingows.Forms.Desian.MaskDesignerDialog.resources"System.Wingows.Forms.Desian.ShortcutKevsEditorresources"。 System.SR.resources“System.Wingows.Forms.DesignStringCo ectionEditorresources”。 System.Windows.Forms.Design.Resources.System.ComponentMode.Design.BinaryEditor.resources" 'System.Windows.Forms.Design.colordlg.data"... 嵌入在程序集中 "System.Windows.Forms.Design 也不在指定區域性的任何附屬程序集中的資源中。也許資源嵌入了不正確的名稱在此處輸入圖像描述

[重現步驟] 將 PropertyGrid 控件添加到您的 WinForm。 將 PropertyGrid.SelectedObject 設置為具有 Byte[] 類型屬性的對象。 運行窗體,然后單擊 PropertyGrid 中的 Byte[] 類型屬性,您會看到錯誤消息。

[.NET 版本信息] 版本:6.0.400-preview.22301.10 提交:25580ffe7a

主機(用於支持):版本:6.0.6 提交:7cca709db2

.NET SDK 安裝:6.0.301 [C:\Program Files\dotnet\sdk] 6.0.400-preview.22301.10 [C:\Program Files\dotnet\sdk]

.NET 運行時安裝:Microsoft.AspNetCore.App 3.1.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft .AspNetCore.App] Microsoft.AspNetCore.App 6.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 3.1.25 [C:\Program Files\dotnet\shared\Microsoft .NETCore.App] Microsoft.NETCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.6 [C:\Program Files\dotnet\shared\Microsoft .NETCore.App] Microsoft.WindowsDesktop.App 3.1.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft .WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

解決方法解釋

第一步,使用 TypeConverterAttribute 或 EditorAttribute 或兩者標記屬性。

    [TypeConverter(typeof(Hex2BinConverter)), Editor("MyWinFormApp.ByteArrayEditor, MyWinFormApp","System.Drawing.Design.UITypeEditor, System.Windows.Forms")]

解決方法 1:TypeConverter 的簡單解決方案:

   public class Hex2BinConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string)) return true;
            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string s && Utils.TryHex2Bytes(s, out byte[] bs)) return bs;  //TryHex2Bytes might be any method u wrote to convert hexstring to byte[]

            object r = null;
            try { r = base.ConvertFrom(context, culture, value); }
            catch { }
            return r;
        }

        public override object ConvertTo(ITypeDescriptorContext context,
           CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string) && value is byte[] bs) return bs.ToHexString();  //ToHexString might be any method u wrote to convert byte[] to hexstring.
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

解決方法 2:自定義 UITypeEditor 的簡單解決方案:

public class ByteArrayEditor : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;

    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
        TextBox txtBox = new TextBox();

        if (value is byte[] bs) txtBox.Text = bs.ToHexString();

        service.DropDownControl(txtBox);

        if (Utils.TryHex2Bytes(txtBox.Text, out byte[] bs1)) value = bs1;


        return value;

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM