简体   繁体   English

在Windows Phone中删除枢纽项目

[英]remove pivot item in windows phone

I have around 30 images which i want to keep as items in pivot control.But if i do all those i have encountered OutOfMemoryException. 我有大约30张图像,我想将它们保留为数据透视控制中的项目。但是如果我做的所有这些我都遇到了OutOfMemoryException。 SO i was adding pivots dynamically.Now if i exceed some limit i want to remove pivot items, but if i remove on pivot selection changed i am getting InvalidException. 所以我正在动态添加数据透视表。现在,如果我超出某个限制,我想删除数据透视表项,但是如果我在数据透视表选择更改后删除,我会收到InvalidException In the snippet pivotshow is the pivot control. 片段中的pivotshow是枢轴控件。

    void PivotShow_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        AddItems();
    }

    private void AddItems()
    {
        PivotItem toadd = PivotGen(images[i]);
        i = (i + 1) % (images.Length);
        PivotShow.Items.Add(toadd);
        try
        {
            if (PivotShow.Items.Count > 3)
                PivotShow.Items.RemoveAt(0);
        }
        catch (InvalidOperationException)
        {
            MessageBox.Show("Operation not allowed");
        }
    }

    private PivotItem PivotGen(string urlimage)
    {
        PivotItem p = new PivotItem();
        p.Margin = new Thickness(0, -90, 0, 0);

        Image img = new Image();
        BitmapImage bmp = new BitmapImage(new Uri(urlimage, UriKind.Relative));
        img.Stretch = Stretch.Fill;
        img.Source = bmp;
        p.Content = img;

        return p;
        //PivotShow.Items.Add(p);
    }

thanks in advance 提前致谢

This is most likely occuring because you are trying to change a collection that is currently being modified. 这很可能发生,因为您正在尝试更改当前正在修改的集合。 You could defer your code as follows: 您可以按以下方式延迟代码:

        EventHandler handler = null;
        handler = (s, e) =>
        {
            element.LayoutUpdated -= handler;

            AddItems();
        };
        element.LayoutUpdated += handler;

The above code will invoke AddItems on the next layout pass. 上面的代码将在下一次布局传递时调用AddItems Give this a go and see if it helps! 试试看,看看是否有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM