Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

c# - Session in WPF?

In ASP.NET, I can do Session["something"] = something;, and then I can retrieve in another page the value of the session. Is there a Session in WPF that would allow me to do the same in ASP.NET? I notice there is no session in WPF because there is state. Because I got many user control pages, I need to get its values and display it on the MainWindow.

Is there anything similar to Session in WPF?

Some resources say to use cookies. How do I do that? mine Is a WPF application not a WPF web application?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I understand your question correctly, you just need some kind of global storage for certain values in your program.

You can create a static class with public static properties for the various values that you need to store and be able to globally access inside your application. Something like:

public static class Global
{
    private string s_sSomeProperty;

    static Globals ()
    {
        s_sSomeProperty = ...;
        ...
    }

    public static string SomeProperty
    {
        get
        {
            return ( s_sSomeProperty );
        }
        set
        {
            s_sSomeProperty = value;
        }
    }

    ...
}

This way you can just write Global.SomeProperty anywhere in your code where the Global class is available.

Of course, you'd need validation and - if your application is multithreaded - proper locking to make sure your global (shared) data is protected across threads.

This solution is better than using something like session because your properties will be strongly typed and there's no string-based lookup for the property value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...