我正在开发一个Windows Phone 7应用程序,并试图将Windows Phone 8的功能实现为广泛的磁贴。我使用反射实现了它,但是当我想使用ScheduledAgent为周期性任务更新磁贴时,磁贴没有被创建。
预定代理OnInvoke代码看起来像
protected override void OnInvoke(ScheduledTask task)
{
//TODO: Add code to perform your task in background
if (task is PeriodicTask)
{
//Update the tile using Scheduled Task
CreateTileForWindowsPhone.CreateWideTile();
}
NotifyComplete();
}
我使用此代码
创建了磁贴public class CreateTileForWindowsPhone
{
private static Version TargetedVersion = new Version(7, 10, 8858);
public static bool IsTargetedVersion { get { return Environment.OSVersion.Version >= TargetedVersion; } }
public static void CreateWideTile()
{
if (IsTargetedVersion)
{
try
{
// Get the new FlipTileData type.
Type flipTileDataType = Type.GetType("Microsoft.Phone.Shell.FlipTileData, Microsoft.Phone");
// Get the ShellTile type so we can call the new version of "Update" that takes the new Tile templates.
Type shellTileType = Type.GetType("Microsoft.Phone.Shell.ShellTile, Microsoft.Phone");
// Loop through any existing Tiles that are pinned to Start.
QuotesCollection aq = new QuotesCollection();
Random rand = new Random();
int randNum = rand.Next(0, 163);
//String wideBackStr = "Dont be the same, Be Better.";
String wideBackStr = aq.quotes[randNum];
foreach (var tileToUpdate in ShellTile.ActiveTiles)
{
// Get the constructor for the new FlipTileData class and assign it to our variable to hold the Tile properties.
var UpdateTileData = flipTileDataType.GetConstructor(new Type[] { }).Invoke(null);
// Set the properties.
SetProperty(UpdateTileData, "WideBackgroundImage", new Uri("/images/QuottedWideTile.png", UriKind.Relative));
SetProperty(UpdateTileData, "WideBackContent", wideBackStr);
// Invoke the new version of ShellTile.Update.
shellTileType.GetMethod("Update").Invoke(tileToUpdate, new Object[] { UpdateTileData });
break;
}
}
catch
{
MessageBox.Show("Tile Error Caught");
}
}
}
private static void SetProperty(object instance, string name, object value)
{
var setMethod = instance.GetType().GetProperty(name).GetSetMethod();
setMethod.Invoke(instance, new object[] { value });
}
}
我制作了注册代理方法来注册周期性任务
private void RegisterAgent()
{
string taskName = "MyTask";
try
{
if (ScheduledActionService.Find(taskName) != null)
{
//if the agent exists, remove and then add it to ensure
//the agent's schedule is updated to avoid expiration
ScheduledActionService.Remove(taskName);
}
PeriodicTask periodicTask = new PeriodicTask(taskName);
periodicTask.Description = "Random Quote Update On Tile";
ScheduledActionService.Add(periodicTask);
}
catch (InvalidOperationException exception)
{
MessageBox.Show(exception.Message);
}
catch (SchedulerServiceException schedulerException)
{
MessageBox.Show(schedulerException.Message);
}
}
并在应用程序启动中调用该注册表
private void Application_Launching(object sender, LaunchingEventArgs e)
{
RegisterAgent();
}
但是当我运行应用程序时,没有创建宽磁贴。 在我使用预定代理更新磁贴之前,广泛的磁贴创建过去了。我曾经通过在应用程序启动时调用该函数来创建一个宽磁贴。
private void Application_Launching(object sender, LaunchingEventArgs e)
{
CreateTileForWindowsPhone.CreateWideTile();
}
为什么没有创建图块。我做错了吗?
答案 0 :(得分:0)
Deployment.Current.Dispatcher.BeginInvoke(
CreateTileForWindowsPhone.CreateWideTile);