保存在数据库中的图像未显示在Xamarin Forms应用程序的Home.xaml页中

时间:2019-08-09 16:34:24

标签: xamarin xamarin.forms

在我的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]);
            }

        }

enter image description here

//也添加了我的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;

    }

1 个答案:

答案 0 :(得分:1)

如果您从Xamarin.Forms中的byte []数组加载图像,则可以尝试以下代码:

c#代码:

byte[] bitmapData = ...;
ImageSource imageSource= ImageSource.FromStream(() => new MemoryStream(bitmapData));

PlayerImage.Source = imageSource;//binding in code

xaml代码:

<Image x:Name="PlayerImage"  WidthRequest="25" HeightRequest="25"/>

或在xaml中绑定

<image Source="{Binding imageSource}"/>

注意:

  1. 我发现x:Name="{PlayerImage}"不正确。 应该是:x:Name="PlayerImage"而不是x:Name="{PlayerImage}"

  2. 您只需要使用以下一种绑定方法中的一种:

     PlayerImage.Source = imageSource;// in code
    

还有

 <Image x:Name="PlayerImage" Source="{Binding imageSource}" />

更新:

您可以尝试利用从IValueConverter派生的Converter,它可以基于字节数组创建图像。

ByteArrayToImageSourceConverter.cs

public class ByteArrayToImageSourceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        ImageSource retSource = null;
        if (value != null)
        {
            byte[] imageAsBytes = (byte[])value;
            var stream = new MemoryStream(imageAsBytes);
            retSource = ImageSource.FromStream(() => stream);
        }
        return retSource;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

PlayerDetails.cs

 public class PlayerDetails
  {
    // other fields

    public byte[] PlayerImage { get; set; }
  }

xaml(a usage example):

 <ContentPage.Resources>
    <ResourceDictionary>
        <myformapp1:ByteArrayToImageSourceConverter x:Key="ByteArrayToImage" 
/>
    </ResourceDictionary>
  </ContentPage.Resources>

 <StackLayout Margin="5">
    <CollectionView x:Name="collectionView"
                    ItemsSource="{Binding YoudataList}"> <!--changd to your dataList-->
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Grid Padding="10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Image Grid.RowSpan="2"  
                           x:Name="PlayerPic"
                           Source="{Binding PlayerImage, Converter={StaticResource ByteArrayToImage}}"
                           Aspect="AspectFill"
                           HeightRequest="60" 
                           WidthRequest="60" />
                    <Label Grid.Column="1" 
                           Text="test1" 
                           FontAttributes="Bold" />
                    <Label Grid.Row="1"
                           Grid.Column="1" 
                           Text="test2"
                           FontAttributes="Italic" 
                           VerticalOptions="End" />
                </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</StackLayout>