windows phone开发学习

换了一家公司以后,做的是windows phone的开发,好在是用C#,所以上手不是很难,做了一个礼拜,觉得还是有点意思的。这一周主要学习了windows phone上面的磁贴技术。

关于磁贴其实MSDN上面写的很详细,具体可以参看这里:(v=vs.92)

对于我这个程序,要做的主要有以下几点工作:

1、实现磁贴正反翻转

2、实现磁贴用户手动更新

3、实现磁贴在后台任务作用下自动更新

其实第一个正反翻转很简单,你给磁贴设置了前景和背景,各种内容,磁贴自己就会实现翻转。所以主要说说后面两点。

实现磁贴用户手动更新:

看了MSDN后会发现,磁贴分为两种,一种是系统默认的只有一个,一种是用户自定义的,可以有多个。先来说说修改系统磁贴,代码如下:

// 要用的图片的相对路径private const string FORE_PIC = "/Images/Background.png";private const string BACK_PIC = "/Images/1.png";public static void Create(string title, string count, string backtitle, string backcontent){ShellTile TileToFind = ShellTile.ActiveTiles.First();int Counter = 0;StandardTileData data = new StandardTileData();if (!string.IsNullOrEmpty(title)){data.Title = title;}if (int.TryParse(count, out Counter)){data.Count = Counter;}if (!string.IsNullOrEmpty(backtitle)){data.BackTitle = backtitle;}if (!string.IsNullOrEmpty(backcontent)){data.BackContent = backcontent;}data.BackgroundImage = new Uri(FORE_PIC, UriKind.Relative);data.BackBackgroundImage = new Uri(BACK_PIC, UriKind.Relative);TileToFind.Update(data);

以上函数就能修改系统磁贴,无论该磁贴是否被pin到主页上,这块磁贴都会被修改。其实,尽管你在主页上看不到系统磁贴,系统磁贴总是存在的,,它原来是隐藏的。

当用户创建自定义磁贴时,只需要修改一下代码即可

// 创建新磁贴int Counter = 0;// StandardTileData就是用来传递ShellTitle的属性参数的,// 如正面背景图的URI,标题,计数器等。StandardTileData myData = new StandardTileData(){Title = string.IsNullOrEmpty(title) == true ? string.Empty : title,Count = int.TryParse(count, out Counter) == true ? Counter : 0,BackTitle = string.IsNullOrEmpty(backtitle) == true ? string.Empty : backtitle,BackContent = string.IsNullOrEmpty(backcontent) == true ? string.Empty : backcontent,BackgroundImage = new Uri(FORE_PIC, UriKind.Relative),BackBackgroundImage = new Uri(BACK_PIC, UriKind.Relative)};ShellTile.Create(new Uri("/MainPage.xaml", UriKind.Relative), myData);当然,这里用户可能需要创建多个磁贴,为了表示区别,可以在uri中修改ShellTile.Create(new Uri("/MainPage.xaml?s=1", UriKind.Relative), myData); 但是之前需要判断该块磁贴是否存在 ShellTile myTitle = ShellTile.ActiveTiles.FirstOrDefault(m => m.NavigationUri.ToString().Contains("s=1")); 具体可以看这篇文章:

好了,用户自定义创建,更新磁贴就这样完成了。下面说说系统后台自动修改磁贴。

显然,windows phone是支持后台程序的,也就是尽管当前程序被关闭,但是后台还是有进程在运行。当我们的程序在第一次被打开后,后台进程就启动,最大运行周期是14天,也就是14天内如果主程序不再次启动,后台程序就会被关闭。当前比较好的做法时每次主程序启动,就修改一下后台进程的运行周期,确保每次都能得到更新。

首先是后台进程控制:

public static void SetBackgroundAgent(){CancelBackgroundAgent();PeriodicTask periodicTask = new PeriodicTask(GlobalAttributes.REFRESH_AGENT_NAME);periodicTask.Description = "refresh reminders";periodicTask.ExpirationTime = DateTime.Today.AddDays(10);try{ScheduledActionService.Add(periodicTask);ScheduledActionService.LaunchForTest(periodicTask.Name, TimeSpan.FromSeconds(15));}catch (InvalidOperationException exception){if (exception.Message.Contains("BNS Error: The action is disabled")){MessageBox.Show("Background agents for this application have been disabled by the user.");//agentsAreEnabled = false;}if (exception.Message.Contains("BNS Error: The maximum number of ScheduledActions of this type have already been added.")){// No user action required. The system prompts the user when the hard limit of periodic tasks has been reached.}}catch { }}public static void CancelBackgroundAgent() { try { ScheduledActionService.Remove(GlobalAttributes.REFRESH_AGENT_NAME); } catch { } }

在ScheduledSyncTaskAgent中添加下面代码,可以参看这篇文章:

protected override void OnInvoke(ScheduledTask task){if (task.Name == GlobalAttributes.REFRESH_AGENT_NAME){RefreshPlaster(); //更新系统磁贴}else{NotifyComplete();}}

更新磁贴代码就调用之前的代码即可public void RefreshPlaster(){SetPlaster.Create("123","22","fsdfd","dsfd");//参数其实可以自己设定,例如获取系统本地时间NotifyComplete();}windows phone中每次只能添加一个后台任务,所以如果需要执行多个任务,只能放到一个任务中,也就是像这样if (task.Name == GlobalAttributes.REFRESH_AGENT_NAME){RefreshReminder();RefreshPlaster();//…//…}

以上就是我自己的一些心得,其实也可以通过消息推送机制更新磁贴,但我这个程序并不需要,具体MSDN上面都有,最后不得不说MSDN实在太强大了。

你不勇敢,没人替你坚强!

windows phone开发学习

相关文章:

你感兴趣的文章:

标签云: