Windows phone 7 绑定到itemsSource后,LongListSelector不会刷新 从网络获取数据并显示 我使用一个LongListSelector(articleList)来显示数据,WebAPI可以工作,已经通过设置断点在VisualStudio中进行了验证 [问题]将ItemSource设置为viewModel.ArticleCollection,从web api检索数据后,LongListSelector不会刷新 但是,在将OnNavigatedTo中的代码编码到MainPage.xaml.cs中的方法之后,从“_viewModel.LoadPage(_searchTerm,_pageNumber++)”到“articlist.ItemsSource=wait CollectionHttpClient.GetAllArticlesAsync()”,它将显示web api数据

Windows phone 7 绑定到itemsSource后,LongListSelector不会刷新 从网络获取数据并显示 我使用一个LongListSelector(articleList)来显示数据,WebAPI可以工作,已经通过设置断点在VisualStudio中进行了验证 [问题]将ItemSource设置为viewModel.ArticleCollection,从web api检索数据后,LongListSelector不会刷新 但是,在将OnNavigatedTo中的代码编码到MainPage.xaml.cs中的方法之后,从“_viewModel.LoadPage(_searchTerm,_pageNumber++)”到“articlist.ItemsSource=wait CollectionHttpClient.GetAllArticlesAsync()”,它将显示web api数据,windows-phone-7,windows-phone-8,windows-phone,async-await,longlistselector,Windows Phone 7,Windows Phone 8,Windows Phone,Async Await,Longlistselector,有人能帮我吗?提前谢谢 MainPage.xaml <phone:PhoneApplicationPage x:Class="CollectionApp.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Ph

有人能帮我吗?提前谢谢

MainPage.xaml

<phone:PhoneApplicationPage
x:Class="CollectionApp.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:vm="clr-namespace:CollectionApp.ViewModels"
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">

<phone:PhoneApplicationPage.Resources>

    <vm:ArticleListViewModel x:Key="viewModel"/>

    <DataTemplate x:Key="ResultItemTemplate">
        <Grid Margin="0,6,0,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Rectangle Fill="Gray" Height="50" Width="50" Grid.Row="0" Grid.Column="0" 
                     VerticalAlignment="Top" Margin="0,7,7,0"
                   Grid.RowSpan="2">

            </Rectangle>
            <Image Height="50" Width="50" Grid.Row="0" Grid.Column="0" 
                     VerticalAlignment="Top" Margin="0,7,7,0"
                   Grid.RowSpan="2">
                <Image.Source>
                    <BitmapImage UriSource="{Binding ImagePath}"
                                 CreateOptions="BackgroundCreation"/>
                </Image.Source>
            </Image>
            <TextBlock Text="{Binding Subject}" Grid.Row="0" Grid.Column="1"
                             Foreground="{StaticResource PhoneAccentBrush}" VerticalAlignment="Top"/>

            <TextBlock Text="{Binding Words}" TextWrapping="Wrap"
                           Grid.Row="1" Grid.Column="1"
                           VerticalAlignment="Top"
                           />

        </Grid>
    </DataTemplate>

</phone:PhoneApplicationPage.Resources>

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">

    <!-- LOCALIZATION NOTE:
        To localize the displayed strings copy their values to appropriately named
        keys in the app's neutral language resource file (AppResources.resx) then
        replace the hard-coded text value between the attributes' quotation marks
        with the binding clause whose path points to that string name.

        For example:

            Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}"

        This binding points to the template's string resource named "ApplicationTitle".

        Adding supported languages in the Project Properties tab will create a
        new resx file per language that can carry the translated values of your
        UI strings. The binding in these examples will cause the value of the
        attributes to be drawn from the .resx file that matches the
        CurrentUICulture of the app at run time.
     -->

    <!--Pivot Control-->
    <phone:Pivot Title="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}">
        <!--Pivot item one-->
        <phone:PivotItem Header="Articles">
            <!--Double line list with text wrapping-->
            <phone:LongListSelector 
                x:Name="articleList"
                Grid.Row="1"   
                Margin="0,0,-12,0" 
                DataContext="{StaticResource viewModel}"
                ItemTemplate="{StaticResource ResultItemTemplate}"   
                ItemsSource="{Binding viewModel.ArticleCollection}"
                >

            </phone:LongListSelector>
        </phone:PivotItem>

        <!--Pivot item two-->
        <phone:PivotItem Header="second">
            <!--Double line list no text wrapping-->
            <phone:LongListSelector Margin="0,0,-12,0" ItemsSource="{Binding Items}">
                <phone:LongListSelector.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="0,0,0,17">
                                <TextBlock Text="{Binding LineOne}" TextWrapping="NoWrap" Margin="12,0,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                <TextBlock Text="{Binding LineThree}" TextWrapping="NoWrap" Margin="12,-6,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            </StackPanel>
                        </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
        </phone:PivotItem>
    </phone:Pivot>

    <!--Uncomment to see an alignment grid to help ensure your controls are
        aligned on common boundaries.  The image has a top margin of -32px to
        account for the System Tray. Set this to 0 (or remove the margin altogether)
        if the System Tray is hidden.

        Before shipping remove this XAML and the image itself.-->
    <!--<Image Source="/Assets/AlignmentGrid.png" VerticalAlignment="Top" Height="800" Width="480" Margin="0,-32,0,0" Grid.Row="0" IsHitTestVisible="False" />-->
</Grid>

</phone:PhoneApplicationPage>
ArticleListViewModel.cs

using CollectionApp.Models;
using CollectionApp.Network;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CollectionApp.ViewModels
{
    public class ArticleListViewModel : INotifyPropertyChanged
    {
        private bool _isLoading = false;

        public bool IsLoading
        {
            get
            {
                return _isLoading;
            }
            set
            {
                _isLoading = value;
                NotifyPropertyChanged("IsLoading");
            }
        }

        public ArticleListViewModel()
        {
            this.ArticleCollection = new ObservableCollection<Article>();
            this.IsLoading = false;
        }


        public ObservableCollection<Article> ArticleCollection
        {
            get;
            private set;
        }

        public void LoadPage(string searchTerm, int pageNumber)
        {
            if (pageNumber == 1)
            {
                this.ArticleCollection.Clear();
            }

            IsLoading = true;
            ReadArticleList();

        }

        public async void ReadArticleList()
        {
            try
            {
                if (CollectionHttpClient.IsDirty)
                {
                    ArticleCollection = await CollectionHttpClient.GetAllArticlesAsync();
                    IsLoading = false;

                }

            }
            catch
            {                
                MessageBox.Show("sorry, no data.");
                IsLoading = false;
            }

        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }
}
使用CollectionApp.Models;
使用CollectionApp.网络;
使用制度;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
命名空间CollectionApp.ViewModels
{
公共类ArticleListViewModel:INotifyPropertyChanged
{
私有bool_isLoading=false;
公共布尔孤岛加载
{
收到
{
返回-卸载;
}
设置
{
_isLoading=值;
NotifyPropertyChanged(“IsLoading”);
}
}
public ArticleListViewModel()
{
this.ArticleCollection=新的ObservableCollection();
this.IsLoading=false;
}
公共可观测集合文章集合
{
收到
私人设置;
}
公共无效加载页(字符串搜索项,整数页码)
{
如果(页码==1)
{
此.ArticleCollection.Clear();
}
IsLoading=true;
ReadArticleList();
}
公共异步无效ReadArticleList()
{
尝试
{
if(CollectionHttpClient.IsDirty)
{
ArticleCollection=await CollectionHttpClient.GetAllArticlesAsync();
IsLoading=false;
}
}
接住
{                
Show(“对不起,没有数据”);
IsLoading=false;
}
}
公共事件属性更改事件处理程序属性更改;
私有void NotifyPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(null!=处理程序)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
}

我怀疑是因为在设置
ArticleCollection
时没有引发属性更改事件。这就是为什么
LongListSelector
没有刷新<代码>ObservableCollection仅在从集合中添加或删除项目时通知UI。但是当
observateCollection
属性自身被分配给新的
observateCollection
对象时,您需要手动引发属性更改事件。尝试按如下所示进行更改,然后查看它现在是否工作:

private ObservableCollection<Article> _articleCollection;
public ObservableCollection<Article> ArticleCollection
{
    get
    {
        return _articleCollection;
    }
    set
    {
        _articleCollection = value;
        NotifyPropertyChanged("ArticleCollection");
    }
}

相反,请清除集合并将每个新项目添加到集合中。

最后,我完成了此操作。我每次都刷新文章集,这就是原因。 每次将项分配给UI容器时都会激发ItemImplemented事件,直到没有新项分配给UI容器为止

最后一次,web api没有返回任何结果,因为最后一页上没有结果。这次我将no result分配给ArticleCollection。这就是longlistselector不显示任何内容的原因

改变方法

public async void ReadArticleList()
        {
            try
            {
                if (CollectionHttpClient.IsDirty)
                {
                    ArticleCollection = await CollectionHttpClient.GetAllArticlesAsync();
                    IsLoading = false;

                }

            }
            catch
            {                
                MessageBox.Show("sorry, no data.");
                IsLoading = false;
            }

        }
进入:

private async void ReadArticleList(int页码)
{
尝试
{
List articleList=新列表();
articleList=await CollectionHttpClient.GetArticlesByPageAsync(页码);
foreach(articleList中的var项)
{
此.ArticleCollection.Add(项目);
}
IsLoading=false;
}
接住
{
Show(“对不起,没有数据”);
IsLoading=false;
}
}

我也有同样的问题,但我不明白你的解决方案@max,你能改进一下吗。我试图逐个重新加载项目,但没有成功。请改进您的解决方案,手动属性onchanged call如何工作?我调用它,但它不会刷新LLS上的内容,它必须是一个可观察的连接,或者可以是任何IEnumerable@哈
ArticleCollection = await CollectionHttpClient.GetAllArticlesAsync();
public async void ReadArticleList()
        {
            try
            {
                if (CollectionHttpClient.IsDirty)
                {
                    ArticleCollection = await CollectionHttpClient.GetAllArticlesAsync();
                    IsLoading = false;

                }

            }
            catch
            {                
                MessageBox.Show("sorry, no data.");
                IsLoading = false;
            }

        }
private async void ReadArticleList(int pageNumber)
        {
            try
            {

                List<Article> articleList = new List<Article>();
                articleList = await CollectionHttpClient.GetArticlesByPageAsync(pageNumber);

                foreach (var item in articleList)
                {
                    this.ArticleCollection.Add(item);
                }

                IsLoading = false;


            }
            catch
            {
                MessageBox.Show("sorry, no data.");
                IsLoading = false;
            }

        }