会议WPF? [英] Session in WPF?

查看:132
本文介绍了会议WPF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET中,我所能做的会话[东西] =东西; ,然后我可以在另一个页面检索会话的价值。在WPF中一个会话,让我做同样的ASP.NET?我注意到有WPF中没有会话,因为有状态。因为我也有很多的用户控件的页面,我需要得到它的值,并在主窗口显示出来。



有没有类似于会话东西在WPF?



某些资源说使用Cookie。我怎么做?煤矿是一个WPF应用程序不是一个WPF Web应用程序?


解决方案

如果我正确地理解你的问题,你只需要某种形式的全球存储在程序特定的值。



您可以创建你需要存储并能在全球范围内获得的各种值的公共静态属性静态类的应用。是这样的:

 公共静态类全球
{
私人字符串s_sSomeProperty;

静态全局()
{
s_sSomeProperty = ...;

}

公共静态字符串SomeProperty
{
得到
{
回报(s_sSomeProperty);
}

{
s_sSomeProperty =价值;
}
}


}

这样你就可以只写其中全球类可用 Global.SomeProperty 在你的代码的任何地方



当然,你需要验证和 - 如果你的多线程应用程序 - 适当的锁,以确保您的全局(共享)数据跨线程保护

这解决方案比使用类似的会议,因为你的属性将被强类型,并有该属性值没有基于字符串的查找更好。


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?

解决方案

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.

这篇关于会议WPF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆