OutOfMemoryException未处理

时间:2014-08-28 07:06:47

标签: c# windows-phone-8

我的“Windows Phone模拟器--WVGA 512 MB”和我的设备出现以下异常。使用“Windows Phone Emulator - WVGA”时,我没有遇到这个问题。

这是我的xaml

    <phone:Panorama  Name="pano" >


    </phone:Panorama>

这是我背后的代码

   protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        if (e.NavigationMode != NavigationMode.Back)
        {

            type = NavigationContext.QueryString["type"];
            ReadFromXml(type);
        }
    }

    private void ReadFromXml(string type)
    {
        XDocument xml=XDocument.Load(@"data/image_saba7.xml");


        var query = from c in xml.Root.Descendants("post")

                    select  c.Element("image").Value;
        foreach (var name in query)
        {


                createPanoramItem("", name);

        }
    }


    private void createPanoramItem(string tit, string imgurl)
    {
        BitmapImage image = new BitmapImage(
            new Uri(imgurl, UriKind.Relative)
            );


        Grid g = new Grid();
        Image im = new Image
        {

            Source = image,
            VerticalAlignment = System.Windows.VerticalAlignment.Center,
            Margin = new Thickness(10, 290,-10, 80),

        };
        img = imgurl;
        im.Tap += im_Tap;
        TextBlock t = new TextBlock
        {
            TextWrapping = TextWrapping.Wrap,
            Margin = new Thickness(10, 296, 10, 300),
            HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
            TextAlignment = TextAlignment.Right,
            FontSize = 25,
            FontWeight = System.Windows.FontWeights.SemiBold,
            Foreground = new SolidColorBrush(new Color()
            {
                A = 255 /*Opacity*/,
                R = 132 /*Red*/,
                G = 91 /*Green*/,
                B = 54 /*Blue*/
            }),
            Text = tit

        };


        g.Children.Add(t);
        g.Children.Add(im);

        PanoramaItem panoramaCtrlItem = new PanoramaItem();
        panoramaCtrlItem.Content = g;
        // panoramaCtrlItem.Header = title;
        pano.Items.Add(panoramaCtrlItem);
    }

1 个答案:

答案 0 :(得分:2)

你使用的RAM太多了。您需要找到一种方法来摆脱全景图中的几个项目,或者开发一种方案,通过该方案,您只需加载相关的内容,例如:仅在图像显示在屏幕上之前加载图像。

因此,不要一次性加载图像,而是在用户向左或向右滑动时加载它们。或者决定每个“页面”的数字,并且一次只加载那么多。