【发布时间】:2019-12-17 10:23:21
【问题描述】:
在我的 Xamarin Forms 应用程序中,Home.xaml 在保存到数据库后不显示图像。在调试时,我可以看到,bytes[] 正在 PlayerImage 上针对播放器显示。在 xaml 中,我有 Source="{Binding PlayerImage}" 但无法弄清楚不显示的原因。断点处显示的字节是否正确?
// Home.xaml
<ContentPage.Resources>
<DataTemplate x:Key="playerTemplate">
<ContentView>
<StackLayout Margin="5,5" BackgroundColor="#584961">
<Image x:Name="{PlayerImage}" Source="{Binding PlayerImage}" WidthRequest="25" HeightRequest="25"/>
<Label Text="{Binding FullName}" Font="Bold,18" TextColor="White"/>
<Label Text="{Binding Mobile}" Font="Bold,13" TextColor="White"/>
<Label Text="{Binding SoccerPosition}" Font="Bold,13" TextColor="White"/>
<Button Text="Remove Player" Clicked="DeleteButton_OnClicked" WidthRequest="120" HeightRequest="50" TextColor="White" BackgroundColor="#d6b947"></Button>
</StackLayout>
</ContentView>
</DataTemplate>
</ContentPage.Resources>
<StackLayout Margin="5">
<CollectionView x:Name="collectionview"
ItemTemplate="{StaticResource playerTemplate}">
<!--span here decides the number of items shows in one line. Now is 3 items one line-->
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" Span="3" />
</CollectionView.ItemsLayout>
</CollectionView>
</StackLayout>
// PlayerDetails.cs
public byte[] PlayerImage { get; set; }
//Home.xaml.cs
public void DisplayDetails()
{
List<PlayerDetails> details = (from x in conn.Table<PlayerDetails>() select x).ToList();
for (int i = 0; i < details.Count; i++)
{
players.Add(details[i]);
}
}
// 也添加了我的 PlayerDetails.cs 类
public class PlayerDetails : INotifyPropertyChanged
{
[PrimaryKey, AutoIncrement]
public int id { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
public byte[] PlayerImage { get; set; }
string fullname;
string mobile;
string soccerposition;
string email;
public PlayerDetails()
{
}
[Ignore]
public Image Image
{
get
{
var image = new Image();
image.Source = ImageSource.FromStream(() => new MemoryStream(PlayerImage));
return image;
}
set
{
//PlayerImage = Convert.ToByteArray(value.Source);
//Bitmap.FromStream(inStream);
}
}
public string FullName
{
set
{
if (fullname != value)
{
fullname = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("FullName"));
}
}
}
get
{
return fullname;
}
}
public string Mobile
{
set
{
if (mobile != value)
{
mobile = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Mobile"));
}
}
}
get
{
return mobile;
}
}
public string SoccerPosition
{
set
{
if (soccerposition != value)
{
soccerposition = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("SoccerPosition"));
}
}
}
get
{
return soccerposition;
}
}
public string Email
{
set
{
if (email != value)
{
email = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Email"));
}
}
}
get
{
return email;
}
}
//public ImageSource Source { get; internal set; }
public event PropertyChangedEventHandler PropertyChanged;
}
【问题讨论】:
-
首先,byte[] 不是有效的 ImageSource。其次,
byte[0]表示您的数组长度为 0,这意味着您的图像数据没有被加载。最后,将图像存储在数据库中通常不是一个好主意。 -
好的,从
List<PlayerDetails> details我们如何获取字节然后转换成图像,找到转换代码public Image byteArrayToImage(byte[] byteArrayIn) { MemoryStream ms = new MemoryStream(byteArrayIn); Image returnImage = Image.FromStream(ms); return returnImage; } -
如果您将图像存储在文件系统中,然后将路径存储在数据库中,这将容易得多。然后,您可以只使用图像源绑定的字段路径。但正如我之前提到的,您的 PlayerImage 属性似乎是一个空数组,所以我首先要弄清楚为什么会发生这种情况。
-
谢谢,我会检查它为什么存储为空数组
-
@Jason 现在我成功地将
bytearray保存到 SQlite 数据库中。现在如何将 bytearray 转换回 Image 然后传递给Source="{Binding PlayerImage}"
标签: xamarin xamarin.forms