以共享形式引用本地设置

时间:2013-02-25 02:02:27

标签: c# .net vb.net

在.NET 4 winforms中,我有一个包含多个项目的解决方案。在其中两个项目中,我想记住每个表单的表单设置,所以每个表单都继承自一个'CustomForm',它保存并加载位置,大小等设置类,使用ferived表单的名称作为参考。

作为参考,CustomForm代码如下所示:

Public Class CustomForm : Inherits Form

    Private ReadOnly Property SuidenSettings As My.MySettings
        Get
            Return My.MySettings.Default
        End Get
    End Property


    ''' <summary>
    ''' Restore the window settings from the settings file
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub RestoreSettings() Handles MyClass.Load

        Dim state As FormWindowState
        Dim settings As List(Of String) = SuidenSettings.Properties.Cast(Of SettingsProperty).Where(Function(p) p.Name.StartsWith(Me.Name)).Select(Function(p) p.Name).ToList()
        Dim primaryWorkArea As Rectangle = Screen.PrimaryScreen.WorkingArea

        ' Make sure each setting exists before accessing or exceptions will fly
        If settings.Any() Then
            If settings.Contains(Me.Name & "_Location") Then
                Me.Location = CType(SuidenSettings(Me.Name & "_Location"), Point)
            End If

            If settings.Contains(Me.Name & "_Size") Then
                Me.Size = CType(SuidenSettings(Me.Name & "_Size"), Size)
            End If

            If settings.Contains(Me.Name & "_State") Then
                If [Enum].TryParse(SuidenSettings(Me.Name & "_State").ToString(), state) Then
                    Me.WindowState = state
                Else
                    Me.WindowState = FormWindowState.Normal
                End If
            End If
        End If

        ' Verify that the form is actually positioned on screen, otherwise position it on the primary screen
        If Not IsOnScreen() Then
            Me.Location = New Point(Me.Location.X Mod primaryWorkArea.Width, Me.Location.Y Mod primaryWorkArea.Height)
            If Not primaryWorkArea.Contains(New Rectangle(Me.Location, Me.Size)) Then
                Me.Location = New Point(primaryWorkArea.Top, primaryWorkArea.Left)
            End If
        End If

    End Sub

    ''' <summary>
    ''' Save the form settings to the settings file
    ''' </summary>
    Public Overridable Sub SaveSettings() Handles MyClass.FormClosed

        Dim settings As List(Of String) = SuidenSettings.Properties.Cast(Of SettingsProperty).Where(Function(p) p.Name.StartsWith(Me.Name)).Select(Function(p) p.Name).ToList()

        ' Make sure each setting exists before accessing or exceptions will fly
        If settings.Any() Then
            If settings.Contains(Me.Name & "_Location") Then
                SuidenSettings(Me.Name & "_Location") = Me.Location
            End If

            If settings.Contains(Me.Name & "_Size") Then
                SuidenSettings(Me.Name & "_Size") = Me.Size
            End If

            If settings.Contains(Me.Name & "_State") Then
                SuidenSettings(Me.Name & "_State") = Me.WindowState
            End If
            Call SuidenSettings.Save()
        End If

    End Sub

    ''' <summary>
    ''' Check if the form is at least partially visible on screen
    ''' </summary>
    ''' <returns>True if the form is partially visible on screen, else false</returns>
    Private Function IsOnScreen() As Boolean

        Dim formRectangle As Rectangle = New Rectangle(Me.Location, Me.Size)

        Return Screen.AllScreens.Any(Function(s) s.WorkingArea.IntersectsWith(formRectangle))

    End Function

End Class

当我还有一个项目并且工作正常时,我使用了这种方法。现在,我不得不复制该项目以运行两个(略有不同的程序并排 - 请不要问为什么我必须这样做,我只是必须)。所以我已经取出了CustomForm对象,并把它放在一个单独的项目中,我在两个应用程序项目中都引用了它。

我现在遇到的问题是我不知道如何修改CustomForm代码,以便它引用一个设置文件,该文件对于正在使用该表单的项目是本地的,而不是而不是共享项目中的设置文件。

由于使用表单名称检索表单设置,并且两个项目几乎完全相同,因此所有表单都具有相同的名称。我想避免将项目中的所有表单重命名为略有不同的名称,而是从项目本地的设置文件中加载设置。这可能吗?

C#或VB.NET中的任何答案都可以。

2 个答案:

答案 0 :(得分:1)

您可以重构代码以使用应用程序可执行路径。

例如:

If settings.Contains(Application.ExecutablePath & "_" & Me.Name & "_Location") Then
    SuidenSettings(Application.ExecutablePath & "_" & Me.Name & "_Location") = Me.Location
End If

答案 1 :(得分:0)

Derek Thomas的回答让我走上正轨。我想我可以在Me.GetType().Assembly.GetName().Name类中使用CustomForm,它返回实例所在程序集的名称。然后我可以重新设置我的设置以包含程序集+表单名称。

这不是我要求的,但它确实是我猜的。