我想在用户更改页面时从 flipview 中删除一个项目。有什么我可以做的吗?
I want to remove an item from flipview when the user change pages. Is there anything I can do?
我的要求是我在我的应用程序中使用了 flipview 并添加页面 dynamically.i 当我单击添加页面按钮时我想这样做我的当前页面从 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;
}
完成这项工作的一个非常方便的方法是在 DataTemplate
中为 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>
在 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" });
}
最后,如果你想在按钮点击事件中删除一个项目并添加一个新项目:
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);
}
我的要求是我在我的应用程序中使用了 flipview 并添加页面 dynamically.i 当我单击添加页面按钮时我想这样做我的当前页面从 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;
}
完成这项工作的一个非常方便的方法是在 DataTemplate
中为 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>
在 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" });
}
最后,如果你想在按钮点击事件中删除一个项目并添加一个新项目:
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);
}