求助windows phone后台进程访问UI

Felven 2012-05-17 03:14:58
最近刚开始做windows phone日历应用的开发,程序要实现磁贴的更新,我的做法是这样的:
1、启动程序后后台任务就启动,定时调用函数就行判断更新
在ScheduledAgent.cs 中创建一个函数


if (task.Name == GlobalAttributes.REFRESH_AGENT_NAME)
{

RefreshTile();
NotifyComplete();
}

2、由于我需要本地产生背景图片,所以我考虑的本地绘图,不懂的可以参看这里http://www.cnblogs.com/alexis/archive/2011/07/22/2113253.html
于是我创建了一个工程,里面仅包含一个 windows phone user control页面,前台布局如下:
   <Grid x:Name="LayoutRoot">
<Grid x:Name="xamlGrid" Width="173" Height="173" >
<Image Source="/365Plus;component/Images/Default.jpg" HorizontalAlignment="Left"
Name="image1" Stretch="Fill"
VerticalAlignment="Top" />
<TextBlock HorizontalAlignment="Left" Margin="5,5,0,0" Name="YearAndMonthTextBlock" Text="2012年12月" VerticalAlignment="Top" FontFamily="Portable User Interface" FontSize="20" />
<TextBlock HorizontalAlignment="Left" FontFamily="Portable User Interface" Margin="5,35,0,0" Name="DayTextBlock" Text="20日" VerticalAlignment="Top" FontSize="35" />
<TextBlock HorizontalAlignment="Left" FontFamily="Portable User Interface" Margin="85,55,0,0" Name="WeekTextBlock" Text="周四" VerticalAlignment="Top" FontSize="20" />
<TextBlock HorizontalAlignment="Left" FontFamily="Portable User Interface" Margin="5,129,0,0" Name="LunarTextBlock" Text="初八" VerticalAlignment="Top" FontSize="26" />
</Grid>
</Grid>

在后台代码中这样写
 public Tile()
{
InitializeComponent();

//判断是否是当前日期,如果不是就刷新页面
if (_365Settings.GetDateForTile().Date != DateTime.Today)
{
CreateAndModify();
}


}

public void CreateAndModify()
{
YearAndMonthTextBlock.Text = DateTime.Today.ToString("yyyy年MM月");
DayTextBlock.Text = DateTime.Today.Day.ToString();
LunarTextBlock.Text = GetLunarString(DateTime.Today);

WriteableBitmap bitmap = new WriteableBitmap(this.xamlGrid, null);

string tiledirectory = "Shared/ShellContent/tiles";//note :父目录必须是 Shared/ShellContent

string tempJPEG = tiledirectory + @"/" + "LiveTile.jpg";

using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!store.DirectoryExists(tiledirectory))
{
store.CreateDirectory(tiledirectory);
}
using (var stream = store.OpenFile(tempJPEG, System.IO.FileMode.OpenOrCreate))
{
bitmap.SaveJpeg(stream, 173, 173, 0, 100);
}
}

_365Settings.SetDateForTile(DateTime.Today);
ShellTile TileToFind = ShellTile.ActiveTiles.First();
StandardTileData data = new StandardTileData();
data.BackBackgroundImage = new Uri("isostore:/" + tempJPEG, UriKind.Absolute);
TileToFind.Update(data);
}



然后我觉得只需要在ScheduledAgent.cs RefreshPlaster();函数中创建Tile用户控件就可以了,于是我的 RefreshPlaster()是这样写的
   public void RefreshTile()
{
Tile t = new Tile();
}


问题就是出现Invalid cross-thread access ,后台任务不能进行UI创建,我不知道如何解决,我查了一下,发现http://stackoverflow.com/questions/5666772/how-can-i-render-text-on-a-writeablebitmap-on-a-background-thread-in-windows-ph这里有类似的问题,但是那个是自己重写类了,我不想那么麻烦。想请教高人指点如何调用UI,只要执行RefreshTile时能确保Tile工程里面的构造函数执行即可。

谢谢了
...全文
396 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Felven 2012-05-17
  • 打赏
  • 举报
回复
哦[Quote=引用 9 楼 的回复:]

你只把更新UI的代码写进我给的函数中,其他代码不变。
不会有影响
[/Quote]
gnimgnot 2012-05-17
  • 打赏
  • 举报
回复
你只把更新UI的代码写进我给的函数中,其他代码不变。
不会有影响
Felven 2012-05-17
  • 打赏
  • 举报
回复
这样系统会不会不稳定,每次进来都要创建对象[Quote=引用 7 楼 的回复:]

你可以试试

但凡操作UI的代码都放到
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
在这里更新Ui });


[/Quote]
gnimgnot 2012-05-17
  • 打赏
  • 举报
回复
你可以试试

但凡操作UI的代码都放到
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
在这里更新Ui });

Felven 2012-05-17
  • 打赏
  • 举报
回复
你的意思是这样?
 public void RefreshPlaster()
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
Tile t = new Tile();
});
}
public v
[Quote=引用 4 楼 的回复:]

引用 3 楼 的回复:
差不多

引用 2 楼 的回复:

你不会是问
C# code

Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
在这里更新Ui });


这个问题吧

那我帮到你了么?
[/Quote]
Felven 2012-05-17
  • 打赏
  • 举报
回复
通知还需要微软服务器的[Quote=引用 1 楼 的回复:]

用通知吧 不知道行不行 顺便同看其他方案
[/Quote]
gnimgnot 2012-05-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
差不多

引用 2 楼 的回复:

你不会是问
C# code

Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
在这里更新Ui });


这个问题吧
[/Quote]
那我帮到你了么?
Felven 2012-05-17
  • 打赏
  • 举报
回复
差不多[Quote=引用 2 楼 的回复:]

你不会是问
C# code

Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
在这里更新Ui });


这个问题吧
[/Quote]
gnimgnot 2012-05-17
  • 打赏
  • 举报
回复
你不会是问

Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
在这里更新Ui });


这个问题吧
  • 打赏
  • 举报
回复
用通知吧 不知道行不行 顺便同看其他方案

7,655

社区成员

发帖
与我相关
我的任务
社区描述
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox LIVE游戏、Zune音乐与独特的视频体验整合至手机中。
社区管理员
  • Windows客户端开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧