Windows Phone 8项目。我有一个类,它保存一个图像的引用。我在app类中的启动事件处理程序中初始化了上述引用:
private void Application_Launching(object sender, LaunchingEventArgs e)
{
TheClass.Load();
}
//Elsewhere...
class TheClass
{
static private int[] s_Pixels = null;
static public void Load()
{
BitmapImage bi = new BitmapImage(new Uri("/res/image.png", UriKind.Relative));
bi.CreateOptions = BitmapCreateOptions.BackgroundCreation;
bi.ImageOpened += OnImageLoaded;
bi.ImageFailed += OnImageFailed;
}
private static void OnImageLoaded(object o, RoutedEventArgs a)
{
BitmapImage bi = o as BitmapImage;
s_Pixels = new WriteableBitmap(bi).Pixels;
}
// And consumers call this one:
static public WriteableBitmap GetImage()
{
if (s_Pixels == null)
SendDebugReport();
}
}这段代码适用于我。然而,我得到了那些调试报告,表示s_Pixels为null。我不能复制它,但我的用户显然可以。有一个代码路径会导致GetImage()被调用,而不需要事先调用Load()。
没有被调用的是Load,也不是我所说的Load和OnImageLoaded从未发生过。
s_Pixels在任何地方都没有其他任务。
我确实检查了图像加载错误。有一个ImageFailed事件处理程序会留下一个日志跟踪。它从来没有被调用过,为什么它会被调用--所讨论的图像在应用程序的资源中。
这怎么可能?Windows Phone应用程序如何在不被调用的情况下初始化和加载?
发布于 2015-06-23 15:27:29
只有在应用程序重新启动时才会调用Application_Launching。如果您将其发送到后台,系统最终会对其进行删除,然后用户重新激活它,您的静态数据就会消失,但是Launching不会被调用。相反,您将接到Application_Activated的电话。
因此,基本上,您需要在Launching和Activated方法上运行所有静态初始化。
您很可能通过强制使用Visual对应用程序进行墓碑处理来重现用户所看到的问题:检查项目选项的Debug选项卡上的“调试时停用时的墓碑”,在调试器下运行应用程序,在应用程序运行时按下Windows键,然后切换回应用程序。
https://stackoverflow.com/questions/31006113
复制相似问题