动态地向PivotItem添加内容

时间:2014-05-04 18:22:00

标签: c# xaml windows-phone-8 windows-phone

我有一个带有Pivot的页面。它基于Visual Studio模板。

    <!--Pivot Control-->
    <phone:Pivot SelectionChanged="evt_pivot_SelectionChanged">
        <phone:Pivot.Title>
            <StackPanel HorizontalAlignment="Center">
                <!-- <TextBlock Text="MyApp" /> -->
                <Image Stretch="None" HorizontalAlignment="Left" Margin="0" MinWidth="50" MaxHeight="50" Source="/mAppData/logo.png"/>
            </StackPanel>
        </phone:Pivot.Title>

        <!--Pivot item one-->
        <phone:PivotItem Header="Favoriten">
            <!--Double line list with text wrapping-->
            <phone:LongListSelector Margin="13,0,0,0" ItemsSource="{Binding Items}">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,25">
                            <Grid VerticalAlignment="Top">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="100" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Grid.ColumnSpan="2" Text="FELIX ClubRestaurant (Berlin)" TextWrapping="NoWrap" Style="{StaticResource PhoneTextLargeStyle}" VerticalAlignment="Top" Margin="0,0,0,22" />
                                <Image Grid.Column="0" Width="110" Height="20" Source="/mAppData/stars-3.png" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="0"/>
                                <TextBlock Grid.Column="1" Text="10 min." TextWrapping="NoWrap" Margin="0" Style="{StaticResource PhoneTextSubtleStyle}" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
                            </Grid>
                            <Grid VerticalAlignment="Top" Margin="0,10,0,0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Image Grid.Column="0" Width="100" Height="100" Source="http://img.myserver.net/news-teaser//p189j19861b36c5d1pp012i21grgd.gif"/>
                                <Image Grid.Column="1" Width="100" Height="100" Source="http://img.myserver.net/news-teaser//p187qrndfcj0la0f12clfkv10ec7.gif"/>
                                <Image Grid.Column="2" Width="100" Height="100" Source="http://img.myserver.net/news-teaser/005e5d03f058fa8f7bd95f6410dfc6d6.gif"/>
                                <Image Grid.Column="3" Width="100" Height="100" Source="http://img.myserver.net/news-teaser/3c05cbf76fba7ada5182b4426e55d96b.gif"/>
                            </Grid>
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
        </phone:PivotItem>

        <!--Pivot item two-->
        <phone:PivotItem Header="Empfohlen">
        </phone:PivotItem>
    </phone:Pivot>

我在CodeBehind中添加了对SelectionChanged事件的处理。这很好。因此,当用户访问第二个PivotItem时,我可以通过代码捕获。

    private async void evt_pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int mPivotIndex = Convert.ToInt16(((Pivot)sender).SelectedIndex.ToString());
        if (mPivotIndex == 1)
        {
             // HERE I WANT TO INSERT THE SOLUTION
        }
    }

现在出现了我的问题:当用户导航到秒项时,我想:

  • 从WebService请求一些数据(这不是问题)
  • 转换数据(这不是问题)和
  • 填充LongListSelector中的数据(这是我的问题第1部分)

如果发生错误,我不想显示LongListSelector,而是显示消息的TextBox(这是我的问题第2部分)

如何解决我的2个问题呢?

2 个答案:

答案 0 :(得分:1)

您需要将itemtemplate中控件的文本/内容绑定到数据集合fieldname Text =“{Binding Fieldname}” http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207023(v=vs.105).aspx

在错误处理中,将Longlistselector的可见性设置为Collapsed。然后,您可以显示另一个显示文本框的控件

<StackPanel>
<LongListSelector x:Name=”MyListSelector”>
..stuff
</LongListSelector>
<TextBlock x:Name=”MyError” Visibility=”Collapsed”> </TextBlock>
</StackPanel>

Catch (Exception Ex)
{
    MyListSelector.Visibility = Visibility.Collapsed
MyError.Visibility = Visibility.Visible
MyError.Text = Ex.Message;
}

答案 1 :(得分:0)

您可以隐藏Longlist选择器并直接从后面的代码添加带有Error消息的Textbox。请参阅下面的代码。

try
{
   //make the service call and do your stuff
}
catch(exception ex)
{
    MyListSelector.Visibility = Visibility.Collapsed;
    var errorTextBox = new TextBox();
    errorTextBox.Text = "some Error has occured";
    //add all the properties you want to add for textbox such as color,height,fontsiz ect..
    //Name your pivotitem control, say pivotitem1
    pivotitem1.Content = errorTextBox;    

}

希望这会有所帮助。

注意:未编译的代码。赦免汇编错误