繁体   English   中英

我想在用户更改页面时从flipview中删除项目。 有什么我能做的吗?

[英]I want to remove an item from flipview when the user change pages. Is there anything I can do?

我的要求是我在我的应用程序中使用了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;
    }

一种非常方便的方法是在DataTemplateFlipView每个项目构建一个结构,然后使用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>

为需要在DataTemplate绑定的数据定义模型:

public class ImageSourceClass
{
    public string ImageSource { get; set; }
}

然后在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" });
}

最后,如果要删除项目并在Button click事件中添加新项目:

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);                                
}

问题是,如果您没有使用此方法来构建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);              
}

暂无
暂无

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

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