求助windows phone后台进程访问UI
最近刚开始做windows phone日历应用的开发,程序要实现磁贴的更新,我的做法是这样的:
1、启动程序后后台任务就启动,定时调用函数就行判断更新
在ScheduledAgent.cs 中创建一个函数
if (task.Name == GlobalAttributes.REFRESH_AGENT_NAME)
{
RefreshTile();
NotifyComplete();
}
<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);
}
public void RefreshTile()
{
Tile t = new Tile();
}
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
在这里更新Ui });
public void RefreshPlaster()
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
Tile t = new Tile();
});
}
public v