Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在应用程序中初始化的静态变量为空_C#_Windows Phone 8_Windows Phone - Fatal编程技术网

C# 在应用程序中初始化的静态变量为空

C# 在应用程序中初始化的静态变量为空,c#,windows-phone-8,windows-phone,C#,Windows Phone 8,Windows Phone,WindowsPhone8项目。我有一个类,它保存对图像的引用。我在app类的启动事件处理程序中初始化所述引用: private void Application_Launching(object sender, LaunchingEventArgs e) { TheClass.Load(); } //Elsewhere... class TheClass { static private int[] s_Pixels = null; static public vo

WindowsPhone8项目。我有一个类,它保存对图像的引用。我在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_像素为空。我不能复制它,但我的用户显然可以。有一个代码路径导致调用
GetImage()
,而事先没有调用
Load()

没有调用的是
Load
,而不是我调用的
Load
onimageload
从未发生过

任何地方都没有其他s_像素的指定

我会检查图像加载错误。有一个
ImageFailed
事件处理程序,它会留下日志跟踪。它从未被调用过,为什么会被调用呢?有问题的图像在应用程序的资源中


这怎么可能呢?Windows Phone应用程序如何在不调用启动的情况下进行初始化和加载?

应用程序启动
仅在应用程序重新启动时调用。如果您将其发送到后台,系统最终会将其删除,然后用户重新激活它,您的静态数据将消失,但不会调用
启动
。相反,您将得到一个调用
应用程序\u Activated

因此,基本上,您需要在
启动
激活
方法上运行所有静态初始化

通过使用Visual Studio强制删除应用程序,您很可能会重现用户看到的问题:在项目选项的“调试”选项卡上选中“调试时停用时删除”,在“调试”下运行应用程序,在应用程序运行时按Windows键,然后切换回应用程序