【问题标题】:Using C# Settings w/ customized class将 C# 设置与/自定义类一起使用
【发布时间】:2011-09-22 13:49:17
【问题描述】:

我正在尝试使用我自己制作的类型定义设置,但我似乎无法在设置 UI 的类型组合框中找到我的类(也无法在由在 ComboBox 中选择“浏览”)。

如何在设置中使用自定义类?

我的班级:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Key = System.Windows.Input.Key;

namespace GameOfLife
{
    [Serializable()]
    class KeyShortCut
    {
        [XmlElement("Key")]
        public Key Key { get; private set; }

        [XmlAttribute("Ctrl")]
        public bool Ctrl { get; private set; }

        [XmlAttribute("Alt")]
        public bool Alt { get; private set; }

        [XmlAttribute("Shift")]
        public bool Shift { get; private set; }

        public KeyShortCut(Key Key, bool Ctrl = false, bool Alt = false, bool Shift = false)
        {
            this.Key = Key;

            this.Ctrl = Ctrl;
            this.Alt = Alt;
            this.Shift = Shift;
        }
        public override string ToString()
        {
            StringBuilder str = new StringBuilder(this.Key.ToString());
            if (Ctrl)
                str.Insert(0, "Ctrl + ");
            if (Alt)
                str.Insert(0, "Alt + ");
            if (Shift)
                str.Insert(0, "Shift + ");
            return str.ToString();
        }
    }
}

【问题讨论】:

    标签: c# .net settings


    【解决方案1】:

    尝试使用IXmlSerializable 而不是Serializable,因为它只定义二进制可串行化或读取THIS 问题/答案。

    【讨论】:

    • 无属性。但是,同名的接口确实存在。我应该同时实现接口并附加 [Serializable] 属性吗?
    猜你喜欢
    • 1970-01-01
    • 2012-05-25
    • 2017-11-20
    • 2014-04-08
    • 1970-01-01
    • 2020-08-28
    • 2017-08-31
    • 2012-04-01
    • 1970-01-01
    相关资源
    最近更新 更多