我有一个 UWP 项目想要读取 StorageFolder VideosLibrary 并在带有缩略图的视图中显示 mp4 文件列表。
使用 MVVM ligth 工具包,我用 xaml 设置了这 4 只苍蝇。Xaml 正在使用 UWP 社区工具包包装面板。
1)ViewModelLocator.cs
namespace UWP.ViewModels
{
/// <summary>
/// This class contains static reference to all the view models in the
/// application and provides an entry point for the bindings.
/// </summary>
class ViewModelLocator
{
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
if (ViewModelBase.IsInDesignModeStatic)
{
// Create design time view services and models
}
else
{
// Create run Time view services and models
}
//Register services used here
SimpleIoc.Default.Register<VideoListModel>();
}
public VideoListModel VideoListModel
{
get { return ServiceLocator.Current.GetInstance<VideoListModel>();
}
}
}
2) 视频列表项.cs
namespace UWP.Models
{
class VideoListItem : ViewModelBase
{
public string VideoName { get; set; }
public string Author { get; set; }
public Uri Vid_url { get; set; }
public BitmapImage Image { get; set; }
public VideoListItem(string videoname,string author,Uri url, BitmapImage img)
{
this.VideoName = videoname;
this.Author = author;
this.Vid_url = url;
this.Image = img;
}
}
}
3) 视频列表模型.cs
namespace UWP.ViewModels
{
class VideoListModel : ViewModelBase
{
public ObservableCollection<VideoListItem> VideoItems { get; set; }
private VideoListItem videoItems;
public VideoListModel()
{
}
public async static Task<List<VideoListItem>> GetVideoItem()
{
List<VideoListItem> videoItems = new List<VideoListItem>();
StorageFolder videos_folder = await KnownFolders.VideosLibrary.CreateFolderAsync("Videos");
var queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, new[] { ".mp4" });
var videos = await videos_folder.CreateFileQueryWithOptions(queryOptions).GetFilesAsync();
foreach (var video in videos)
{
//Debug.WriteLine(video.Name);
//videoItems.Add(new VideoListItem());
var bitmap = new BitmapImage();
var thumbnail = await video.GetThumbnailAsync(ThumbnailMode.SingleItem);
await bitmap.SetSourceAsync(thumbnail);
videoItems.Add(new VideoListItem(video.DisplayName, "", new Uri(video.Path),bitmap));
}
//foreach(var video in videoItems)
//{
// Debug.WriteLine("Name:{0} , Author:{1}, Uri:{2}, Bitmap:{3}", video.VideoName, video.Author, video.Vid_url, video.Image.UriSource);
//}
return videoItems;
}
}
}
4) 视频.xml
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWP.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
x:Class="UWP.Views.Video"
mc:Ignorable="d"
NavigationCacheMode="Enabled"
DataContext="{Binding Source={StaticResource ViewModelLocator},Path=VideoListModel}">
<!--NavigationCacheMode Enable for the page state save-->
<Page.Resources>
<DataTemplate x:Key="VideoTemplate">
<Grid Width="{Binding Width}"
Height="{Binding Height}"
Margin="2">
<Image HorizontalAlignment="Center"
Stretch="UniformToFill"
Source="{Binding Image}" />
<TextBlock Text="{Binding VideoName}"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Author" />
<TextBlock Text="{Binding Author}" />
</StackPanel>
</Grid>
</DataTemplate>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView Name="VideosListWrapPanal"
ItemTemplate="{StaticResource VideoTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Controls:WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ListView>
</Grid>
</Page>
我想在我的 VideoListModel 中为构造函数做类似下面的事情。
public async MainViewModel()
{
VideoItems = new ObservableCollection<MainMenuItem>(await GetVideoItem());
}
如何以异步方式完成此初始化?为了获取缩略图,我创建了 GetVideoItem() 方法,但我找不到在构造函数中异步调用 GetVideoItem 的方法。有谁知道如何解决这个任务?