【问题标题】:.net 2010 VB.NET Xml Winforms - Persisting MDI child position and state.net 2010 VB.NET Xml Winforms - 保持 MDI 子位置和状态
【发布时间】:2010-09-22 12:04:53
【问题描述】:

我知道有人问过类似的问题,但这是我的问题。

在 MDI WinForm 中,我想保存每个应用程序用户的每个子窗口位置和状态,这与登录的 Windows 用户不同。我的应用程序有自己的用户;所以我不会使用 my.Settings... 等用户设置。

一种选择是直接读/写数据库,但我不喜欢访问数据库的想法,因为窗口位置如此微不足道。好处是我可以通过用户工作的机器独立存储这些信息,无论她在哪里登录,都会记住她的偏好。

我想遵循的另一个选择是使用 Xml 将该信息存储在用户计算机本地的文件中。结构可能类似于:

<form name="form name">
    <Top>120</Top>
            <Left>100</Left>
            <State>0</State>
    </form>

    <form name="another form">
        <Top>120</Top>
            <Left>120</Left>
            <State>1</State>
    </form>

我很难理解如何做到这一点;也许使用 Linq to Xml?我发现我可以写一些简单的东西

Dim formPos As XElement = _
    <User><%= My.Application.connectedUser.id %>
                <form1>
                    <Top>120</Top>
                    <Left>100</Left>
                    <State>0</State>
                </form1>

                <form2>
                    <Top>120</Top>
                    <Left>100</Left>
                    <State>0</State>
                </form2>
         </User>

但是:

1) 如何动态填充 xml 选项:我想要

<User id="1"> 

而不是 &lt;User&gt;&lt;%= My.Application.connectedUser.id %&gt; 转换为 &lt;User&gt;1

2) 构建完成后如何编写 XElement。我应该使用XmlWriter.Create 吗?很高兴将其传递给 XElement?

3)当Xml文件中已经有一个同名节点时会发生什么,如果它们已经存在,我想覆盖以前的用户设置,但不附加到文件,也不重写整个文件,当然

谢谢。

【问题讨论】:

    标签: vb.net linq-to-xml


    【解决方案1】:

    可能对其他人有用:

    Public Shared Sub SaveWindowPosition(ByVal formToSave As Form, ByVal userid As Integer)
    
            Dim formPosLeft As Integer = formToSave.Location.X
            Dim formPosTop As Integer = formToSave.Location.Y
            Dim formDimWidth As Integer = formToSave.Width
            Dim formDimHeight As Integer = formToSave.Height
            Dim formState As Integer = formToSave.WindowState
            Dim formName As String = formToSave.Name
    
    
            '   Apri il file dove memorizzo le posizioni delle varie form
            '
            Dim xdoc As XDocument
            Try
                xdoc = XDocument.Load(FILE_NAME)
            Catch ex As Exception
                '   Non trovato, va creato uno nuovo
                '
                Dim settings As New XmlWriterSettings()
                settings.Indent = True
                Dim writer As XmlWriter = XmlWriter.Create(FILE_NAME, settings)
    
                writer.WriteStartDocument()
                writer.WriteStartElement("Root")
    
                writer.WriteEndElement()
                writer.WriteEndDocument()
                writer.Close()
    
                Try
                    xdoc = XDocument.Load(FILE_NAME)
                Catch ex2 As Exception
                    '   Nemmeno questa volta è riuscito ad aprire il file, basta
                    '
                    Exit Sub
                End Try
            End Try
    
            Dim elementRoot As XElement = xdoc.Element("Root")
    
            '   Cerca l'utente corrente (per id)
            '
            Dim cercaUtente As IEnumerable(Of XElement) =
                From c In elementRoot.Elements("Utente") Where c.Attribute("id").Value = CStr(userid)
            Select c
            If cercaUtente.Count = 0 Then
                '   Utente non trovato, crealo con tutti gli altri elementi
                '
                Dim xutente As XElement =
                    <Utente id=<%= userid %>>
                        <Form name=<%= formName %>>
                            <Position top=<%= formPosTop %> left=<%= formPosLeft %> width=<%= formDimWidth %> height=<%= formDimHeight %> state=<%= formState %>></Position>
                        </Form>
                    </Utente>
    
                elementRoot.Add(xutente)
            Else
    
                '   Utente trovato, cerca la form 
                '
                Dim cercaForm As IEnumerable(Of XElement) =
                    From c In cercaUtente.Elements("Form") Where c.Attribute("name").Value = formName
                Select c
    
                If cercaForm.Count = 0 Then
                    '   Form non trovata, creala
                    '
                    Dim formPos As XElement = _
                        <Form name=<%= formName %>>
                            <Position top=<%= formPosTop %> left=<%= formPosLeft %> width=<%= formDimWidth %> height=<%= formDimHeight %> state=<%= formState %>></Position>
                        </Form>
    
                    cercaUtente.ElementAt(0).Add(formPos)
                Else
                    '   Trovato tutto, sostituisci gli attributi
                    '
                    Dim position As XElement = cercaForm.ElementAt(0).Element("Position")
                    position.ReplaceAttributes(
                        {
                            New XAttribute("top", formPosTop),
                            New XAttribute("left", formPosLeft),
                            New XAttribute("width", formDimWidth),
                            New XAttribute("height", formDimHeight),
                            New XAttribute("state", formState)
                        })
    
                End If
            End If
    
            '   Salva il file
            '
            xdoc.Save(FILE_NAME)
        End Sub
    
    
    
    
    
    
    Public Shared Sub SetWindowPosition(ByVal formToPosition As Form, ByVal userid As Integer)
    
            formToPosition.SuspendLayout()
    
            Dim xdoc As XDocument
            Try
                xdoc = XDocument.Load(FILE_NAME)
            Catch ex As Exception
                '   File non trovato, nulla da fare
                '
                Exit Sub
            End Try
    
            Dim elementRoot As XElement = xdoc.Element("Root")
    
            '   Cerca l'utente corrente (per id)
            '
            Dim cercaUtente As IEnumerable(Of XElement) =
                From c In elementRoot.Elements("Utente") Where c.Attribute("id").Value = CStr(userid)
            Select c
            If cercaUtente.Count = 0 Then Exit Sub
    
            Dim cercaForm As IEnumerable(Of XElement) =
                    From c In cercaUtente.Elements("Form") Where c.Attribute("name").Value = formToPosition.Name
                Select c
    
            If cercaForm.Count = 0 Then Exit Sub
    
            '   Preleva tutti gli attributi
            '
            Dim position As XElement = cercaForm.ElementAt(0).Element("Position")
            Dim top As Integer = CInt(position.Attribute("top"))
            Dim left As Integer = CInt(position.Attribute("left"))
            Dim width As Integer = CInt(position.Attribute("width"))
            Dim height As Integer = CInt(position.Attribute("height"))
            Dim state As FormWindowState = CType([Enum].Parse(GetType(FormWindowState), CStr(position.Attribute("state"))), FormWindowState)
    
            '   Imposta posizione, dimensione e stato, ma solo se lo stato è normale, altrimenti ignora i valori 
            '
            formToPosition.WindowState = state
    
            If state = FormWindowState.Normal Then
                formToPosition.Location = New Point(left, top)
                formToPosition.Width = width
                formToPosition.Height = height
                formToPosition.StartPosition = FormStartPosition.Manual
            End If
    
            '   TODO: controllare che posizione e dimensioni siano NORMALI cioè che la form non vada a finire fuori dallo schermo 
    
            formToPosition.ResumeLayout(False)
        End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      相关资源
      最近更新 更多