如何在触发所有ImageOpened()
事件之前等待后台代理执行(在本例中为3),以便使用自定义映像更新辅助实时磁贴?
编辑1:
在OnInvoke()
的{{1}}方法中我调用自己的创建切片数据函数在共享库中实现,而共享库又订阅了3 ScheduledAgent
个事件,因为我正在尝试创建所有实时图块模板的自定义图像,即小,中,宽。
由于这些是异步事件,我无法检查所有事件是否已成功完成,因此我可以调用ImageOpened()
通知后台代理其作业现已完成。所以有时瓷砖会更新,而大部分时间都没有。此外,我每次启动应用程序时都使用相同的功能来更新动态磁贴,因此它的实现没有问题。我还尝试通过部署位图并强制调用NotifyComplete()
来处理ScheduledAgent的所有内存限制。
请以任何可能的方式帮助解决此问题。
答案 0 :(得分:1)
添加一个允许您创建自定义事件的新类 -
public class SaveImageCompleteEventArgs : EventArgs
{
public bool Success { get; set; }
public Exception Exception { get; set; }
public string ImageFileName { get; set; }
public SaveImageCompleteEventArgs(bool success, string fileName)
{
Success = success;
ImageFileName = fileName;
}
}
从 -
初始化要更新自定义动态磁贴的文件中的事件和必需变量public static int countTile = 3;
public event EventHandler<SaveImageCompleteEventArgs> SaveMediumImageComplete;
public event EventHandler<SaveImageCompleteEventArgs> SaveWideImageComplete;
public event EventHandler<SaveImageCompleteEventArgs> SaveSmallImageComplete;
public event EventHandler<SaveImageCompleteEventArgs> SaveAllImagesComplete;
在所有磁贴的ImageOpened()
事件处理程序中触发完成事件,并检查是否需要触发SaveAllImagesComplete
事件 -
public void OnBackgroundBmpOpenedMedium(object sender, RoutedEventArgs e)
{
if (SaveMediumImageComplete != null)
{
countTile -= 1;
CheckIfAllImagesOpened();
SaveMediumImageComplete(this, new SaveImageCompleteEventArgs(true, mediumTileImageUriIronMan));
}
}
private void CheckIfAllImagesOpened()
{
if (countTile == 0)
{
if (SaveAllImagesComplete != null)
{
var args1 = new SaveImageCompleteEventArgs(true, "");
SaveAllImagesComplete(this, args1);
}
}
}
在ScheduledAgent文件中 -
public static ManualResetEvent evt;
public bool IsPaused { get { return !evt.WaitOne(0); } }
在OnInvoke()函数中 -
evt = new ManualResetEvent(false);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
//Initialize secondary tile here
if (secondaryTile != null)
{
/*
obj is a object of a helper file that contains all the
functions responsible for updating the custom live tile
Call the function that is responsible for initializing all the
tile image bitmpas and that subscribes to the ImageOpened events
*/
obj.SaveMediumImageComplete += async (s, args) =>
{
if (!IsPaused)
evt.Set();
};
obj.SaveWideImageComplete += async (s, args) =>
{
if (!IsPaused)
evt.Set();
};
obj.SaveSmallImageComplete += async (s, args) =>
{
if (!IsPaused)
evt.Set();
};
obj.SaveAllImagesComplete += async (s, args) =>
{
try
{
if (args.Success)
obj.UpdateTileIcon();
}
catch (Exception) { }
finally
{
if (!IsPaused)
evt.Set();
}
};
}
});
evt.WaitOne();
NotifyComplete();