/// <summary> /// 加载UserProfile的数据 /// 如果独立存储中不存在数据,将从数据库同步数据到独立存储; /// </summary> public void LoadProfileData(Action <object> callback) { var sync_keys = IsolatedStoreageHelper.Read(BuildKey(SYNC_KEYS)) as List <string>; if (sync_keys != null && sync_keys.Count > 0) { m_changedFlags[BuildKey(SYNC_KEYS)] = true; LoadDataByKeys(sync_keys, callback); } else { m_changedFlags[BuildKey(SYNC_KEYS)] = false; m_serviceClient.QueryAsync(new ProfileQueryV40 { Header = new MessageHeader(), Body = new ProfileQueryMsg { ApplicationId = CPApplication.Current.Application.Id, InUser = CPApplication.Current.LoginUser.ID, ProfileType = SYNC_KEYS } }, new LoadProfileType { CallBack = callback, IsLoadProfile = false }); } }
/// <summary> /// 通过同步的Key获取对应的Profile Data数据到独立存储 /// </summary> private void LoadDataByKeys(List <string> keys, Action <object> callback) { var count = keys.Count(key => IsolatedStoreageHelper.Read(BuildKey(key)) == null); if (count == keys.Count) { m_serviceClient.QueryAsync(new ProfileQueryV40 { Header = new MessageHeader(), Body = new ProfileQueryMsg { ApplicationId = CPApplication.Current.Application.Id, InUser = CPApplication.Current.LoginUser.ID, } }, new LoadProfileType { CallBack = callback, IsLoadProfile = true }); foreach (var key in keys) { m_changedFlags[BuildKey(key)] = false; } } else { foreach (var key in keys) { m_changedFlags[BuildKey(key)] = true; } callback(null); } }
/// <summary> /// 同步独立存储的数据到数据库 /// 由固定的Timer调用 /// </summary> public void SyncData() { var userProfiles = new ObservableCollection <UserProfile>(); var keys = IsolatedStoreageHelper.Read(BuildKey(SYNC_KEYS)) as List <string>; if (keys != null) { #region 步Keys的数据 if (m_changedFlags.ContainsKey(BuildKey(SYNC_KEYS)) && m_changedFlags[BuildKey(SYNC_KEYS)]) { userProfiles.Add(new UserProfile { ApplicationId = CPApplication.Current.Application.Id, InUser = CPApplication.Current.LoginUser.LoginName, Key = SYNC_KEYS, Data = UtilityHelper.XmlSerialize(keys) }); m_changedFlags[BuildKey(SYNC_KEYS)] = false; } #endregion #region 步UserProfile的数据 foreach (var key in keys) { var data = IsolatedStoreageHelper.Read(BuildKey(key)); if (data != null && m_changedFlags.ContainsKey(BuildKey(key)) && m_changedFlags[BuildKey(key)]) { userProfiles.Add(new UserProfile { ApplicationId = CPApplication.Current.Application.Id, InUser = CPApplication.Current.LoginUser.LoginName, Key = key, Data = data.ToString() }); m_changedFlags[BuildKey(key)] = false; } } #endregion if (userProfiles.Count > 0) { m_serviceClient.SaveAsync(new SimpleTypeDataContractOfArrayOfUserProfilep9OHFywk { Header = new MessageHeader(), Value = userProfiles }); } } }
/// <summary> /// Persists this instance to isolate storage. /// </summary> private static void Persist() { while (true) { Thread.Sleep(PersistInterval); lock (syncObject) { IsolatedStoreageHelper.Write(persistFile, Container); } } }
/// <summary> /// Inits this instance from isolate storage. /// </summary> private static void Init() { try { Container = IsolatedStoreageHelper.Read(persistFile) as Dictionary <string, CacheItem>; } finally { if (Container == null) { Container = new Dictionary <string, CacheItem>(); } } }
/// <summary> /// 通过Key获取独立存储中的数据 /// </summary> /// <typeparam name="T">数据对象的类型</typeparam> /// <param name="key">唯一Key</param> /// <returns></returns> public T Get <T>(string key) { var value = IsolatedStoreageHelper.Read(BuildKey(key)); try { return(value != null?UtilityHelper.XmlDeserialize <T>(value.ToString()) : default(T)); } catch { //当反序列化失败的时候,需要将相关key值,设置成null. IsolatedStoreageHelper.Write(BuildKey(key), null); return(default(T)); } }
private void Application_Startup(object sender, StartupEventArgs e) { Dictionary <string, string> dic; if (Application.Current.IsRunningOutOfBrowser) { Application.Current.MainWindow.WindowState = WindowState.Maximized; dic = (Dictionary <string, string>)IsolatedStoreageHelper.Read("InitParams.bin"); } else { dic = new Dictionary <string, string>(e.InitParams.Count * 2); foreach (var entry in e.InitParams) { if (!dic.ContainsKey(entry.Key)) { dic.Add(entry.Key, entry.Value); } } IsolatedStoreageHelper.Write("InitParams.bin", dic); } // 将sliverlight的default.aspx页面的<param name="initParams" value="...." />的数据放入CPApplication.Current // 作为配置数据,供应用程序其他地方使用 if (dic != null && dic.Count > 0) { CPApplication.Current.InitParams = new ReadOnlyDirectionary <string, string>(dic); } else { CPApplication.Current.InitParams = ReadOnlyDirectionary <string, string> .Empty; } string defatulCulture = string.Empty; if (CPApplication.Current.InitParams["defatulCulture"] != null) { defatulCulture = CPApplication.Current.InitParams["defatulCulture"]; } InitLanguageAndTheme(defatulCulture); if (CPApplication.Current.CommonData == null) { CPApplication.Current.CommonData = new Dictionary <string, object>(); } CPApplication.Current.CommonData.Add("ECCentralServiceURL_Login", CPApplication.Current.InitParams["login"]); this.RootVisual = new RootVisualWrapper(); }
/// <summary> /// 保存数据到独立存储 /// </summary> /// <param name="key">在独立存储中保存或获取的唯一Key</param> /// <param name="data">数据对象</param> /// <param name="needSync">是否需要同步数据到数据库</param> public void Set(string key, object data, bool needSync) { var keys = IsolatedStoreageHelper.Read(BuildKey(SYNC_KEYS)) as List <string>; var b = false; if (keys == null) { keys = new List <string>(); } if (needSync) { if (!keys.Contains(key)) { keys.Add(key); m_changedFlags[BuildKey(SYNC_KEYS)] = true; b = true; } m_changedFlags[BuildKey(key)] = true; } else { if (keys.Contains(key)) { keys.Remove(key); m_changedFlags[BuildKey(SYNC_KEYS)] = true; b = true; } } if (b) { IsolatedStoreageHelper.Write(BuildKey(SYNC_KEYS), keys); } IsolatedStoreageHelper.Write(BuildKey(key), UtilityHelper.XmlSerialize(data)); }
void m_serviceClient_QueryCompleted(object sender, QueryCompletedEventArgs e) { if (e.Error != null) { Deployment.Current.Dispatcher.BeginInvoke(() => { CPApplication.Current.Browser.Logger.LogError(e.Error); }); return; } if (e.Result.Faults != null && e.Result.Faults.Count > 0) { var errorMsg = e.Result.Faults[0].ErrorDetail; Deployment.Current.Dispatcher.BeginInvoke(() => { CPApplication.Current.Browser.Logger.WriteLog(errorMsg, "ExceptionLog"); }); return; } //True:加载ProfileData的数据; //False:加载同步Key的数据 var loadProfileType = e.UserState as LoadProfileType; if (loadProfileType != null) { if (loadProfileType.IsLoadProfile) { var result = e.Result.Value; var key = result.ToList().SingleOrDefault(p => p.Key == SYNC_KEYS); if (key != null) { result.Remove(key); } foreach (var userProfile in result) { IsolatedStoreageHelper.Write(BuildKey(userProfile.Key), userProfile.Data); } if (loadProfileType.CallBack != null) { loadProfileType.CallBack(null); } } else { if (e.Result.Value != null && e.Result.Value.Count == 1) { var keys = UtilityHelper.XmlDeserialize <List <string> >(e.Result.Value[0].Data); IsolatedStoreageHelper.Write(BuildKey(SYNC_KEYS), keys); if (keys != null) { LoadDataByKeys(keys, loadProfileType.CallBack); } } else { if (loadProfileType.CallBack != null) { loadProfileType.CallBack(null); } } } } }