wpf WPF中的会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17779355/
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
Session in WPF?
提问by what
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.
在 ASP.NET 中,我可以这样做Session["something"] = something;,然后我可以在另一个页面中检索会话的值。WPF 中是否有一个会话可以让我在 ASP.NET 中做同样的事情?我注意到 WPF 中没有会话,因为有状态。因为我有很多用户控制页面,所以我需要获取它的值并将其显示在 MainWindow 上。
Is there anything similar to Session in WPF?
WPF中是否有类似于Session的东西?
Some resources say to use cookies. How do I do that? mine Is a WPF application not a WPF web application?
一些资源说要使用 cookie。我怎么做?我的 WPF 应用程序不是 WPF Web 应用程序吗?
回答by xxbbcc
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.SomePropertyanywhere in your code where the Globalclass is available.
通过这种方式,您可以Global.SomeProperty在代码中Global可用的任何位置编写代码。
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.
此解决方案比使用 session 之类的方法更好,因为您的属性将是强类型的,并且没有基于字符串的属性值查找。
回答by vivat pisces
One possibility is to use:
一种可能性是使用:
Application.Current.Resources["something"] = something;
I wouldn't get into the habit of using that too frequently though. I think it's generally for data that's being stored once (such as styles) and then just referenced from other points in the application. Everything in your app reading/writing to some globally shared piece of data is poor practice and could make debugging tough.
不过我不会养成频繁使用它的习惯。我认为它通常用于存储一次(例如样式)然后从应用程序中的其他点引用的数据。应用程序中读取/写入某些全局共享数据的所有内容都是糟糕的做法,可能会使调试变得困难。
回答by G.Dealmeida
In my opinion, the best way to do it will be to store it in the current properties of your application like this :
在我看来,最好的方法是将它存储在应用程序的当前属性中,如下所示:
Application.Current.Properties["stuff"]
You can store any kind of object, but don't abuse it.
您可以存储任何类型的对象,但不要滥用它。
I will personally use this kind of tricks only if I have no other way to share data (same advice than Grant Winney)
只有当我没有其他方式共享数据时,我才会亲自使用这种技巧(与 Grant Winney 的建议相同)
I prefer using Resources for translation or images and properties for other flag/data just to separate things a little
我更喜欢使用资源进行翻译或图像和其他标志/数据的属性只是为了将事物分开
回答by Imtiyaz Uddin
Try the following:
请尝试以下操作:
System.Windows.Application.Current.Properties["Something"] = Something;
I Used this many times in my project,
我在我的项目中多次使用这个,

