WPFでは、あるUserControlで値を保存し、後で別のUserControlでその値に再度アクセスできます.Webプログラミングのセッション状態のようなものです。
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
答え:
ありがとう、ボブ、あなたのコードに基づいて、私が作業したコードは次のとおりです。
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);
}
}
}
変数を保存するには:
ApplicationState.SetValue("currentCustomerName", "Jim Smith");
変数を読み取るには:
MainText.Text = ApplicationState.GetValue<string>("currentCustomerName");