【发布时间】:2019-08-13 21:18:02
【问题描述】:
我目前正在开发 Xamarin.Forms 上的跨平台应用程序(iOS 和 Android)我目前的目标是为多个服务开发带有图像的滑块。
类似:
图片 |图片 |图片 |图片
标签 |标签 |标签 |标签
可滚动到两侧。
为此我创建了:
-服务类
string ID
string Name
string ServiceType
ImageSource ImgUrl
-A ViewModel (HLandingVM) 在这里,我准备了列表对象并将它们加载到页面中
-HLandingPage.xaml 用于视图
-HLandingPage.cs 加载视图模型
主要问题是我确实看到我的标签正确显示并按预期滚动。但是图像根本不显示。
我尝试传递给模型: 一个 ImageSource , Image 本身,只为绑定传递一个 Uri。但是图像根本不会显示。
-类
`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");
}
}`
-查看
`
<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>
`
-虚拟机
添加服务和图像源 `
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 返回空,则尝试(不工作)加载资源图像
`
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);
`
没有触发明显的错误。只是空的图片。
【问题讨论】:
标签: forms image xamarin uri viewmodel