Sentil Sebastian February 2016

Sqlite Data not displaying

Application
x:Class="sqlasynctest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:sqlasynctest"
xmlns:viewModel="using:sqlasynctest.ViewModel"
RequestedTheme="Light">
<Application.Resources>
    <ResourceDictionary>
        <viewModel:ViewModelLocator x:Key="Locator" />
    </ResourceDictionary>
</Application.Resources>

public class ViewModelLocator
{
    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        if (!SimpleIoc.Default.IsRegistered<IDal>())
        {
            SimpleIoc.Default.Register<IDal, Dal>();
        }


        SimpleIoc.Default.Register<MainViewModel>();
    }
    public MainViewModel Main
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MainViewModel>();
        }
    }
    public static void Cleanup()
    {
        // TODO Clear the ViewModels
    }

}

MainViewModel

public class MainViewModel : ViewModelBase
{
    private readonly IDal _iDal;

    private IList<User> _user;

    public IList<User> Users
    {
        get { return _user; }
        set { Set("Users", ref _user, value); }
    }
    public MainViewModel(IDal dal)
    {
        _iDal = dal;
        LoadDataCommand = new RelayCommand(async () => await LoadData());
    }
    public ICommand LoadDataCommand { get; private set; }
    public async Task LoadData()
    {
        Users = await _iDal.LoadAllUser();
    }
}

MainPage

<Page
x:Class="sqlasynctest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:sqlasynctest"
DataContext="{Binding Main, Source={StaticResource Locator}}"
xmlns:d="http://schemas.microsoft.com/exp        

Answers


Igor Kulman February 2016

Your problems is that your collection is a List. It does not notify the UI about chnages. User an ObservableCollection instead and it will start to work.


Yarik February 2016

You have LoadDataCommand in your view model, but it is not called from anywhere. There is no magic here – you have to invoke it either from XAML, or from code-behind file. Here's the way this can be done in XAML:

  1. Install Microsoft.Xaml.Behaviors.Uwp.Managed NuGet package.
  2. Modify XAML of your main page:

    <Page
    ...
    xmlns:i="using:Microsoft.Xaml.Interactivity"
    xmlns:c="using:Microsoft.Xaml.Interactions.Core"> 
    
    <i:Interaction.Behaviors>
        <c:EventTriggerBehavior EventName="Loaded">
            <c:InvokeCommandAction Command="{Binding LoadDataCommand}"/>
        </c:EventTriggerBehavior>
    </i:Interaction.Behaviors>
    
    <Grid ...>
    
  3. Remove overridden OnNavigatedTo method from the code-behind file.

  4. LoadDataCommand will now be invoked each time MainPage is loaded.

Post Status

Asked in February 2016
Viewed 2,072 times
Voted 8
Answered 2 times

Search




Leave an answer