前言(一些廢話哈,能夠略過)app
wp8.1的SDK相對8.0的仍是變化很多的,類改變了不少,我就是我遇到的一些問題總結下,你們有用到的能夠少走些彎路,一些東西國內都搜不到async
(作這我的少,資料少,開始移植到wp8.1的也少些),建議小夥伴們google,或者關鍵字用英文搜索比較好,其實我大多都在stackoverflow找到的,ide
看官方文檔也能夠,可是就比較慢了。測試
正文ui
MessageBox被MessageDialog取代了具體用法以下(有些變量名是隨手寫的,沒通過思考。。。你們能夠改下)google
public async static void ShowCustomDialog(string title, string mes, string ok, Action ok_callback, string cancel, Action cancel_callback) { MessageDialog c = new MessageDialog(mes,title); UICommand ok_cmd = new UICommand(ok); ok_cmd.Invoked += (dd) => { if (ok_callback != null) { ok_callback.Invoke(); } }; UICommand canle_cmd = new UICommand(cancel); ok_cmd.Invoked += (dd) => { if (cancel_callback != null) { cancel_callback.Invoke(); }; }; c.Commands.Add(ok_cmd); c.Commands.Add(canle_cmd); IUICommand result = await c.ShowAsync(); }
獲取設備基本信息的類DeviceStatus被EasClientDeviceInformation取代基本用法以下spa
public static string GetDeviceInfo() { Windows.Security.ExchangeActiveSyncProvisioning.EasClientDeviceInformation deviceInfo = new Windows.Security.ExchangeActiveSyncProvisioning.EasClientDeviceInformation(); var firmwareVersion = deviceInfo.SystemFirmwareVersion; string d = "{\"devicename\":\"" + deviceInfo.SystemProductName + "\",\"deviceManufacturer\":\"" + deviceInfo.SystemManufacturer + "\"}"; return d; }
獲取設備惟一ID的類DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out uniqueId))被HardwareIdentification取代,具體使用以下線程
public static string GetDeviceUniqueID() { HardwareToken token = HardwareIdentification.GetPackageSpecificToken(null); IBuffer hardwareId = token.Id; HashAlgorithmProvider hasher = HashAlgorithmProvider.OpenAlgorithm("MD5"); IBuffer hashed = hasher.HashData(hardwareId); string hashedString = CryptographicBuffer.EncodeToHexString(hashed); return hashedString; }
在後臺進程中執行UI進程任務時候用到的Deployment.Current.Dispatcher.BeginInvoke 被CoreWindow.GetForCurrentThread().Dispatcher取代,具體用法code
var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{ // UI code goes hereorm
});
//or CoreApplication.MainWindow.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, ()=>{ // UI code goes here });
當前線程睡眠的Thread.Sleep()
被 await Task.Delay(1000)取代
IsolatedStorageSettings被ApplicationData.Current.RoamingSettings取代,IsolatedStorageFile被ApplicationData.Current.LocalFolder;取代,具體使用以下
public class IsolatedStorageHelper { private static ApplicationDataContainer _appSettings= ApplicationData.Current.RoamingSettings; public static T LoadSetttingFromStorage<T>(string Key, T defaultValue) { T ObjToLoad = default(T); if (_appSettings.Values.Keys.Contains(Key)) { ObjToLoad = (T)_appSettings.Values[Key]; } else { ObjToLoad = defaultValue; } return ObjToLoad; } public static void SaveSettingToStorage(string Key, object Setting) { if (!_appSettings.Values.Keys.Contains(Key)) { _appSettings.Values.Add(Key, Setting); } else { _appSettings.Values[Key] = Setting; } } public static bool IsSettingPersisted(string Key) { return _appSettings.Values.Keys.Contains(Key); } public static bool DeleteSettingPersisted(string Key) { while (_appSettings.Values.Keys.Contains(Key)) { _appSettings.Values.Remove(Key); } return false; } public static async Task<bool> AppendStrToFolder(string str) { str = "時間:" + DateTime.Now.ToString() + str; // 獲取應用程序數據存儲文件夾 StorageFolder applicationFolder = ApplicationData.Current.LocalFolder; // 在指定的應用程序數據存儲文件夾內建立指定的文件 StorageFile storageFile = await applicationFolder.CreateFileAsync("LuaPostJsonStr.txt", CreationCollisionOption.OpenIfExists); // 將指定的文本內容寫入到指定的文件 using (Stream stream = await storageFile.OpenStreamForWriteAsync()) { byte[] content = Encoding.UTF8.GetBytes(str); await stream.WriteAsync(content, 0, content.Length); } return true; } public static async Task<bool> WriteFile(string filename,string content) { content = "時間:" + DateTime.Now.ToString() + "--------" + content; IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder; IStorageFile storageFile = await applicationFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists); try { using (Stream transaction = await storageFile.OpenStreamForWriteAsync()) { byte[] contents = Encoding.UTF8.GetBytes(content); //設置如何打開流的位置有內容就設置流的位置到結束位置 if (transaction.CanSeek) { transaction.Seek(transaction.Length, SeekOrigin.Begin); } await transaction.WriteAsync(contents,0, contents.Length); return true; } } catch (Exception exce) { return false; // OutputTextBlock.Text = "異常:" + exce.Message; } } public static async Task SavePic(UIElement root, string filename) { RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(); await renderTargetBitmap.RenderAsync(root); var pixelBuffer = await renderTargetBitmap.GetPixelsAsync(); string path = "picSdk"; StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFolder store = await storageFolder.CreateFolderAsync(path, CreationCollisionOption.OpenIfExists); string ss = Path.Combine(path, filename); StorageFile file = await store.CreateFileAsync(ss); using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream); encoder.SetPixelData( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight, DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, pixelBuffer.ToArray()); await encoder.FlushAsync(); } } public static async Task<bool> SaveFile(StorageFile filee) { string path = "picSdk"; StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFolder store = await storageFolder.CreateFolderAsync(path, CreationCollisionOption.OpenIfExists); string ss = Path.Combine(path, filee.Name); StorageFile file = await store.CreateFileAsync(ss); await filee.MoveAndReplaceAsync(file); return true; } public static async Task< BitmapImage> LoadPic(string folderName,string fileName) { BitmapImage source = new BitmapImage(); // Reopen and load the PNG file. if (await IsExistFolder(folderName)) { var folder = ApplicationData.Current.LocalFolder; if (await IsExistFile(fileName,folderName)) { StorageFile file = await folder.GetFileAsync(fileName); using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapDecoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream); var stream = await encoder.GetPreviewAsync(); source.SetSource(stream); source.CreateOptions = BitmapCreateOptions.None; } } return source; } else { return null; } } public static async Task<bool> IsExistFile(string fileName) { StorageFolder storageFolder = ApplicationData.Current.LocalFolder; try { StorageFile file = await storageFolder.GetFileAsync(fileName); return true; } catch (FileNotFoundException e) { return false; } catch (Exception e) { return false; } } public static async Task<bool> IsExistFile(string fileName,string folderName) { StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFolder folder= await storageFolder.GetFolderAsync(folderName); try { StorageFile file = await folder.GetFileAsync(fileName); return true; } catch (FileNotFoundException e) { return false; } catch (Exception e) { return false; } } public static async Task<bool> IsExistFolder(string folderName) { StorageFolder storageFolder = ApplicationData.Current.LocalFolder; try { StorageFolder file = await storageFolder.GetFolderAsync(folderName); return true; } catch (FileNotFoundException e) { return false; } catch (Exception e) { return false; } } public static async Task< Stream> LoadPicByStream(string filename) { var store = ApplicationData.Current.LocalFolder; if ( await IsExistFile(filename)) { StorageFile file= await store.GetFileAsync(filename); // fileStream.Close(); return (await file.OpenAsync(FileAccessMode.ReadWrite)).AsStream(); } else { return null; } } }
最後這個類,東西比較多哈,有的還沒來得及測試,那個小夥伴遇到問題也請告訴我下。耽誤你們時間了。