【问题标题】:Unable to bind data from async method to a listview无法将数据从异步方法绑定到列表视图
【发布时间】:2019-11-04 02:27:24
【问题描述】:

总的来说,我是 UWP 和 C# 的新手。我正在尝试从我的视频库中浏览文件并将数据绑定到列表视图控件。一切运行良好,我已经检查了所有变量,并且它们确实包含正确的信息,但是列表视图中没有填充任何内容。你能给我一个关于如何使它工作的提示吗?谢谢你,如果我的代码不是最先进的,请提前道歉,我正在学习;)

这是我的主要课程:

namespace RedMedia
{
    public sealed partial class MainPage : Page
    {
        private readonly List<Video> Videos;
        public string resultat;
        int i;
        public MainPage()
        {  
            this.InitializeComponent();
            Videos = new List<Video>();
            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);
                    Video video = new Video();
                    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;
                    }

                    Videos.Add(video);
               }

这是我的课堂视频:

namespace RedMedia.Modeles
    {
        class Video : 
        {
            public BitmapImage Cover { get; set; }
            public uint Bitrate { get; set; }
            public List<string> Directors { get; set; }
            public List<string> Producers { get; set; }
            public TimeSpan Duration { get; set; }
            public uint Height { get; set; }
            public List<string> Keywords { get; set; }
            public uint Rating { get; set; }
            public string Subtitle { get; set; }
            public string Title { get; set; }
            public string Publisher { get; set; }
            public uint Year { get; set; }
            public string Path { get; set; }
            public string category { get; set; }

            public Video()
            {
                //some code to do
            }
        }

    }

最后是我的 main.xaml

    <Page
        x:Class="RedMedia.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:Data="using:RedMedia.Modeles"
        mc:Ignorable="d" >

        <Page.Resources>
            <DataTemplate x:DataType="Data:Video" x:Key="VideoDataTemplate">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                    <TextBlock FontSize="16" Text="{x:Bind Title}" HorizontalAlignment="Center"/>
                </StackPanel>
            </DataTemplate>
        </Page.Resources>

        <Grid Background="Orange" Margin="0,20,20,0">
            <ListView  Name="lstview1" ItemsSource="{x:Bind Videos}"
                      ItemTemplate="{StaticResource VideoDataTemplate}">
            </ListView>
        </Grid>

    </Page>

任何帮助将不胜感激

【问题讨论】:

    标签: c# xaml uwp binding


    【解决方案1】:

    首先,公开所有课程。 然后,下一步,您的视频集合/列表应按如下方式定义和实施:

    public List<Video> Videos { get; set; }
    
    

    注意:为了遵循 MVVM 或您使用的任何模式,我建议重构您的代码,以便您使用绑定到您的视图模型,而不是代码隐藏。 在这种情况下,您还可以实现 INotifyPropertyChanged 并在定义 VM 属性时使用它。 那么您的 VM 属性将如下所示:

    private List<Video> _videos = new List<Video>();
    
    public List<Video> Videos 
    {
       get { return _videos; }
       set 
       {
          _videos = value; 
          OnPropertyChanged();
       }
    }
    
    

    如果您决定使用此链接,可能会有所帮助:https://blogs.msdn.microsoft.com/msgulfcommunity/2013/03/13/understanding-the-basics-of-mvvm-design-pattern/

    【讨论】:

    • 在这种情况下我会使用 ObservableCollection 而不是 List 因为它实现了 INotifyCollectionChanged
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 2013-04-12
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 2018-05-20
    相关资源
    最近更新 更多