Windows Phone 8 APP 移植到Windows Phone 8.1 常见问题总结

前言(一些废话哈,可以略过)

    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取代基本用法如下

       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;
            }

        }
    }

  最后这个类,东西比较多哈,有的还没来得及测试,那个小伙伴遇到问题也请告诉我下。耽误大家时间了。

时间: 2024-10-16 03:06:01

Windows Phone 8 APP 移植到Windows Phone 8.1 常见问题总结的相关文章

windows phone 8.0 app 移植到windows10 app 页面类

phone:PhoneApplicationPage    全部替换为Page phone:WebBrowser               全部替换为   WebView IsScriptEnabled="True"           空格 SupportedOrientations="Portrait"     空格 Orientation="Portrait"     空格 Orientation="Landscape"

LEDAPS1.3.0版本移植到windows平台----HuCsm云掩膜模块

这个是2012年左右放在百度空间的,谁知百度空间关闭...转移到博客园. 最近项目用到3.1.2版本的LEDAPS,新版本的使用情况会在后续文章中慢慢丰富. HuCsm是将LEDAPS项目中的TM/ETM+大气校正流程系列算法中的云掩膜模块由linux系统移植到windows下的产物,代码本身改动不大,使用接口不变. 包含文件: HuCsm.exe hd423m.dll hm423m.dll 编译程序需要包含的静态库有: gctp.lib hdfeos.lib hd423m.lib hm423m

Windows下将ImageMagick移植到Android平台

Windows下将ImageMagick移植到Android平台 原文链接  http://www.pedant.cn/2014/06/18/imagemagick-ported-android/ ImageMagick是一个用来创建.编辑.合成图片的软件.它可以读取.转换.写入多种格式的图片.在移动平台做一些较复杂的图像处理时,难免会请出这尊大神.官方网站上也说明了它可以运行在多个平台之上,包括Android.不过官方的Releases版本只有Unix.Mac OS X.IOS.Windows

将Linux代码移植到Windows的简单方法

将Linux代码移植到Windows的简单方法 一.前言 Linux拥有丰富各种源代码资源,但是大部分代码在Windows平台情况是无法正常编译的.Windows平台根本无法直接利用这些源代码资源.如果想要使用完整的代码,就要做移植工作.因为C/C++ Library的不同和其他的一些原因,移植C/C++代码是一项困难的工作.本文将以一个实际的例子(Tar)来说明如何把Linux代码移植到Windows平台上.移植过程将尽量少修改代码,以便代码的运行逻辑不会发生任何变动.保留绝大部分软件主要功能

Unity for Windows: II – Publishing Unity games to Windows Store

原地址:http://digitalerr0r.wordpress.com/2013/08/27/unity-for-windows-ii-publishing-to-windows-8/ Windows 8 is a new OS with a Windows Store where you can distribute your apps to millions of users world wide, for both PC, laptops and tablets. You can se

虚拟化基础架构Windows 2008篇之5-安装Windows部署服务

看完文章,请顺手投我一票(王春海),谢谢 http://edu.51cto.com/activityvote/voteRanking 京东6.18结束了,IT人自己的6.18来了!!!51CTO学院3周年(6月30日-7月1日)70000课程不止5折!http://edu.51cto.com/lecturer/user_id-225186.html 在组建云计算数据中心时,会经常安装操作系统.无论是云计算的基础平台Hyper-V Server 2008 R2.Hyper-V Server 201

虚拟化基础架构Windows 2008篇之6-启动Windows部署服务

看完文章,请顺手投我一票(王春海),谢谢 http://edu.51cto.com/activityvote/voteRanking 京东6.18结束了,IT人自己的6.18来了!!!51CTO学院3周年(6月30日-7月1日)70000课程不止5折!http://edu.51cto.com/lecturer/user_id-225186.html 2.4 启动Windows 部署服务 在"服务器管理器"窗口中(如果原来"服务器管理器"已经打开,请关闭并再次进入),

虚拟化基础架构Windows 2008篇之9-配置Windows部署服务

看完文章,请顺手投我一票(王春海),谢谢 http://edu.51cto.com/activityvote/voteRanking 京东6.18结束了,IT人自己的6.18来了!!!51CTO学院3周年(6月30日-7月1日)70000课程不止5折!http://edu.51cto.com/lecturer/user_id-225186.html 2.7 配置Windows部署服务 在添加完安装映像与启动映像后,右击服务器名dc.heinfo.local,从快捷菜单中选择"属性"命令

在 Windows 7 和 Windows Server 2008 R2 上安装 Windows PowerShell 3.0

在 Windows 7 和 Windows Server 2008 R2 上安装 Windows PowerShell 3.0 Windows 7 和 Windows Server 2008 R2 内核版本同为6.1,以下步骤说明如何在运行 Windows 7 SP1 和 Windows Server 2008 R2 SP1 的机器上安装 Windows PowerShell 3.0. 安装准备 1. 在安装Windows Management Framework 3.0之前,卸载任何Windo