【发布时间】:2014-07-24 20:17:36
【问题描述】:
我正在开发一个 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();
}
为什么没有创建磁贴。我是不是做错了什么?
【问题讨论】:
-
为什么要针对 WP7 并通过反射来做到这一点?您是否尝试过升级到 WP8 并更新您的磁贴?
-
目前我正在开发 vs2010 上的 Wp7。所以我使用反射来创建瓷砖。我刚刚发现了一个“无效的跨线程错误”。有什么建议吗?
-
听起来你应该在 UI 线程上运行一些代码。使用 Dispatcher RunAsync
-
这是什么状态?
-
@Mikko 感谢您的建议。但我想通了。有用。 CreateTileForWindowsPhone 类中包含一些不应该使用的 api,因为 ScheduledAgents 不支持它们。因此,没有创建宽瓷砖。删除它们,我的代码工作正常。
标签: c# windows-phone-7 windows-phone-8 windows-phone windows-phone-7.1