本文介绍了Xamarin形式,如何将byte []保存到SQLite数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我正在处理如何将字节数组保存到SQLite数据库的问题.当我将base []保存到databse时,它看起来像是持有该数组的属性,但实际上,当我在设备上重新启动应用程序时,它并没有保存到数据库中.也许我必须先将byte []转换为以64为基数,然后再将其放入数据库中?如果可以,我该怎么做?也许还有另一种保存到sqlite数据库而不转换为base64的方法吗?

today I am dealing with issue how to save byte array to SQLite database. When I am saving base[] to databse, it's looks like that property holding that array, but actually it is not save to database, when I restart application on device. Maybe I have to convert byte[] to base 64 before putting it to database? If yes, how I can do that? Also maybe there are another way save to sqlite database , without converting to base64?

这是我的对象类:

public class Unit
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public bool IsStarted { get; set; }
        public byte[] CImage { get; set; }
    }

我在这里从图库中选择图像并设置为可绑定的值:

Here I am selecting image from gallery and set to bindable value:

IGalleryImageService galleryService = Xamarin.Forms.DependencyService.Get<IGalleryImageService>();
            galleryService.ImageSelected += (o, imageSourceEventArgs) =>
            {
                ActiveP.CImageBindable = imageSourceEventArgs.ImageSource;

                MemoryStream st = new MemoryStream(imageSourceEventArgs.ImageSource);
                ImageSource imgSource = ImageSource.FromStream(() => st);
                (ActiveP.Page as PTemplate).CImage.Source = imgSource;
            };
            galleryService.SelectImage();

这是我的另一个Unit类,正在其中更新数据库或将对象插入数据库:

here is my other Unit class where I am updating my database or inserting objects to database:

     private void UpdateObjectToDB()
     {
        PUnit pUinit = new PUnit()
        {
            Id = this.Id,
            IsStarted = this.IsStarted,
            CImage = this.CImage
        };
        App.database.Update(pUinit);
    }

    public int InsertObjectToDB()
    {
        PUnit pUnit = new PUnit()
        {
            IsStarted = this.IsStarted,
            CCImage = this.CImage
        };
        return App.database.AddItem(pUnit);
}

我必须在哪里转换以及如何实现?感谢您的回答或建议.

Where I have to convert and how to implement that? Thank you for answers or suggestions.

推荐答案

好吧,我通过将此类属性更改为字符串类型来解决了问题:

Well, I solved issue by changing this class property to string type:

public class Unit
    {
        [PrimaryKey, AutoIncrement]
        public string CImageBase64 { get; set; }
    }

然后在其他单元类中,我更改了可绑定数据类型:

Then in other unit class I changed bindable data type:

public string CImageBindable
        {
            get
            {
                return base.CImageBase64;
            }
            set
            {
                base.CImageBase64 = value;
                OnPropertyChanged(nameof(CImageBindable));
            }
        }

在这里,我转换为以64为底的

And here I converted to base 64

    if (this.P.CImageBindable == null)
    {
        CImage.Source = "img.png";
    }
    else
    {
        var imageBytes = Convert.FromBase64String(this.P.CImageBindable);

        MemoryStream st = new MemoryStream(imageBytes);
        ImageSource imgSource = ImageSource.FromStream(() => st);
        CImage.Source = imgSource;
    }

图片选择也会转换:

IGalleryImageService galleryService = Xamarin.Forms.DependencyService.Get<IGalleryImageService>();
            galleryService.ImageSelected += (o, imageSourceEventArgs) =>
            {
                ActiveParking.CImageBindable = Convert.ToBase64String(imageSourceEventArgs.ImageSource);
                MemoryStream st = new MemoryStream(imageSourceEventArgs.ImageSource);
                ImageSource imgSource = ImageSource.FromStream(() => st);
                (ActiveParking.Page as PTemplate).CImage.Source = imgSource;
            };
            galleryService.SelectImage();

这篇关于Xamarin形式,如何将byte []保存到SQLite数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 06:41