设为首页 收藏本站
查看: 893|回复: 0

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

[复制链接]

尚未签到

发表于 2015-5-23 08:48:16 | 显示全部楼层 |阅读模式
  前言(一些废话哈,可以略过)
    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(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 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 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 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 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 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 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;
}
}
}

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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-69696-1-1.html 上篇帖子: Windows Phone开发之路(8) Silverlight三大布局容器 下篇帖子: 《微软官方Windows 8设计指南》归纳整理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表