如何获得数组的第一项

时间:2013-07-24 08:00:18

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

我有一个关于媒体新闻的Windows手机项目。 在这个项目上,我有一个panaroma项目。在我的第一次筛选中,我从我的网络API获得了新闻。

我使用长列表选择器来获取这些新闻,但我想把它的第一个/热门/最新版本放在顶部,并附上它的大图和文字。但是我无法在我的网格和LongListSelector同时正确地给出来源。

简而言之,我想列出我的新闻“一个在另一个之下”,最新/第一个新的将在顶部大图。

这是我尝试过的;

MainPage.xaml中

<!--Panorama item one-->
<phone:PanoramaItem Header="Haberler" Foreground="DarkGreen">
    <ScrollViewer VerticalScrollBarVisibility="Auto" Background="White">
        <StackPanel Orientation="Vertical">


                        <Grid x:Name="HeadNew">
                            <TextBlock x:Name="textBlock14" Text="{Binding Path=TITLE}"  TextWrapping="Wrap" Margin="20,0" FontSize="{StaticResource PhoneFontSizeLarge}" Foreground="Black"/>
                            <Border Width="400" Height="250">
                                <Border.Background>
                                    <ImageBrush Stretch="Uniform" ImageSource="{Binding Path=MF658370_IMAGE}"/>
                                </Border.Background>
                            </Border>
                        </Grid>

            <phone:LongListSelector x:Name="MainLongListSelector" Background="White" Margin="-20,0,0,0" Height="Auto" Loaded="MainLongListSelectorLoaded" SelectionChanged="MainLongListSelectorSelectionChanged" toolkit:TiltEffect.IsTiltEnabled="True">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="12,2,0,4" Height="Auto" Width="Auto">
                            <Border Width="126" Height="70">
                                <Border.Background>
                                    <ImageBrush Stretch="Uniform" ImageSource="{Binding Path=MF12670_IMAGE}"/>
                                </Border.Background>
                            </Border>
                            <TextBlock x:Name="textBlock1" Text="{Binding Path=TITLE}" Margin="10,0" FontSize="{StaticResource PhoneFontSizeMedium}" Foreground="Black" />
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>

        </StackPanel>
    </ScrollViewer>
</phone:PanoramaItem>

这是MAINPAGE.XAML.CS

private void MainLongListSelectorLoaded(object sender, RoutedEventArgs e)
{
    const string url = "MY_WEB_API_URL";
    var hWebRequest = (HttpWebRequest)WebRequest.Create(url);
    hWebRequest.Method = "GET";
    hWebRequest.BeginGetResponse(MainLongListSelectorLoadCompleted, hWebRequest);
}

private void MainLongListSelectorLoadCompleted(IAsyncResult arr)
{
    var request = (HttpWebRequest)arr.AsyncState;
    var response = (HttpWebResponse)request.EndGetResponse(arr);
    using (var streamReader = new StreamReader(response.GetResponseStream()))
    {
        _json = streamReader.ReadToEnd();
        Headlines = JsonConvert.DeserializeObject<FanatikApiAll.RootObject>(_json);
    }
    Deployment.Current.Dispatcher.BeginInvoke(() =>
    {
        MainLongListSelector.ItemsSource = Headlines.Headline;
    });

    Deployment.Current.Dispatcher.BeginInvoke(() =>
    {
        HeadNew.DataContext = Headlines.Headline;
    });
}

1 个答案:

答案 0 :(得分:1)

由于HeadNew不是列表控件,因此您只需要指定所需的项目,而不是整个列表。例如,要获得第一项(如果有):

Deployment.Current.Dispatcher.BeginInvoke(() =>
{
    HeadNew.DataContext = Headlines.Headline.FirstOrDefault();
});