概述
实现页面间的数据共享有很多方法,比如:①事件的发布订阅 ②委托
③ioc容器④静态类等,这节主要通过静态的集合实现数据的添加和获取。
实现效果如下:数据在登录页面写入字典,然后在conductor页面获取
代码解析
-. 数据写入:
public void SetData()
{
DataShareBase.SetData<string>(CommonConstant.OfficialAccount, "dotnet讲堂");
var loginViewModel = new LoginViewModel();
loginViewModel.UserInformation = new UserInformation()
{
UserName = "zls365",
Password = "abc123"
};
DataShareBase.SetData<LoginViewModel>(CommonConstant.LoginViewModelKey, loginViewModel);
}
-. 数据获取:
public void GetData()
{
string dynamicValue = DataShareBase.GetData<string>(CommonConstant.OfficialAccount);
DXMessageBox.Show(dynamicValue, CommonConstant.OfficialAccount, MessageBoxButton.OK, MessageBoxImage.Information);
LoginViewModel dynamicObject = DataShareBase.GetData<LoginViewModel>(CommonConstant.LoginViewModelKey);
StringBuilder stringBuild = new StringBuilder();
stringBuild.AppendLine(dynamicObject.UserInformation.UserName);
stringBuild.AppendLine(dynamicObject.UserInformation.Password);
DXMessageBox.Show(stringBuild.ToString(), CommonConstant.LoginViewModelKey, MessageBoxButton.OK, MessageBoxImage.Information);
}
写入的方法封装在类DataShareBase
using System;
using System.Collections.Concurrent;
namespace Caliburn.Micro.Hello.Helper
{
public class DataShareBase
{
private static readonly Common.Logging.ILog logger = Common.Logging.LogManager.GetLogger<DataShareBase>();
/// <summary>
/// Dynamic Data
/// </summary>
public static ConcurrentDictionary<string, object> DynamicData { get; set; } = new ConcurrentDictionary<string, object>();
public static void SetData(string key, object value, bool isForceWrite = true)
{
logger.Info($"[SetData] Enter. Key = [{key}], Value = [{value}], isForceWrite = [{isForceWrite}]");
if (DynamicData.Keys.Contains(key))
{
string logMsg = $"DynamicData key = {key} has existed in context.";
if (isForceWrite)
{
DynamicData[key] = value;
}
else
{
logger.Debug(logMsg);
}
}
try
{
DynamicData.TryAdd(key, value);
}
catch (Exception ex)
{
logger.Error($"[SetData] {ex}");
}
logger.Info($"[SetData] Enter. Key = [{key}], Value = [{value}], isForceWrite = [{isForceWrite}]");
}
public static void SetData<TData>(string key, TData value, bool isForceWrite = true)
{
SetData(key, (object)value, isForceWrite);
}
public static TData GetData<TData>(string key)
{
logger.Debug($"Enter [GetData]. Key = {key}.");
var data = GetData(key);
if (!(data is TData))
{
string logMsg = $"In [GetData<TData>] DynamicData type is not [{typeof(TData)}].";
logger.Error(logMsg);
throw new Exception(logMsg);
}
logger.Debug($"Enter [GetData]. Return {((TData)data).ToString()}.");
return (TData)data;
}
public static object GetData(string key)
{
if (!(DynamicData.Keys.Contains(key)))
{
string logMsg = $"In [GetData] DynamicData key = {key} not existed in context.";
logger.Error(logMsg);
throw new Exception(logMsg);
}
object data = null;
DynamicData.TryGetValue(key, out data);
return data;
}
}
}
ConcurrentDictionary是一个线程安全的字典,它的值是object类型,每次数据写入的时候会将数据转换为object,即装箱,每次取的时候会将object类型拆箱为对应的类型。
源码下载
链接:https://pan.baidu.com/s/1kE_plpDD6-CzkjEsmtigzA
提取码:6666