[英]C# Xamarin ImageURL
我有3個領域的模型:TItle,Body,Picture。
public class Names
{ [PrimaryKey]
public string Title { get; set; }
public string Body { get; set; }
public string Picture { get; set; }}
字段'圖片'是字符串值,它是圖像網址的一部分。 所以真正的URL看起來像:
Source = Settings.ServerUrl + "/api/File/" + Picture
我如何在xamarin中使用這樣的url作為imagecell的imagesource?
當前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代碼添加為圖像。 對不起。
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();
}
}
xmlns:local="clr-namespace:SameNameSpace;assembly=SomeAssemblyName"
<ContentPage.Resources>
<ResourceDictionary>
<local:PictureURLConverter x:Key="pictureURL"></local:PictureURLConverter>
</ResourceDictionary>
</ContentPage.Resources>
<Image Source="{Binding Picture, Converter={StaticResource pictureURL}}" />
創建一個viewModel並更改您的屬性以返回所需的URL並將此屬性綁定到您的Image
Source
。
public string PictureUrl
{
get
{
return Settings.ServerUrl + "/api/File/" + this.PictureUrl;
}
set
{
this.PictureUrl = value;
OnPropertyChanged("PictureUrl");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.