如何从枢轴页面中删除两个透视项目? [英] How to remove two pivot items from the pivot page?
问题描述
好吧,我的枢轴页面中有四个透视项目。我想以编程方式删除两个数据透视项。
所以,我编码如下,但应用程序中断了。如果我编码删除单个Pivot项目。它运作良好。但不适用于两个枢轴项目。为什么?
Well, I have four pivot items in my pivot page. I wanna remove two pivot items programmatically.
So, I coded like below, But the app breaks. If I coded to remove single Pivot Item. It works well. But not working for two pivot items. Why ?
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.ContainsKey("Page"))
{
var page = NavigationContext.QueryString["Page"];
switch (page)
{
case "2":
pivot.Items.Remove(item3);
pivot.Items.Remove(item4);
break;
case "3":
pivot.Items.Remove(item3);
pivot.Items.Remove(item4);
break;
}
}
}
App break原因:
> * Microsoft.Phone.ni.dll中出现'System.ArgumentException'类型的异常,但未在用户代码中处理*
指向此行pivot.Items。删除(第4项);
$
XAML代码:
App breaks Reason:
>*An exception of type 'System.ArgumentException' occurred in Microsoft.Phone.ni.dll but was not handled in user code*
points this line pivot.Items.Remove(item4);
XAML Code:
<phone:Pivot x:Name="pivot">
<!--Pivot item one-->
<phone:PivotItem Margin="12,20,12,7" Name="item1">
<Grid/>
</phone:PivotItem>
<!--Pivot item two-->
<phone:PivotItem Margin="12,20,12,7" Name="item2">
<Grid/>
</phone:PivotItem>
<!--Pivot item three-->
<phone:PivotItem Margin="12,20,12,7" Name="item3">
<Grid/>
</phone:PivotItem>
<!--Pivot item four-->
<phone:PivotItem Margin="12,20,12,7" Name="item4">
<Grid/>
</phone:PivotItem>
</phone:Pivot>
- Mohamed Thaufeeq A
- Mohamed Thaufeeq A
推荐答案
你好!
我尝试但没有任何例外:
I try it but didn't get any exceptions:
xaml:
<phone:PhoneApplicationPage
x:Class="PhoneApp1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<phone:Pivot x:Name="pivot"
Height="600" HorizontalAlignment="Stretch">
<!--Pivot item one-->
<phone:PivotItem Margin="12,20,12,7" Name="item1"
Header="item 1">
<TextBlock Text="text 1"/>
</phone:PivotItem>
<!--Pivot item two-->
<phone:PivotItem Margin="12,20,12,7" Name="item2"
Header="item 2">
<TextBlock Text="text 2"/>
</phone:PivotItem>
<!--Pivot item three-->
<phone:PivotItem Margin="12,20,12,7" Name="item3"
Header="item 3">
<TextBlock Text="text 3"/>
</phone:PivotItem>
<!--Pivot item four-->
<phone:PivotItem Margin="12,20,12,7" Name="item4"
Header="item 4">
<TextBlock Text="text 4"/>
</phone:PivotItem>
</phone:Pivot>
<Button Content="click" Grid.Row="1" Click="Button_Click" Width="200" Height="100"/>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
代码背后:
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.pivot.Items.Remove(this.item2);
this.pivot.Items.Remove(this.item3);
}
}
这篇关于如何从枢轴页面中删除两个透视项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!