c# – 如何在UWP MVVM模型中使我的构造函数异步? (MVVM Lighttoolkit)

前端之家收集整理的这篇文章主要介绍了c# – 如何在UWP MVVM模型中使我的构造函数异步? (MVVM Lighttoolkit)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个想要读取StorageFolder VideosLibrary的UWP项目,并在带缩略图的Views中显示mp4文件列表.

使用MVVM ligth工具包,我已经使用xaml设置了这4只苍蝇.
Xaml正在使用UWP社区工具箱包装面板.

1)viewmodelLocator.cs

  1. namespace UWP.viewmodels
  2. {
  3. /// <summary>
  4. /// This class contains static reference to all the view models in the
  5. /// application and provides an entry point for the bindings.
  6. /// </summary>
  7.  
  8. class viewmodelLocator
  9. {
  10. /// <summary>
  11. /// Initializes a new instance of the viewmodelLocator class.
  12. /// </summary>
  13. public viewmodelLocator()
  14. {
  15. ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
  16. if (viewmodelBase.IsInDesignModeStatic)
  17. {
  18. // Create design time view services and models
  19. }
  20. else
  21. {
  22. // Create run Time view services and models
  23. }
  24. //Register services used here
  25.  
  26. SimpleIoc.Default.Register<VideoListModel>();
  27. }
  28.  
  29.  
  30. public VideoListModel VideoListModel
  31. {
  32. get { return ServiceLocator.Current.GetInstance<VideoListModel>();
  33. }
  34. }
  35. }

2)VideoListItem.cs

  1. namespace UWP.Models
  2. {
  3. class VideoListItem : viewmodelBase
  4. {
  5. public string VideoName { get; set; }
  6. public string Author { get; set; }
  7. public Uri Vid_url { get; set; }
  8. public BitmapImage Image { get; set; }
  9.  
  10. public VideoListItem(string videoname,string author,Uri url,BitmapImage img)
  11. {
  12. this.VideoName = videoname;
  13. this.Author = author;
  14. this.Vid_url = url;
  15. this.Image = img;
  16. }
  17. }
  18. }

3)VideoListModel.cs

  1. namespace UWP.viewmodels
  2. {
  3. class VideoListModel : viewmodelBase
  4. {
  5. public ObservableCollection<VideoListItem> VideoItems { get; set; }
  6.  
  7. private VideoListItem videoItems;
  8.  
  9. public VideoListModel()
  10. {
  11.  
  12. }
  13.  
  14. public async static Task<List<VideoListItem>> GetVideoItem()
  15. {
  16. List<VideoListItem> videoItems = new List<VideoListItem>();
  17. StorageFolder videos_folder = await KnownFolders.VideosLibrary.CreateFolderAsync("Videos");
  18. var queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery,new[] { ".mp4" });
  19. var videos = await videos_folder.CreateFileQueryWithOptions(queryOptions).GetFilesAsync();
  20.  
  21.  
  22. foreach (var video in videos)
  23. {
  24. //Debug.WriteLine(video.Name);
  25. //videoItems.Add(new VideoListItem());
  26. var bitmap = new BitmapImage();
  27. var thumbnail = await video.GetThumbnailAsync(ThumbnailMode.SingleItem);
  28. await bitmap.SetSourceAsync(thumbnail);
  29. videoItems.Add(new VideoListItem(video.DisplayName,"",new Uri(video.Path),bitmap));
  30.  
  31. }
  32.  
  33. //foreach(var video in videoItems)
  34. //{
  35. // Debug.WriteLine("Name:{0},Author:{1},Uri:{2},Bitmap:{3}",video.VideoName,video.Author,video.Vid_url,video.Image.UriSource);
  36. //}
  37.  
  38.  
  39. return videoItems;
  40. }
  41.  
  42.  
  43. }
  44. }

4)Video.xaml

  1. <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3. xmlns:local="using:UWP.Views"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:Controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
  7. x:Class="UWP.Views.Video"
  8. mc:Ignorable="d"
  9. NavigationCacheMode="Enabled"
  10. DataContext="{Binding Source={StaticResource viewmodelLocator},Path=VideoListModel}">
  11. <!--NavigationCacheMode Enable for the page state save-->
  12. <Page.Resources>
  13. <DataTemplate x:Key="VideoTemplate">
  14. <Grid Width="{Binding Width}"
  15. Height="{Binding Height}"
  16. Margin="2">
  17. <Image HorizontalAlignment="Center"
  18. Stretch="UniformToFill"
  19. Source="{Binding Image}" />
  20. <TextBlock Text="{Binding VideoName}"/>
  21. <StackPanel Orientation="Horizontal">
  22. <TextBlock Text="Author" />
  23. <TextBlock Text="{Binding Author}" />
  24. </StackPanel>
  25. </Grid>
  26. </DataTemplate>
  27. </Page.Resources>
  28.  
  29. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  30. <ListView Name="VideosListWrapPanal"
  31. ItemTemplate="{StaticResource VideoTemplate}">
  32. <ItemsControl.ItemsPanel>
  33. <ItemsPanelTemplate>
  34. <Controls:WrapPanel />
  35. </ItemsPanelTemplate>
  36. </ItemsControl.ItemsPanel>
  37. </ListView>
  38.  
  39. </Grid>
  40. </Page>

我想在我的VideoListModel中为构造函数执行类似下面的操作.

  1. public async Mainviewmodel()
  2. {
  3. VideoItems = new ObservableCollection<MainMenuItem>(await GetVideoItem());
  4.  
  5. }

如何以异步方式完成此初始化?
为了获得缩略图,我创建了GetVideoItem()的方法,
但我找不到在构造函数中异步调用GetVideoItem的方法.
有谁知道如何解决这个任务?

解决方法

我建议使用异步任务通知程序,如我在 async MVVM data binding上的文章中所述.

例如,使用this helper library的NotifyTask:

  1. public NotifyTask<List<VideoListItem>> VideoItems { get; }
  2.  
  3. public VideoListModel(IKnownFolderReader knownFolder)
  4. {
  5. _knownFolder = knownFolder;
  6. VideoItems = NotifyTask.Create(() => _knownFolder.GetData());
  7. }

然后,您的数据绑定将从ItemsSource =“{Binding VideoItems}”更改为ItemsSource =“{Binding VideoItems.Result}”.此外,VideoItems还有其他几个属性,如IsNotCompleted和IsFaulted,因此您的数据绑定可以根据任务的状态显示/隐藏元素.

这种方法避免了细微的problems with Resultproblems with ContinueWith.

猜你在找的C#相关文章