Could not save pictures into db with Maui and SQlite episode2

Episode 1 here

neattanaka.hatenablog.com

Causing2

Reason for ListView showing Items of picture

 ListView must have a List to show at MainViewModel(Binding Context) not only binding ItemSource at Xaml.

Solution

Add the code at MainViewModel then, bind ItemSource.

For your information, [ObserverProperty] can generate automatically to Capital G "GazouByte" at Xaml ItemSource from field gazouByte at MainV MainViewModel

  •  
  • namespace WorkReview.ViewModels;

    public partial class MainViewModel : ObservableObject
    {


    [ObservableProperty]
    private string? statusMessage;

    [ObservableProperty]
    private List<GazouByte>? gazouBytes; //here, List for MainPage

    [ObservableProperty]
    private ImageSource? userPreview; //MainPageのプレビュー用


    private string? gazouName; //ファイル名

    private byte[]? gazouBinary; //画像のバイナリデータ

    private string? gazouExtension; //拡張子情報

     

     

    public MainViewModel()
    { }

    [RelayCommand]
    private void OnGetAllGazou() //画像リストの取得ボタン
    {
    GazouBytes = App.GazouByteRepo.GetAllGazouBytes();
    StatusMessage = "got all file list"; StatusMessage = "got all file list";

    }

    [RelayCommand]
    private void OnFileSave()
    {
    if (gazouName == null) return;

    var gazouByte = new GazouByte
    {
    GazouName = gazouName!,
    GazouBinary = gazouBinary!, //!はNull出ないことを宣言

    GazouExtension = gazouExtension!,
    };

    App.GazouByteRepo.AddNewGazouByte(gazouByte);

    StatusMessage = "file saved";

    }

    [RelayCommand]
    private async Task OnFileSelect() //非同期でTaskとして結果を返す

    {

    try

    {

    var result = await FilePicker.PickAsync();

    if (result == null) return;


    var fileName = result.FileName;

    if (fileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||

    fileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))

    {

    using (var stream = await result.OpenReadAsync())

    using (var memoryStream = new MemoryStream())

    {

    await stream.CopyToAsync(memoryStream);

    gazouName = fileName;

    gazouBinary = memoryStream.ToArray();

    gazouExtension = result.ContentType;


    var previewStream = new MemoryStream(memoryStream.ToArray());

    UserPreview = ImageSource.FromStream(() => previewStream);

    }

    }

    else

    {

    StatusMessage = "Unsupported file type.";

    }

    }

    catch (Exception ex)

    {

    StatusMessage = $"Error selecting file: {ex.Message}";

    }

    }

    }

 

Afterword

 As you can see from my past questions, I've been a bit of a pain with my inquiries. I'll try to be more considerate in the future.

 I've realized I still have a lot to learn about MVVM and XAML.

 But I was really impressed by how easily the people who answered my questions were able to solve problems I'd been stuck on for a week. I hope to be able to do that someday too.

 I learned a lot just by asking questions, so thanks for that.