wpf - 如何在VB.NET/WPF应用程序中拥有一个全局字典来保存来自不同窗口的数据?

标签 wpf vb.net wpf-controls vb.net-2010

我是 VB.NET 和 WPF 的新手。

我正在构建一个“问卷”应用程序。将依次向用户呈现不同的问题/任务(窗口)。在他们回答每个问题/任务并按下“提交”按钮后,将打开一个包含新问题/任务的新窗口,并且先前的窗口将关闭。每个问题之后,当按下按钮时,我需要将数据存储到某个全局对象中。回答完所有问题后,该对象的数据应写入输出文件。

我发现字典是在每个窗口之后存储结果的最佳选择。

我不知道如何、在哪里创建这个全局词典以及如何访问它。我应该使用 View 模型吗?如果是,您能举个例子吗?或者,它应该只是一个具有共享属性的简单类吗? (类似于this)

编辑2:我尝试了网上推荐的许多不同方法

全局模块:

Module GlobalModule
   Public Foo As String
End Module

全局变量:

Public Class GlobalVariables
   Public Shared UserName As String = "Tim Johnson"
   Public Shared UserAge As Integer = 39
End Class

全局属性:

Public Class Globals

Public Shared Property One As String
    Get
        Return TryCast(Application.Current.Properties("One"), String)
    End Get
    Set(ByVal value As String)
        Application.Current.Properties("One") = value
    End Set
End Property

Public Shared Property Two As Integer
    Get
        Return Convert.ToInt32(Application.Current.Properties("Two"))
    End Get
    Set(ByVal value As Integer)
        Application.Current.Properties("Two") = value
    End Set
End Property

End Class

这是我将数据保存到第一个窗口中的全局变量/属性的位置。在关闭旧窗口并打开新窗口之前,我需要在此子例程中存储数据。我使用 MessageBox 只是为了测试。

Private Sub btnEnter_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEnter.Click

    Dim instructionWindow As InstructionsWindow

    instructionWindow = New InstructionsWindow()

    Application.Current.Properties("number") = textBoxValue.Text

    Globals.One = "2"
    Globals.Two = 3

    MessageBox.Show("GlobalVariables: UserName=" & GlobalVariables.UserName & " UserAge=" & GlobalVariables.UserAge)

    GlobalVariables.UserName = "Viktor"
    GlobalVariables.UserAge = 34

    GlobalModule.Foo = "Test Foo"


    'testing if it saved tha value
    'MessageBox.Show(Application.Current.Properties("number"))

    Application.Current.MainWindow.Close()

    instructionWindow.ShowDialog()

End Sub

下一个子例程是我尝试从第二个窗口中的全局属性/变量中检索值的地方,但消息框显示为空。也可能存在这样的情况:我以错误的方式分配值,或者没有以正确的方式读取它们(强制转换?):

Private Sub FlowDocReader_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles FlowDocReader.Initialized

    ' Get a reference to the Application base class instance.
    Dim currentApplication As Application = Application.Current

    MessageBox.Show(currentApplication.Properties("number"))

    MessageBox.Show("One = " & Globals.One & " Two = " & Globals.Two)

    MessageBox.Show("GlobalVariables: UserName=" & GlobalVariables.UserName & " UserAge=" & GlobalVariables.UserAge)

    MessageBox.Show("GlobalModule.Foo = " & GlobalModule.Foo)

    Dim filename As String = My.Computer.FileSystem.CurrentDirectory & "\instructions.txt"

    Dim paragraph As Paragraph = New Paragraph()
    paragraph.Inlines.Add(System.IO.File.ReadAllText(filename))
    Dim document As FlowDocument = New FlowDocument(paragraph)
    FlowDocReader.Document = document

End Sub

谢谢。

最佳答案

您可以为表单创建公共(public) Dictionary 属性,并将字典放入此属性中,或者使用 Dictionary 参数创建构造函数。

关于wpf - 如何在VB.NET/WPF应用程序中拥有一个全局字典来保存来自不同窗口的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14423568/

相关文章:

c# - Canvas ActualWidth 和 ActualHeight 以 MVVM 方式传入 ViewModel

vb.net - 删除文本框中的结束字符而不重写所有数据

vb.net - VB.NET中的Powershell具有管理员权限

WPF - 选择 Tabitem 或将鼠标悬停在其上时 TabItem 背景颜色会发生变化

c# - 将 WPF 应用程序连接到 MySQL 数据库

c# - 项目中视频的存储位置

wpf - 无法在 wpf 的组合框中捕获向下箭头

wpf - WPF 类库中的全局静态资源?

WPF 数据触发器

vb.net - 具有 NULL 值的 Npgsql 参数,vb.net