前言(一些废话哈,可以略过)
wp8.1的SDK相对8.0的还是变化不少的,类改变了很多,我就是我遇到的一些问题总结下,大家有用到的可以少走些弯路,一些东西国内都搜不到
(做这个人少,资料少,开始移植到wp8.1的也少些),建议小伙伴们google,或者关键字用英文搜索比较好,其实我大多都在stackoverflow找到的,
看官方文档也可以,但是就比较慢了。
正文
MessageBox被MessageDialog取代了具体用法如下(有些变量名是随手写的,没经过思考。。。大家可以改下)
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取代基本用法如下
class="brush:csharp;gutter:true;"> 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取代,具体用法
var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{ // UI code goes here
});
//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; } } }
最后这个类,东西比较多哈,有的还没来得及测试,那个小伙伴遇到问题也请告诉我下。耽误大家时间了。