Windows 应用程序中是否有类似 SESSION 的东西?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2537482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:12:02  来源:igfitidea点击:

Is there something like SESSION in Windows application?

c#windows

提问by Manish

Is there something like SESSION in Windows application? I want to store a few values to be persistent between forms.

Windows 应用程序中是否有类似 SESSION 的东西?我想存储一些值以在表单之间保持不变。

For example: First form has some check boxes and third form process them accordingly. So I need to store the checked checkboxes somewhere.

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

采纳答案by Will Marcouiller

You could only expose your CheckBoxes Checked state through properties of this form where you put your CheckBoxes on, and access these properties from your third or Process form.

您只能通过您放置 CheckBox 的此表单的属性公开您的 CheckBoxes Checked 状态,并从您的第三个或 Process 表单访问这些属性。

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.
    }
}

Please consider that I picked this code up the top of my head, so it might happen not to compile. Anyway, I hope that this gives you an idea of how to pass your values throughout your forms.

请考虑到我是在头脑中挑选了这段代码,所以它可能无法编译。无论如何,我希望这能让您了解如何在整个表单中传递您的价值观。

The biggest difference between Web and WinForm programming is that Web is stateless. SESSION and VIEWSTATE are workarounds to allow one to preserve values.

Web 和 WinForm 编程的最大区别在于 Web 是无状态的。SESSION 和 VIEWSTATE 是允许保留值的变通方法。

WinForms are stateful, so you don't need to go through SESSION and VIEWSTATE-like variables. A value is preserved as long as the object exists.

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

回答by codeulike

If you're talking about different Forms within the same Application, then just create some static members on a class, it will be persisted for the lifetime of the executable.

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

回答by 0x49D1

You can use app.config (or Settings section in Project's Properties) if you use Visual Studio, or just serialize your values and store them in some file.

如果您使用 Visual Studio,您可以使用 app.config(或项目属性中的设置部分),或者只是序列化您的值并将它们存储在某个文件中。

回答by Aiden Bell

If you want to persist data between independent execution of the same app (as in concurrent request serving in a HTTP farm) then just write out some XML or use a mashalling/serializing system with your runtime/plaform (dunno what it would be for C#).

如果您想在同一应用程序的独立执行之间保留数据(如在 HTTP 场中的并发请求服务),那么只需写出一些 XML 或在您的运行时/平台中使用混编/序列化系统(不知道 C# 会是什么) )。

Then import it again. Just watch your concurrency control.

然后再次导入。只需注意您的并发控制。

回答by Jeff Sternal

If this is just a regular single-user windows application, create a class to model the state you want to pass around and require it in your form constructors:

如果这只是一个普通的单用户 Windows 应用程序,请创建一个类来对要传递的状态进行建模,并在表单构造函数中要求它:

internal class ApplicationState 
{
    // Store the selected checkbox values here, for example
    public List<int> SelectedProductIds { get; }
    // ... additional state ...
}

internal class EditOrderForm: Form
{
    private ApplicationState applicationState;
    public EditCustomerForm(ApplicationState applicationState) {
        this.applicationState = applicationState;
    }
    // Rest of the code
}

You coulduse static variables instead of instances - but those are just global variables that make your code harder to read and maintain.

可以使用静态变量而不是实例 - 但这些只是使您的代码更难阅读和维护的全局变量。

回答by cdkMoose

If you are looking to store data on a per user basis between execution sessions, you should consider Isolated Storage.

如果您希望在执行会话之间按用户存储数据,则应考虑隔离存储。

  • Doesn't clutter install directory
  • Doesn't cause issues with AnitVirus software
  • Part of the OS including .Net objects, don't need to install anything else
  • Already works with the Windows security model
  • Exists on a per user basis, so saved settings are separated for each user
  • Can serialize/deserialize obects directly into it
  • 不会弄乱安装目录
  • 不会导致 AnitVirus 软件出现问题
  • 操作系统的一部分,包括 .Net 对象,不需要安装其他任何东西
  • 已经适用于 Windows 安全模型
  • 存在于每个用户的基础上,因此保存的设置为每个用户分开
  • 可以将对象直接序列化/反序列化到其中