我是Xamarin,MVVM和跨平台应用程序开发(Android,iOS,UWP)的新手。我正在使用Realm
以Base64格式存储/显示图像。我已经成功存储了图像base64字符串,但是我在ListView中显示图像时遇到了困难。
ImageModel.cs
namespace RealmTest.Models
{
public class ImageModel : RealmObject
{
[PrimaryKey]
public string ImageId { get; set; } = Guid.NewGuid().ToString();
public string ImageName { get; set; }
public string ImageBase64 { get; set; }
public string ImageCaseHistory { get; set; }
}
}
BaseViewModel.cs
namespace RealmTest.ViewModels
{
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName]string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
RealmImageViewModel.cs
namespace RealmTest.ViewModels
{
public class RealmImageViewModel : BaseViewModel
{
private ObservableCollection<ImageModel> imagesList;
public ObservableCollection<ImageModel> ImagesList
{
get
{
return imagesList;
}
set
{
imagesList = value;
}
}
public RealmImageViewModel()
{
Realm context = Realm.GetInstance();
ImagesList = new ObservableCollection<ImageModel>(context.All<ImageModel>());
Debug.WriteLine($"ImagesList ==>" + ImagesList.Count());
}
}
}
HomePage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="RealmTest.Views.HomePage"
Title="Home">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Add"
Icon="ic_add_black.png"/>
</ContentPage.ToolbarItems>
<ContentPage.Content>
<ListView ItemsSource="{Binding ImagesList}"
HasUnevenRows="True"
BackgroundColor="#f5f5f5">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid BackgroundColor="White"
Margin="4">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Row="0"
Text="{Binding ImageName}"
FontSize="Medium"
Margin="4"
FontAttributes="Bold" />
<Image Source="{Binding ImageBase64}"
HeightRequest="150"
Grid.Row="1"
/>
<StackLayout Orientation="Horizontal"
Grid.Row="2"
Margin="4"
Padding="2">
<Label Text="{Binding ImageCaseHistory}"
FontSize="Small" />
<Label Text="{Binding ImageName}"
FontSize="Small" />
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
HomePage.cs
namespace RealmTest.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class HomePage : ContentPage
{
public HomePage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
BindingContext = new RealmImageViewModel();
/**
var image = Xamarin.Forms.ImageSource.FromStream(
() => new MemoryStream(Convert.FromBase64String(base64)));
**/
}
}
}
到目前为止,我已经了解了有关我的问题/查询的两件事 -
我可以将base64
字符串显示为图像格式,如 -
var image = Xamarin.Forms.ImageSource.FromStream( ()=&gt; new MemoryStream(Convert.FromBase64String(base64)));
我在SO上阅读了this answer并了解到可以创建一些ConverterBase64ImageSource
,但我无法理解它,如何制作和实现它。
我仍然无法找到在ListView中显示所有图像(我的Realm
数据库中的Base64字符串格式)的方法。
答案 0 :(得分:0)
我发现执行此操作的方法有所不同,我将base64文件保存在localDB(SQLite)中。
这是在模型中:
//在这里,我们将base64转换为ImageSource
string foto;
[JsonProperty(PropertyName = "Foto")]
public string Foto
{
get { return foto; }
set
{
foto = value;
OnPropertyChanged("Foto");
FOTO = Xamarin.Forms.ImageSource.FromStream(
() => new MemoryStream(Convert.FromBase64String(foto)));
}
}
private Xamarin.Forms.ImageSource _foto;
[Ignore]//The localdb don't support the ImageSource
public Xamarin.Forms.ImageSource FOTO
{
get { return _foto; }
set
{
_foto = value;
OnPropertyChanged("FOTO");
}
}
就我而言,我像帮助器一样使用此类来转换文件:
public class FilesHelper
{
public static byte[] ReadFully(Stream input)
{
using (MemoryStream ms = new MemoryStream())
{
input.CopyTo(ms);
return ms.ToArray();
}
}
}
在这里,我们将图像转换为base64文件以保存在localDB中:
byte[] imageArray = null;
String image = "";
if (this.file != null)
{
imageArray = FilesHelper.ReadFully(this.file.GetStream());
image = Convert.ToBase64String(imageArray);
}
我希望有帮助。