【问题标题】:How to use xaml pivot with async methods如何将 xaml 数据透视与异步方法一起使用
【发布时间】:2019-11-05 09:18:26
【问题描述】:

我正在使用数据透视控件来列出从异步方法获得的视频集合的内容。

我有 2 个枢轴项目,一个用于收藏,其中包含用户将在另一个枢轴中选择的元素,其中列出了所有视频。

现在,我有一个可用的 xaml 文件。文件是这样的

        <Grid.RowDefinitions>
            <RowDefinition Height = "auto"/>
            <RowDefinition Height = "*"/>
        </Grid.RowDefinitions>
        <TextBlock Text="Video" FontSize="30" Margin="10,0,0,0"/>
        <Pivot Grid.Row = "1">

            <PivotItem Header="Favorite Videos">
                <ItemsControl ItemsSource="{x:Bind ViewModel.FavoriteVideos, Mode=OneWay}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate x:DataType="models:VideoModel">
                            <Grid Height="40">
                                <TextBlock VerticalAlignment="Center" Text="{x:Bind  Title}"/>
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </PivotItem>

            <PivotItem Header="All Videos" >
                <ItemsControl ItemsSource="{x:Bind ViewModel.AllVideos, Mode=OneWay}" >
                    <ItemsControl.ItemTemplate>
                        <DataTemplate x:DataType="models:VideoModel">
                            <StackPanel Orientation="Horizontal">
                                <Image Source="{x:Bind Cover}" HorizontalAlignment="Left" Width="100"/>
                                <TextBlock VerticalAlignment="Center" Text="{x:Bind Title}"/>
                                <ToggleSwitch Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center" IsOn="{x:Bind IsFavorite, Mode=TwoWay}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </PivotItem>


        </Pivot>
    </Grid>

ViewModel.AllVideos 背后的代码是这样的:

namespace VideoManager.ViewModels
{
    public class VideoViewModel
    {
        public List<VideoModel> AllVideos = new List<VideoModel>();
        public ObservableCollection<VideoModel> FavoriteVideos = new ObservableCollection<VideoModel>();

        public VideoViewModel()
        {
            InitVideos();

        }

        private async void InitVideos()
        {
            var files = await KnownFolders.VideosLibrary.GetFilesAsync();
            foreach (var file in files)
            {
                if (file != null)
                {
                    var thumbnail = await file.GetThumbnailAsync(ThumbnailMode.VideosView, 50, ThumbnailOptions.ReturnOnlyIfCached);
                    VideoModel video = new VideoModel();
                    video.PropertyChanged += IsFavoritePropertyChanged;
                    video.Title = file.Name;
                    if (thumbnail != null)
                    {
                        BitmapImage bitmapImage = new BitmapImage();
                        InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
                        await RandomAccessStream.CopyAsync(thumbnail, randomAccessStream);
                        randomAccessStream.Seek(0);
                        await bitmapImage.SetSourceAsync(randomAccessStream);
                        video.Cover = bitmapImage;
                    }

                    AllVideos.Add(video);
                    if (video.IsFavorite)
                    {
                        FavoriteVideos.Add(video);
                    }
                }

            }
        }

        public void IsFavoritePropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            VideoModel toggledVideo = sender as VideoModel;
            if (toggledVideo.IsFavorite)
            {
                FavoriteVideos.Add(toggledVideo);
            }
            else
            {
                FavoriteVideos.Remove(toggledVideo);
            }
        }
    }

}

它工作正常,但如果我更改 PivotItem 的顺序并将 PivotItem 所有视频放在 PivotItem FavoriteVideos 之前,则屏幕上不会出现任何内容。我想这与异步方法有关。

所以我的问题是如何使这项工作无论 PivotItem 的顺序是什么?

谢谢

【问题讨论】:

    标签: xaml data-binding uwp async-await pivot


    【解决方案1】:

    使用ObservableCollection 将解决此问题。 List Class没有实现INotifyPropertyChanged Interface ,更新后不会通知UI。

    public ObservableCollection<VideoModel> AllVideos = new ObservableCollection<VideoModel>();
    

    【讨论】:

    • 有效!非常感谢。它也让我能够解决一些其他问题。
    猜你喜欢
    • 2020-09-22
    • 1970-01-01
    • 2021-06-05
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 2013-09-11
    相关资源
    最近更新 更多