示例#1
0
 static async ValueTask <ImageSource> FetchImageAsync(Uri url)
 {
     var data = await Reddit.FetchImageAsync(url);
     return(new StreamImageSource
     {
         Stream = _ => Task.FromResult((Stream) new MemoryStream(data))
     });
 }
示例#2
0
 static async ValueTask <ImageSource?> FetchImageAsync(Uri url)
 {
     var raStream = new InMemoryRandomAccessStream();
     await raStream.WriteAsync((await Reddit.FetchImageAsync(url)).AsBuffer());
     raStream.Seek(0);
     var bitmap = new BitmapImage();
     await bitmap.SetSourceAsync(raStream);
     return(bitmap);
 }
示例#3
0
        public MainWindowViewModel()
        {
            this.Items         = new ObservableCollection <ItemViewModel>();
            this.IndicatorPile = PileFactory.Create <Panel>();

            // A handler for window loaded
            this.Ready = Command.Factory.CreateSync(() =>
            {
                this.IsEnabled = true;
            });

            // A handler for fetch button
            this.Fetch = CommandFactory.Create(async() =>
            {
                // Disable button
                this.IsEnabled = false;

                // Temporary rent Grid children accessor
                await this.IndicatorPile.RentAsync(async indicator =>
                {
                    // Show WaitingBlock control
                    var waitingBlock = new WaitingBlock();
                    indicator.Children.Add(waitingBlock);

                    try
                    {
                        // Uses Reddit API
                        var reddits = await Reddit.FetchNewPostsAsync("r/aww");

                        this.Items.Clear();

                        static async ValueTask <ImageSource?> FetchImageAsync(Uri url)
                        {
                            try
                            {
                                var bitmap = new WriteableBitmap(
                                    BitmapFrame.Create(new MemoryStream(await Reddit.FetchImageAsync(url))));
                                bitmap.Freeze();
                                return(bitmap);
                            }
                            // Some images will cause decoding error by WPF's BitmapFrame, so ignoring it.
                            catch (FileFormatException)
                            {
                                return(null);
                            }
                        }

                        foreach (var reddit in reddits)
                        {
                            this.Items.Add(new ItemViewModel
                            {
                                Title = reddit.Title,
                                Score = reddit.Score,
                                Image = await FetchImageAsync(reddit.Url)
                            });
                        }
                    }
示例#4
0
 static async ValueTask <ImageSource?> FetchImageAsync(Uri url)
 {
     try
     {
         var bitmap = new WriteableBitmap(
             BitmapFrame.Create(new MemoryStream(await Reddit.FetchImageAsync(url))));
         bitmap.Freeze();
         return(bitmap);
     }
     // Some images will cause decoding error by WPF's BitmapFrame, so ignoring it.
     catch (FileFormatException)
     {
         return(null);
     }
 }
示例#5
0
        private async void fetchButton_Click(object sender, EventArgs e)
        {
            this.Enabled = false;

            try
            {
                // Uses Reddit API
                var reddits = await Reddit.FetchNewPostsAsync("r/aww");

                this.itemsList.Items.Clear();
                this.imageList.Images.Clear();

                foreach (var reddit in reddits)
                {
                    this.imageList.Images.Add(await Reddit.FetchImageAsync(reddit.Url));
                    this.itemsList.Items.Add(reddit.Title, this.imageList.Images.Count - 1);
                }
            }
            finally
            {
                this.Enabled = true;
            }
        }
示例#6
0
 static async ValueTask <Bitmap?> FetchImageAsync(Uri url) =>
 new Bitmap(new MemoryStream(await Reddit.FetchImageAsync(url)));
示例#7
0
 static async ValueTask <ImageSource?> FetchImageAsync(Uri url)
 {
     var bitmap = new BitmapImage();
     bitmap.SetSource(new MemoryStream(await Reddit.FetchImageAsync(url)));
     return(bitmap);
 }