我有3个领域的模型: TItle,身体,图片。
public class Names
{ [PrimaryKey]
public string Title { get; set; }
public string Body { get; set; }
public string Picture { get; set; }}字段'Picture‘是一个字符串值,它是图像url的一部分。因此,实际的URL将如下所示:
Source = Settings.ServerUrl + "/api/File/" + Picture如何在xamarin中为imagecell的imagesource使用这样的url?
当前XAML示例:

当前XAML.CS示例
namespace Project.Mobile.Client.Portable.Views.Names
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class NamesListPage : ContentPage
{
public ObservableCollection<Models.Names> items { get; set; }
public NewPage()
{
items = new ObservableCollection<Models.Names>();
this.BindingContext = this;
InitializeComponent();
// Disabling selection
Lst.ItemSelected += (sender, e) => {
((ListView)sender).SelectedItem = null;
};
Lst.Refreshing += (sender, e) => {
LoadUsersData();
};
LoadUsersData();
}
public async void LoadUsersData()
{
Lst.IsRefreshing = true;
var names = await App.Database.Names.GetItemsAsync();
items.Clear();
foreach (var item in names)
items.Add(item);
Lst.IsRefreshing = false;
}
public async void OnItemTapped(object sender, ItemTappedEventArgs e)
{
NamesReadPage readPage = new NamesReadPage();
readPage.BindingContext = e.Item as Models.Names;
await Navigation.PushAsync(readPage);
}
}由于代码限制,我添加了xaml代码作为图像。很抱歉。
发布于 2018-02-10 13:48:01
通过IValueConverter
public class PictureURLConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Settings.ServerUrl + "/api/File/" + (string)value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}添加IValueConverter的名称空间
xmlns:local="clr-namespace:SameNameSpace;assembly=SomeAssemblyName" 将IValueConverter添加到您的ContentPage.Resources
<ContentPage.Resources>
<ResourceDictionary>
<local:PictureURLConverter x:Key="pictureURL"></local:PictureURLConverter>
</ResourceDictionary>
</ContentPage.Resources>在绑定中使用转换器
<Image Source="{Binding Picture, Converter={StaticResource pictureURL}}" />发布于 2018-02-10 23:05:54
创建一个viewModel并更改属性以返回所需的url,然后将此属性绑定到您的Image Source。
public string PictureUrl
{
get
{
return Settings.ServerUrl + "/api/File/" + this.PictureUrl;
}
set
{
this.PictureUrl = value;
OnPropertyChanged("PictureUrl");
}
}https://stackoverflow.com/questions/48716966
复制相似问题