WPF में, जहाँ मैं एक मूल्य जब एक UserControl में फिर बाद में एक और UserControl पहुँच कि फिर से मूल्य में सत्र स्थिति की तरह कुछ वेब प्रोग्रामिंग में बचा सकता है,,, जैसे:मैं WPF में वैश्विक एप्लिकेशन चर कैसे सहेज सकता हूं?
UserControl1.xaml.cs :
Customer customer = new Customer(12334);
ApplicationState.SetValue("currentCustomer", customer); //PSEUDO-CODE
UserControl2.xaml.cs:
Customer customer = ApplicationState.GetValue("currentCustomer") as Customer; //PSEUDO-CODE
उत्तर:
धन्यवाद, बॉब, यहाँ तुम्हारा पर आधारित है, कोड है कि मैं काम करने के लिए मिल गया है:
ApplicationState.SetValue("currentCustomerName", "Jim Smith");
एक चर पढ़ने के लिए:
public static class ApplicationState
{
private static Dictionary<string, object> _values =
new Dictionary<string, object>();
public static void SetValue(string key, object value)
{
if (_values.ContainsKey(key))
{
_values.Remove(key);
}
_values.Add(key, value);
}
public static T GetValue<T>(string key)
{
if (_values.ContainsKey(key))
{
return (T)_values[key];
}
else
{
return default(T);
}
}
}
एक चर को बचाने के लिए :
MainText.Text = ApplicationState.GetValue<string>("currentCustomerName");
लगता आप समझ में नहीं आया कि मैं क्या स्थिर वर्ग का मतलब ... लगता है कि मैं और अधिक अगली बार विस्तृत करना होगा। – CSharpAtl
शब्दकोश थ्रेड-सुरक्षित नहीं है, यदि आप एकाधिक थ्रेड से एप्लिकेशनस्टेट तक पहुंचने की योजना बनाते हैं तो यह एक व्यवहार्य समाधान नहीं होगा। –
@ जे। मिशेल वह ConcurrentDictionary –