0

I'm starting to learn Xamarin. I followed this video course: Youtube Link.

I have an SQLite database populated, that i added (in root) in the code project as embedded resource. If I check ItemPage.xaml, there is a black windows and i don't see any listview. (This is a Master-Detail project with .net standard).

I checked with the debugger, the ExecuteLoadItemsCommand gets the rows from the db, but the listview didn't display it.

ItemsPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:sfgrid="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:SampleBrowser.SfDataGrid;assembly=SampleBrowser.SfDataGrid"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             xmlns:local1="clr-namespace:ZilahiReformatusTemeto.Models"
             x:Class="ZilahiReformatusTemeto.Views.ItemsPage"
              Title="{Binding Title}"
             x:Name="BrowseItemsPage">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Add" Clicked="AddItem_Clicked">
            <ToolbarItem.Icon>
                <OnPlatform x:TypeArguments="FileImageSource">
                    <On Platform="UWP" Value="add.png"/>
                </OnPlatform>
            </ToolbarItem.Icon>
        </ToolbarItem>
    </ContentPage.ToolbarItems>
    <ContentPage.BindingContext>
        <local1:Sírok x:Name="viewModel" />
    </ContentPage.BindingContext>

    <StackLayout>
        <ListView 
                ItemsSource="{Binding Sírok}"
                VerticalOptions="FillAndExpand"
                HasUnevenRows="true"
                RefreshCommand="{Binding LoadItemsCommand}"
                IsPullToRefreshEnabled="true"
                IsRefreshing="{Binding IsBusy, Mode=OneWay}"
                
                ItemSelected="OnItemSelected">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout >
                            <Label Text="{Binding Parcellaszám}" 
                                LineBreakMode="NoWrap" 
                                Style="{DynamicResource ListItemTextStyle}" 
                                FontSize="16" />
                            <Label Text="{Binding Sírhelyszám}" BindingContext="{Binding Sírhelyszám}"
                                LineBreakMode="NoWrap"
                                Style="{DynamicResource ListItemDetailTextStyle}"
                                FontSize="13" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

ItemsPage.xaml.cs:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

using ZilahiReformatusTemeto.Models;
using ZilahiReformatusTemeto.Views;
using ZilahiReformatusTemeto.ViewModels;

namespace ZilahiReformatusTemeto.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ItemsPage : ContentPage
    {
        ItemsViewModel viewModelg;

        public ItemsPage()
        {
            InitializeComponent();

            viewModelg = new ItemsViewModel();
            BindingContext = viewModelg;
        }

        async void OnItemSelected(object sender, SelectedItemChangedEventArgs args)
        {
            var item = (Sírok)args.SelectedItem;
            if (item == null)
                return;

            await Navigation.PushAsync(new ItemDetailPage(new ItemDetailViewModel(item)));

            // Manually deselect item.
            //SfDataGrid.SelectedIndexProperty = -1;
            //ItemsListView.SelectedItem = null;
        }

        async void AddItem_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushModalAsync(new NavigationPage(new NewItemPage()));
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

            if (viewModelg.Items.Count == 0)
                viewModelg.LoadItemsCommand.Execute(null);
        }
    }
}

Edit - Added - ItemsViewModel:

    using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading.Tasks;

using Xamarin.Forms;

using ZilahiReformatusTemeto.Models;
using ZilahiReformatusTemeto.Views;

namespace ZilahiReformatusTemeto.ViewModels
{
    public class ItemsViewModel : BaseViewModel
    {
        public ObservableCollection<Sírok> Items { get; set; }
        public Command LoadItemsCommand { get; set; }

        public ItemsViewModel()
        {
            Title = "Browse";
            Items = new ObservableCollection<Sírok>();
            LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());

            MessagingCenter.Subscribe<NewItemPage, Sírok>(this, "AddItem", async (obj, item) =>
            {
                var newItem = item as Sírok;
                Items.Add(newItem);
                await DataStore.AddItemAsync(newItem);
            });
        }

        async Task ExecuteLoadItemsCommand()
        {
            if (IsBusy)
                return;

            IsBusy = true;

            try
            {
                Items.Clear();
                var items = await DataStore.GetItemsAsync(true);
                foreach (Sírok item in items)
                {
                    Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }
    }
}

Here is my project that I'm working on : LINK

3
  • where is the code for ItemsViewModel? Commented Dec 15, 2020 at 15:00
  • I added the Added ItemsViewModel.cs. There is also the entire project on the bottom link. Commented Dec 15, 2020 at 15:28
  • 1
    ItemsSource="{Binding Sírok}" - there is no property named Sirok on your ViewModel Commented Dec 15, 2020 at 15:30

1 Answer 1

2

in your XAML

<ListView ItemsSource="{Binding Sírok}"

there is no property named Sirok in your VM. You probably mean to do

<ListView ItemsSource="{Binding Items}"

because your VM does have an Items collection

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.