【问题标题】:Can't save type System.Version to My.Settings无法将 System.Version 类型保存到 My.Settings
【发布时间】:2017-05-09 18:34:13
【问题描述】:

我有一个小型测试项目,用于从服务器检查应用程序版本并提示用户更新。除了我无法将System.Version 类型保存到My.Settings 之外,一切正常。 (我想保存新版本,以防用户要求不再提醒。)

现在,我知道我可以将 Version 保存为字符串并来回转换 - 我已经这样做以解决这个问题 - 但由于 System.Version 列在可用的设置数据类型中,我认为它应该可以工作.但不是保存版本,它只是保存一个没有值的 XML 条目“版本”(请参阅​​下文)。

我正在使用 VB.NET、VS 2013、.NET 4。

下面是一些代码:

Settings.Designer.vb

<Global.System.Configuration.UserScopedSettingAttribute(),  _
Global.System.Diagnostics.DebuggerNonUserCodeAttribute()>  _
Public Property DoNotRemindVersion() As Global.System.Version
    Get
        Return CType(Me("DoNotRemindVersion"),Global.System.Version)
    End Get
    Set
        Me("DoNotRemindVersion") = value
    End Set
End Property

作业示例

If My.Settings.DoNotRemind Then My.Settings.DoNotRemindVersion = oVersion.NewVersion

oVersion.NewVersion 的类型为 System.Version。)

保存在 user.config 中

<setting name="DoNotRemindVersion" serializeAs="Xml">
    <value>
        <Version xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
    </value>
</setting>

那么,我做错了什么?我已经阅读了一些关于必须序列化类以将它们保存到用户设置的帖子,但这是一个简单的版本号,从表面上看,我希望它是受支持的数据类型。

【问题讨论】:

  • 我尝试添加Version 类型的设置并在项目属性中设置它的值,但我希望工作的值都没有实际工作,所以我猜它是只是不可能。

标签: vb.net my.settings system.version


【解决方案1】:

System.Version ClassSerializableAttribute 标记,但您需要用SettingsSerializeAsAttribute 标记属性并传递System.Configuration.SettingsSerializeAs.Binary 值。

您不能使用项目设置设计器界面自动生成您在Settings.designer.vb 文件中找到的代码。您需要自己扩展 My Namespace - MySettings 类。这是部分类有用的领域之一。

向您的项目添加一个新的类文件,并命名一些创意,例如 CustomMySettings。在这个新文件中选择并删除自动生成的代码,并将其替换为以下代码。

Namespace My
    Partial Friend NotInheritable Class MySettings
        ' The trick here is to tell it serialize as binary
        <Global.System.Configuration.SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Binary)> _
        <Global.System.Configuration.UserScopedSettingAttribute(), _
        Global.System.Diagnostics.DebuggerNonUserCodeAttribute()> _
        Public Property DoNotRemindVersion() As Global.System.Version
            Get
                    Return CType(Me("DoNotRemindVersion"), Global.System.Version)
            End Get
            Set(value As Global.System.Version)
                    Me("DoNotRemindVersion") = value
            End Set
        End Property
    End Class
End Namespace

这将允许您使用My.Settings.DoNotRemindVersion,就像您通过设计器创建设置一样。第一次访问该设置时,它将有一个 null (Nothing) 值,因此您可以在 Form.Load 事件处理程序中使用以下内容对其进行初始化。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim savedVersion As Version = My.Settings.DoNotRemindVersion
    If savedVersion Is Nothing Then
        My.Settings.DoNotRemindVersion = New Version(1, 0)
        My.Settings.Save()
    End If
End Sub

【讨论】:

  • @TnTinMan 感谢您的回答。 100% 来自功能性 POV,但为了简单性、设置可读性等,我可能会坚持使用字符串转换。为了这么简单的事情不得不跳过这些圈子,真是太可惜了。我认为 MS 应该只在浏览 My.Settings 原生支持的数据类型中显示。否则会产生误导。
猜你喜欢
  • 2018-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多