【发布时间】:2017-09-12 05:40:10
【问题描述】:
我有一个 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) VideoListItem.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) VideoListModel.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) 视频.xaml
<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 的方法。 有谁知道如何解决这个任务?
【问题讨论】:
-
这是您需要阅读的文章:msdn.microsoft.com/en-gb/magazine/…
-
你为什么不使用委托代替。不建议在构造函数中使用异步方法或函数。
-
抱歉我对委托缺乏了解,我使用了异步方法,因为我的
GetVideoItem方法中有CreateFolderAsync方法和GetFilesAsync方法。你能给我一些建议,我该如何修改我的代码? -
如何使用“加载窗口”中的命令并使命令异步。所以它会异步加载数据
-
构造函数不能是异步的。例如,您可以使用 Activated 事件。但是考虑一下后果,用户会在一段时间内看一个没有内容的窗口。而且你必须确保他不能做任何需要 VideoItems 成员有效的事情。您真的想在窗口变得可见之前 执行此操作。考虑一个异步工厂方法。
标签: c# asynchronous uwp mvvm-light windows-community-toolkit