Windows Phone应用程序中的OutOfMemoryException

时间:2014-08-18 12:03:56

标签: c# windows-phone

我在我的应用中制作图库

当我回来并选择另一个专辑系统时出现.OutOfMemoryException

这是我的Xaml

   <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ScrollViewer Margin="0,1,0,71" >
            <Grid Width="475"  Name="picbtn" Background="WhiteSmoke" >

            </Grid>
        </ScrollViewer>
    </Grid>

这是我的代码

     string type;
    int x = 0;
    int y = 4050;
    int i = 0;
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        if (e.NavigationMode != NavigationMode.Back)
        {
            type = NavigationContext.QueryString["name"];

            ReadFromXml(type);
        }
    }
    private void ReadFromXml(string type)
    {

        XDocument xml;
        if (type == "day")
        {
            xml = XDocument.Load("DayimagesData.xml");
        }
        else
        {
            xml = XDocument.Load("NightmagesData.xml");
        }

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

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


            CreateGrid(name);
            i++;
            if (i % 3 == 0)
            {
                x += 150;
                y -= 150;
            }
        }

    }
    private void CreateGrid(string data)
    {

        BitmapImage image = new BitmapImage(
                new Uri(@"images\" + data, UriKind.Relative)
                );
        Grid g1 = new Grid
        {
            Width = 150,
            Height = 150,
            Margin = new Thickness(0, x, 0, y),
            DataContext = data

        };
        Image im = new Image
        {

            Source = image
        };
        g1.Children.Add(im);
        if (i % 3 == 0)
        { g1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; }
        else if (i % 3 == 1)
        {
            g1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
        }
        else
        {
            g1.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
        }

        picbtn.Children.Add(g1);
        g1.Tap += Grid_Tap;
    }

有什么方法可以在后面点击时清除内存? 我试试 所以GC.Collect 但同样的问题发生了

1 个答案:

答案 0 :(得分:1)

正确处理XDocument和BitmapImage。从您的代码中可以看出那些内存消费者。