在Windows应用程序中有类似SESSION的东西吗?

Man*_*ish 5 c# windows

在Windows应用程序中有类似SESSION的东西吗?我希望存储一些值,以便在表单之间保持持久性.

例如:第一个表单有一些复选框,第三个表单相应地处理它们.所以我需要将复选的复选框存储在某处.

cod*_*ike 5

如果您在同一个应用程序中讨论不同的表单,那么只需在类上创建一些静态成员,它将在可执行文件的生命周期内保留.


Wil*_*ler 3

您只能通过放置复选框的此表单的属性公开您的复选框已选中状态,并从第三个或处理表单访问这些属性。

public partial class MainForm : Form {
    // We assume we have let's say three CheckBoxes named chkFirst, chkSecond and chkThird
    public bool IsFirstChecked { get { return chkFirst.Checked; } }
    public bool IsSecondChecked { get { return chkSecond.Checked; } }
    public bool IsThirdChecked { get { return chkThird.Checked; } }

    // Calling this form from where these checked states will be processed...
    // Let's suppose we have to click a button to launch the process, for instance...
    private void btnLaunchProcess(object sender, EventArgs e) {
        ProcessForm f = new ProcessForm();
        f.Parent = this;
        if (DialogResult.OK == f.ShowDialog()) {
            // Process accordingly if desired, otherwise let it blank...
        }
    }       
}

public partial class ProcessForm : Form {
    // Accessing the checked state of CheckBoxes
    private void Process() {
        if ((this.Parent as MainForm).FirstChecked)
            // Process according to first CheckBox.Checked state.
        else if ((this.Parent as MainForm).SecondChecked)
            // Process according to second CheckBox.Checked state.
        else if ((this.Parent as MainForm).ThirdChecked)
            // Process according to third CheckBox.Checked state.
    }
}
Run Code Online (Sandbox Code Playgroud)

请考虑一下,我在脑海中选择了这段代码,因此它可能无法编译。无论如何,我希望这能让您了解如何在整个表单中传递您的价值观。

Web 和WinForm 编程之间最大的区别是Web 是无状态的。SESSION 和 VIEWSTATE 是允许保留值的解决方法。

WinForms 是有状态的,因此您不需要遍历 SESSION 和 VIEWSTATE 之类的变量。只要对象存在,值就会保留。