读书人

windows phone后台进程访问UI

发布时间: 2012-12-22 12:05:05 作者: rapoo

求助windows phone后台进程访问UI
最近刚开始做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工程里面的构造函数执行即可。

谢谢了
------最佳解决方案--------------------


你不会是问


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


这个问题吧
[其他解释]
用通知吧 不知道行不行 顺便同看其他方案
[其他解释]
差不多
引用:
你不会是问
C# code

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


这个问题吧

[其他解释]
引用:
差不多

引用:

你不会是问
C# code

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


这个问题吧

那我帮到你了么?
[其他解释]
通知还需要微软服务器的
引用:
用通知吧 不知道行不行 顺便同看其他方案

[其他解释]
你的意思是这样?
 public void RefreshPlaster()
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
Tile t = new Tile();
});
}
public v
引用:
引用:
差不多

引用:

你不会是问
C# code

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


这个问题吧

那我帮到你了么?

[其他解释]
你可以试试

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


[其他解释]
这样系统会不会不稳定,每次进来都要创建对象
引用:
你可以试试

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


[其他解释]
你只把更新UI的代码写进我给的函数中,其他代码不变。
不会有影响
------其他解决方案--------------------


引用:
你只把更新UI的代码写进我给的函数中,其他代码不变。
不会有影响

读书人网 >Windows Mobile

热点推荐