Windows Phone应用程序OutOfMemoryException

本文关键字:OutOfMemoryException 应用程序 Phone Windows | 更新日期: 2023-09-27 18:18:34

我在我的应用程序制作图库

当我返回并选择另一个相册系统时。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但是同样的问题又发生了

Windows Phone应用程序OutOfMemoryException

妥善处理XDocument和BitmapImage .从你的代码中,这些是高注意的内存消费者

相关文章:
  • 没有找到相关文章