C# 当用户更改页面时,我想从flipview中删除一个项目。有什么我能做的吗?

C# 当用户更改页面时,我想从flipview中删除一个项目。有什么我能做的吗?,c#,uwp,flipview,C#,Uwp,Flipview,我的要求是,我必须在应用程序中使用flipview并动态添加页面。我希望在单击addpages按钮时执行此操作,从flipview中删除当前页面,然后出现新页面。我的addpage代码 public void Add_Pages_Click(object sender, RoutedEventArgs e) { try { my_canvas = new Canvas(); my_canvas.Nam

我的要求是,我必须在应用程序中使用flipview并动态添加页面。我希望在单击addpages按钮时执行此操作,从flipview中删除当前页面,然后出现新页面。我的addpage代码

   public void Add_Pages_Click(object sender, RoutedEventArgs e)
    {
        try
        {

            my_canvas = new Canvas();
            my_canvas.Name = "Item" + DateTime.Now.ToFileTime().ToString();
            //fp1.ItemsSource = fi;
            fp1.Items.Add(my_canvas);
            fp1.SelectionChanged += Fp1_SelectionChanged;
            Stickers1.Visibility = Visibility.Collapsed;
            Backgrounds1.Visibility = Visibility.Collapsed;
            Popup_wordart.IsOpen = false;
            PopUp_Media.IsOpen = false;
            my_canvas.Visibility = Visibility.Collapsed;
            Audio_Recorder.IsOpen = false;
        }
        catch (Exception ex)
        {


        }
       // Backward_Arrow.Visibility = Visibility.Visible;
    }

完成这项工作的一种非常传统的方法是在
数据模板中为
FlipView
的每个项目构建一个结构,然后使用
ObservableCollection
添加、删除、刷新
FlipView
的项目

<FlipView x:Name="flipView" ItemsSource="{x:Bind flipviewCollection}">
    <FlipView.ItemTemplate>
        <DataTemplate>
            <Canvas>
                <Image Source="{Binding ImageSource}" Stretch="None"/>
            </Canvas>
        </DataTemplate>
    </FlipView.ItemTemplate>
</FlipView>
然后在
FlipView
的代码隐藏中,创建此数据模型的集合,并将数据添加到此集合:

ObservableCollection<ImageSourceClass> flipviewCollection = new ObservableCollection<ImageSourceClass>();

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    flipviewCollection.Clear();
    flipviewCollection.Add(new ImageSourceClass { ImageSource = "Assets/1.jpg" });
    flipviewCollection.Add(new ImageSourceClass { ImageSource = "Assets/2.jpg" });
    flipviewCollection.Add(new ImageSourceClass { ImageSource = "Assets/3.jpg" });
    flipviewCollection.Add(new ImageSourceClass { ImageSource = "Assets/4.jpg" });
    flipviewCollection.Add(new ImageSourceClass { ImageSource = "Assets/5.jpg" });
}
问题是,如果您没有使用此方法构建
FlipView
,您只是手动将项目添加到
FlipView
,您还需要手动删除代码中类似的项目,例如:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var my_canvas = new Canvas();
    var textbox = new TextBox();
    my_canvas.Children.Add(textbox);
    flipView.Items.Insert(flipView.SelectedIndex + 1, my_canvas);
    flipView.Items.RemoveAt(flipView.SelectedIndex);              
}
private void Button_Click(object sender, RoutedEventArgs e)
{
    //index = flipView.SelectedIndex + 1 because when remove one item, next item will be shown, 
    //it will behavior like a new item replace the old one.
    flipviewCollection.Insert(flipView.SelectedIndex + 1, new ImageSourceClass { ImageSource = "Assets/new.jpg" });
    flipviewCollection.Remove(flipView.SelectedItem as ImageSourceClass);                                
}
private void Button_Click(object sender, RoutedEventArgs e)
{
    var my_canvas = new Canvas();
    var textbox = new TextBox();
    my_canvas.Children.Add(textbox);
    flipView.Items.Insert(flipView.SelectedIndex + 1, my_canvas);
    flipView.Items.RemoveAt(flipView.SelectedIndex);              
}