构思:
在首页(MainPage)上定义一个代表应用版本号的字符串,网站上写一个接口(让这个字符串与你的最新应用版本号进行比较,返回应用的ContentIdentifier与操作状态什么的)。一、wp客户端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using Microsoft.Phone.Tasks;
using System.IO.IsolatedStorage;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;
using System.Text;
using System.Windows.Media.Imaging;
namespace Client{
public partial class MainPage : PhoneApplicationPage {
//应用信息静态类包含版本号,应用名等等 public static App.GlableInfo info = new App.GlableInfo();
string localVer = info.WinPhoneVersion;
//"Microsoft_Winphone_V1.0.0" //升级接口 string uploadUrl = "http://www.xxx.com/Upgrade.aspx?ver={
0}
";
public MainPage() {
InitializeComponent();
}
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) {
base.OnBackKeyPress(e);
MessageBoxResult mbr = MessageBox.Show("是否确认要退出" + info.AppName + "?", "提示", MessageBoxButton.OKCancel);
if (mbr == MessageBoxResult.Cancel) {
e.Cancel = true;
}
else {
//下次进入应用继续提示升级 SQLiteHelper.openDB();
SQLiteHelper.CreateVersionTable();
VersionInfo infoVersion = SQLiteHelper.seleceVersion();
if (infoVersion != null)//判断VersionInfo中是否为空 {
infoVersion.IsUpdate = 0;
SQLiteHelper.UpdateVersionData(infoVersion);
}
SQLiteHelper.closeDB();
App.Quit();
}
}
////// 判断是否为第一次启动应用程序,进入引导页(4张图片)和 应用升级 /// /// protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {
SQLiteHelper.openDB();
SQLiteHelper.CreateVersionTable();
VersionInfo infoVersion = SQLiteHelper.seleceVersion();
#region 第一次启动应用程序 if (infoVersion != null)//判断VersionInfo中是否为空 {
//将该应用版本与VersionInfo表中的数据相比较,应用升级时也进入引导页 if (infoVersion.version != info.WinPhoneVersion) {
//将版本号存入 infoVersion.version = info.WinPhoneVersion;
SQLiteHelper.insertVersionData(infoVersion);
//如需加入引导功能,底下一行取消注释 //NavigationService.Navigate(new Uri("/firstIn.xaml", UriKind.Relative));
}
else {
#region 应用升级 若是第一次启动应用程序 需要升级 则将在下一次加载首页面时提示升级 //提示是否升级,有网 且 进入应用程序后第一次提示升级 if (Utility.GetNetStates() != "none" && infoVersion.IsUpdate == 0) {
uploadUrl = string.Format(uploadUrl, localVer);
WebRequest request = HttpWebRequest.Create(uploadUrl);
IAsyncResult result = (IAsyncResult)request.BeginGetResponse(ResponseCallBack, request);
}
#endregion }
}
else {
infoVersion = new VersionInfo();
infoVersion.version = info.WinPhoneVersion;
//将版本号存入 SQLiteHelper.insertVersionData(infoVersion);
//如需加入引导功能,底下一行取消注释 //NavigationService.Navigate(new Uri("/firstIn.xaml", UriKind.Relative));
}
#endregion SQLiteHelper.closeDB();
}
////// 调用接口 /// /// private void ResponseCallBack(IAsyncResult result) {
try {
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
WebResponse response = request.EndGetResponse(result);
using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) {
if (reader != null) {
string s = reader.ReadToEnd();
Dispatcher.BeginInvoke(() => {
if (s != null && s != "") {
if (s == "0") {
//最新版本 }
else {
string[] retArray = s.Split('|');
if (retArray.Length == 2) {
string keyVer = retArray[0];
string urlVer = retArray[1];
//应用商店中应用的ContentIdentifier,如微信的http://www.windowsphone.com/zh-cn/store/app/微信/23e1505b-9383-4ed4-9195-da23a3442820,其ContentIdentifier就为23e1505b-9383-4ed4-9195-da23a3442820 if (keyVer == "1")//选择 {
MessageBoxResult mbr = MessageBox.Show("软件有新版本了,是否要升级?", info.AppName, MessageBoxButton.OKCancel);
if (mbr == MessageBoxResult.OK) {
//直接调用商店 MarketplaceDetailTask mp = new MarketplaceDetailTask();
mp.ContentIdentifier = urlVer;
mp.Show();
}
else if (mbr == MessageBoxResult.Cancel) {
//记住此次启动应用后用户的选择即不升级 SQLiteHelper.openDB();
SQLiteHelper.CreateVersionTable();
VersionInfo infoVersion = SQLiteHelper.seleceVersion();
if (infoVersion != null)//判断VersionInfo中是否为空 {
infoVersion.IsUpdate = 1;
SQLiteHelper.UpdateVersionData(infoVersion);
}
else {
infoVersion.IsUpdate = 1;
SQLiteHelper.insertVersionData(infoVersion);
}
SQLiteHelper.closeDB();
}
}
else if (keyVer == "2")//强制 {
MessageBoxResult mbr = MessageBox.Show("软件有新版本了,请先升级!", info.AppName, MessageBoxButton.OK);
if (mbr == MessageBoxResult.OK) {
MarketplaceDetailTask mp = new MarketplaceDetailTask();
mp.ContentIdentifier = urlVer;
mp.Show();
}
}
}
}
}
}
);
}
}
}
catch (Exception e) {
//MessageBox.Show(e.Message);
}
}
}
}
二、服务端
Upgrade.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using phone.BizComponents;
using System.Data;
namespace Server{
public partial class Upgrade : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
UpgradeBiz biz=new UpgradeBiz();
string result = " ";
string strInfo="";
string ver = Request.QueryString["ver"];
if (ver != null && ver != "") {
string[] verArr = ver.Split('_');
//"Microsoft_Winphone_V1.0.0" if (verArr.Length==3) {
string org = verArr[0];
string cate = verArr[1];
string version=verArr[2];
//通过机构名,类型,版本号与数据库进行比较,判断升级 //SELECT top 1 Id,ORG,Category,[Version],Must,Url,AddTime FROM M_T_PHONE_Upgrade WHERE ORG=@ORG and Category=@Category and [Version]>@Version order by AddTime desc,Id desc;
DataTable table = biz.GetNewVersion(org, cate, version);
if (table.Rows.Count>0) {
string isMust =Convert.ToString(table.Rows[0]["Must"]);
string url = Convert.ToString(table.Rows[0]["url"]);
string strNewVer = string.Format("{
0}
_{
1}
_{
2}
", table.Rows[0]["ORG"], table.Rows[0]["Category"], table.Rows[0]["Version"]);
result = string.Format("{
0}
|{
1}
",isMust,url);
string strMust=isMust=="1"?"选择升级":"强制升级";
strInfo = string.Format("【{
0}
|{
1}
到{
2}
】|{
3}
", strMust,ver,strNewVer, url);
}
else {
result = "0";
}
}
}
Response.Write(result);
}
}
}
Upgrade.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Upgrade.aspx.cs" Inherits="phone.Upgrade" %>