C#Xamarin ImageURL [英] C# Xamarin ImageURL
本文介绍了C#Xamarin ImageURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有3个字段的模型:标题,正文,图片.
I have model with 3 fields : TItle, Body, Picture.
public class Names
{ [PrimaryKey]
public string Title { get; set; }
public string Body { get; set; }
public string Picture { get; set; }}
图片"字段是一个字符串值,它是图片网址的一部分. 因此,真实的网址将如下所示:
Field 'Picture' is a string value which is a part of url of image. So real URL will looks like:
Source = Settings.ServerUrl + "/api/File/" + Picture
如何在xamarin中将此类URL用于imagecell的图像源?
How I can use such url for imagesource of imagecell in xamarin?
当前XAML的示例:
Example of current XAML:
当前XAML.CS的示例
Example of current 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代码添加为图像.抱歉.
I added xaml code as image because of code limit. Sorry for that.
推荐答案
通过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}}" />
这篇关于C#Xamarin ImageURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文