Update ViewModel on form's OnAppearing() event

robs23robs23 Member ✭✭

I have a form like below:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestZXing.ProcessPage"
             Title="Szczegóły zgłoszenia">
    <ContentPage.Content>
        <StackLayout VerticalOptions="Center">
            <Picker
                x:Name="cmbActionTypes"
                HorizontalOptions="Center" 
                Title="Wybierz typ zgłoszenia"
                ItemsSource="{Binding ActionTypes}"
                ItemDisplayBinding="{Binding Name}"
                SelectedItem="{Binding Type, Mode=TwoWay}"
                SelectedIndex="{Binding SelectedIndex}"
                />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Picker's source is "ActionTypes" ObservableCollection. The collection is being filled on form's OnAppearing event, like below:

        protected async override void OnAppearing()
        {
            base.OnAppearing();
            try
            {
                    await vm.Initialize(vm._this.ActionTypeId);
                }
                BindingContext = vm;

            }
            catch (Exception ex)
            {
                DisplayAlert("No connection", "It seems your internet connection stopped working", "OK");
            }
        }

And ViewModel's Initialize is:

public ObservableCollection<ActionType> ActionTypes { get; set; }
        public Process _this { get; set; }


        public async Task Initialize(int AtId = -1)
        {
            try
            {
                int index=-1;
                int i = 0;
                _selectedIndex = -1;
                ActionTypes = new ObservableCollection<ActionType>();
                ActionTypesKeeper keeper = new ActionTypesKeeper();
                await keeper.Reload();
                foreach (ActionType at in keeper.Items)
                {
                    ActionTypes.Add(at);
                    if (at.ActionTypeId == AtId)
                    {
                        index = i;
                    }
                    i++;
                }
                if (AtId >= 0 && index >=0)
                {
                    SelectedIndex = index;
                }
            }
            catch(Exception ex)
            {
                throw;
            }

        }

The thing is when I check if ActionTypes.Any() after vm.Initialize finished, I get true, so there are items in the collection, but they don't appear in picker source list. It's like I would have to tell the forms somehow that vm has been updated (but I'm doing so by BindingContext = vm, right??).
Before I called vm.Initialize from ViewModel's constructor, and it was working correctly. I had to change it, though, because otherwise I was not able to throw error from vm.Initialize back to form's constructor. If you want to have a look on working code, here it is: https://github.com/robs23/TestZXing-master/blob/master/TestZXing/ProcessPage.xaml.cs

Tagged:

Best Answer

Answers

Sign In or Register to comment.