Xamarin.Forms 帮助从 Url/Uri 直接从视图模型加载图像

Xamarin.Forms Help loading Images from Url/Uri directly from viewmodel

我目前正在 Xamarin.Forms 开发跨平台应用程序(iOS 和 Android)我目前的目标是为多种服务开发带有图像的滑块。

类似于:

图像 |图片 |图片 |图片

标签 |标签 |标签 |标签

可向两侧滚动。

为此我创建了:

-服务 class

string ID
string Name
string ServiceType
ImageSource ImgUrl 

-一个视图模型(HLandingVM) 在这里我准备了列表对象并将它们加载到页面中

-HLandingPage.xaml 视图

-HLandingPage.cs加载viewmodel

主要问题是我确实看到我的标签正确显示并按预期滚动。但是图片根本不显示。

我试过传递给模型: 一个 ImageSource ,图像本身,只为绑定传递一个 Uri。但是图片根本不会显示。

-Class

 `private string id;
    public string Id
    {
        get { return id; }
        set
        {
            id = value;
            OnPropertyChange("Id");
        }
    }

    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            OnPropertyChange("Name");
        }
    }

    private string servicetype;
    public string ServiceType
    {
        get { return servicetype; }
        set
        {
            servicetype = value;
            OnPropertyChange("ServiceType");
        }
    }



    private ImageSource imgUrl;
    public ImageSource ImgUrl 
    {
        get { return imgUrl; }
        set
        {
            imgUrl = value;
            OnPropertyChange("ImgUrl");
        }
    }`

-VIEW

`

<StackLayout Margin="10,0,5,0" WidthRequest="150" HeightRequest="150">
   <Image HorizontalOptions="Start"  WidthRequest="150" HeightRequest="150" >
              <Image.Source>
                  <UriImageSource Uri="{Binding ImgUrl}" />
              </Image.Source>
   </Image>
    <Label Style="{StaticResource BoldLabel}" HorizontalTextAlignment="Center" FontSize="13" LineBreakMode="TailTruncation" Text="{Binding Name}" TextColor="Black"/>
</StackLayout>

`

-VM

添加服务和图像源 `

Services.Add(
new Services
{
Name = "Service1",
ServiceType = "Owner",
ImgUrl = new UriImageSource()
{
Uri= new Uri("https://via.placeholder.com/150 "),
CachingEnabled = false
}
});
Services.Add(
new Services
{
Name = "Service2",
ServiceType = "Owner",
ImgUrl = new UriImageSource()
{
Uri = new Uri("https://via.placeholder.com/100 "),
CachingEnabled = false
}
});
Services.Add(
new Services
{
Name = "Service3",
ServiceType = "Owner",
ImgUrl = new UriImageSource()
{
Uri = new Uri("https://via.placeholder.com/250 "),
CachingEnabled = false
}
}); 

`

-如果 URI returns 为空

,则尝试(不工作)加载资源图像

`

foreach (var service in List)
                {
                    if (service.ImgUrl.IsEmpty)
                    {
                        var assembly = typeof(HLandingPage);
                        service.ImgUrl = ImageSource.FromResource("App.Images.150.png", assembly);
                    }
                    OwnerServices.Add(service);

`

没有触发明显的错误。只是空图片。

首先,您可以简化您的 Image

<Image HorizontalOptions="Start"  WidthRequest="150" HeightRequest="150" Source="{Binding ImgUrl}" />

其次,让你的ImgUrl属性成为string

Services.Add(
  new Services
  {
    Name = "Service1",
    ServiceType = "Owner",
    ImgUrl = "https://via.placeholder.com/150"
  });

最后,请确保您实际上没有在 url

中包含尾随 space

一般我们通常将imgUrl定义为字符串类型,如下

private string imgUrl;
public string ImgUrl 
{
    get { return imgUrl; }
    set
    {
        imgUrl = value;
        OnPropertyChange("ImgUrl");
    }
}

并像这样绑定:

<Image Source="{Binding ImgUrl}" />

当然,当你初始化ImgUrl的值时。它也应该是 string 类型。

并且有一个显示图片的示例来自网络,您可以参考一下。 https://github.com/xamarin/xamarin-forms-samples/tree/master/GetStarted/Tutorials/ListViewTutorial

谢谢大家的帮助!

确实,图像的字符串对源绑定起到了作用。

我注意到的另一个我遗漏的细节 在 xaml.cs

中设置 ItemsSource
 protected override void OnAppearing()
        {
            base.OnAppearing();
            viewModel.UpdateServices();
            OwnerServicesSlider.ItemsSource = viewModel.OwnerServices;
        }

此图片开始显示在第一个项目上后

   private string imgUrl;
        public string ImgUrl
        {
            get { return imgUrl; }
            set
            {
                imgUrl = value;
                OnPropertyChange("ImgUrl");
            }
        }

我还将 ItemSource 添加到 xaml

<controls:HorizontalScrollView HeightRequest="200" 
                                   SelectedCommand="{Binding OpenPageCommand}"
                                   Orientation="Horizontal"  
                                   ItemsSource="{Binding OwnerServices}"
                                   x:Name="OwnerServicesSlider">
                <controls:HorizontalScrollView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Margin="10,0,5,0" WidthRequest="150" HeightRequest="150">
                                <Image HorizontalOptions="Start"  Source="{Binding ImgUrl}" WidthRequest="150" HeightRequest="150"/>
                                <Label Style="{StaticResource BoldLabel}" HorizontalTextAlignment="Center" FontSize="13" LineBreakMode="TailTruncation" Text="{Binding Name}" TextColor="Black"/>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </controls:HorizontalScrollView.ItemTemplate>
            </controls:HorizontalScrollView>

我用不同的链接填充我的列表

 Services.Add(
               new Services
               {
                   Name = "Service1",
                   ServiceType = "Owner",
                   ImgUrl = "http://lorempixel.com/150/150"

               });
            Services.Add(
                new Services
                {
                    Name = "Service2",
                    ServiceType = "Owner",
                    ImgUrl = "https://via.placeholder.com/150"

                }); ;
            Services.Add(
               new Services
               {
                   Name = "Service3",
                   ServiceType = "Owner",
                   ImgUrl = "https://www.stevensegallery.com/150/150"


               });

来自 https://via.placeholder.com/150 的图像始终显示,但其他两个不显示绑定。 我有什么遗漏吗?

我尝试将列表中的图像移动到第二甚至第三,但在任何位置都只显示占位符。