Longlist不会在加载时更新

时间:2013-07-20 18:23:48

标签: windows-phone-8 longlistselector

我在使用长列表选择器加载我的数据时遇到一些问题,而我无法找到解决此问题的方法。

这是我的xaml:

<phone:LongListSelector x:Name="animeList" 
                        Margin="0,0,-12,0" 
                        ItemsSource="{Binding Animes}"
                        Tap="AnimeList_OnTap">
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="0,0,0,17">
                <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}">
                    <toolkit:ContextMenuService.ContextMenu>
                        <toolkit:ContextMenu IsZoomEnabled="false">
                            <toolkit:MenuItem Header="Add as favorit" Click="AddFavorite" />
                        </toolkit:ContextMenu>
                    </toolkit:ContextMenuService.ContextMenu>
                </TextBlock>
            </StackPanel>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

我的观点模型很简单:

public ObservableCollection<AnimeItemViewModel> _animes { get; set; }
public ObservableCollection<AnimeItemViewModel> Animes
{
    get { return _animes; }
    set
    {
        if (value != _animes)
        {
            _animes = value;
            NotifyPropertyChanged("Animes");
        }
    }
}

我如何加载数据:

public MainPage()
{
    InitializeComponent();

    DataContext = App.ViewModel;
    this.Loaded += OnLoaded;
}

private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
    ObservableCollection<AnimeItemViewModel> _animes = new ObservableCollection<AnimeItemViewModel>();
    foreach (var i in App.AnimeList.List)
        _animes.Add(new AnimeItemViewModel() { AId = i.AId, Name = i.Name });
    App.ViewModel.Animes = _animes;
}

最后只是为了表明列表中有数据 enter image description here

更新:我也有一个搜索功能,如果我输入搜索文本将更新长列表,但我出于某种原因无法滚动

private void OnKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        ObservableCollection<AnimeItemViewModel> _animes = new ObservableCollection<AnimeItemViewModel>();
        foreach (var i in App.AnimeList.List)
            if (string.IsNullOrWhiteSpace(SearchTextBox.Text) || i.Name.ToLower().Contains(SearchTextBox.Text.ToLower()))
                _animes.Add(new AnimeItemViewModel() { AId = i.AId, Name = i.Name });
        App.ViewModel.Animes = _animes;
    }
}

1 个答案:

答案 0 :(得分:1)

尝试将加载代码放入OnNavigatedTo。至于滚动问题 - 为StackPanel设置合适的高度。如果有效,请告诉我。