【问题标题】:UWP - Display placeholder on gridview image binding while still loadingUWP - 在仍然加载时在 gridview 图像绑定上显示占位符
【发布时间】:2018-02-01 20:38:33
【问题描述】:

我在 gridview 上有一个图像绑定,下面是 XAML:

<Border x:Name="coverBox" Grid.Row="0" Margin="5,10,0,0" Width="230" Height="170" VerticalAlignment="Top" HorizontalAlignment="Left" BorderBrush="Gray" BorderThickness="1" Background="{x:Null}">
                                <Image x:Name="cover" Source="{Binding Thumbnail}" HorizontalAlignment="Center" Stretch="Uniform" AutomationProperties.Name="{Binding Title}" Width="230" Height="170"/>
</Border>

代码:

string urlPath1 = "http://..../API/sekolah";
                            var httpClient1 = new HttpClient(new HttpClientHandler());

                            var values1 = new List<KeyValuePair<string, string>>
                            {
                                new KeyValuePair<string, string>("api_key", "...."),
                                new KeyValuePair<string, string>("limit", limit++.ToString()),
                                new KeyValuePair<string, string>("hal", noHal++.ToString())
                            };

                            var response1 = await httpClient1.PostAsync(urlPath1, new FormUrlEncodedContent(values1));
                            response1.EnsureSuccessStatusCode();

                            if (!response1.IsSuccessStatusCode)
                            {
                                busyIndicator.IsActive = false;
                                RequestException();
                            }
                            string jsonText1 = await response1.Content.ReadAsStringAsync();
                            JsonObject jsonObject1 = JsonObject.Parse(jsonText1);
                            JsonArray jsonData1 = jsonObject1["data"].GetArray();

                            foreach (JsonValue groupValue1 in jsonData1)
                            {
                                JsonObject groupObject2 = groupValue1.GetObject();
                                string thumbnail = groupObject2.ContainsKey("thumbnail") && groupObject2["thumbnail"] != null ? groupObject2["thumbnail"].GetString() : string.Empty;

                                ListingClass file1 = new ListingClass();
                                file1.Thumbnail = thumbnail;

                                listingDatasourceDetail.Add(file1);
                            }
                            listingGridView.ItemsSource = listingDatasourceDetail;

我想在加载的同时从 JSON 中获取图像数据,然后首先显示 paceholder images/SEKOLAH-place-holder-600.png

如何在加载时添加占位符?

【问题讨论】:

    标签: c# json image gridview uwp


    【解决方案1】:

    将您的cover 图像包装在一个网格中,然后创建另一个图像以显示占位符 png 并将其放在cover 后面。

    【讨论】:

    • 我不想使用 2 张图片,因为图片中有一些背景是反式的。所以占位符仍然可见
    • 您可以订阅 ImageOpened 事件并将占位符图像的 Visibility 设置为 Collapsed。
    【解决方案2】:

    尝试将Loading 事件添加到图像:

        private void myimage_Loading(FrameworkElement sender, object args)
            {
                Image myimage = (Image)sender;
                myimage.Source = new BitmapImage(new Uri("ms-appx:///images/SEKOLAH-place-holder-600.png"));
            }
    

    【讨论】:

    • 代码不能用,因为图片在gridview所以没检测到
    • 我试过了。但是即使加载完成,显示的图像也只是一个占位符(图像没有出现)。
    • 哈立德:你能帮帮我吗?
    猜你喜欢
    • 2021-08-07
    • 2015-01-04
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2013-08-27
    • 2016-11-11
    • 2016-07-20
    相关资源
    最近更新 更多