Forum Xamarin.Forms

Unhandled error on ViewModel constructor

Gsr1992Gsr1992 USMember ✭✭

Hi there!

I am facing an unhandled exception that occurs on the constructor of my ViewModel class, and i "imagine" that the problem it is related with the "tasks".

My Xamarin.Forms version is 2.3.4.247.

Follow the code below:

    public class SincronizacaoViewModel : ViewModel_Base
    {
        private INavigation _navigation;

        private ObservableCollection _sincDownloads;
        public ObservableCollection SincDownloads
        {
            get => _sincDownloads;
            set
            {
                _sincDownloads = value;
                base.OnPropertyChanged();
            }
        }

        private int _totalItensParaSincronizar;
        public int TotalItensParaSincronizar
        {
            get => this._totalItensParaSincronizar;
            set
            {
                this._totalItensParaSincronizar = value; OnPropertyChanged();
            }
        }

        private bool _carregando;
        public bool Carregando
        {
            get => _carregando;
            set
            {
                _carregando = value;
                base.OnPropertyChanged();
            }
        }


        // Methods


        public SincronizacaoViewModel(INavigation p_navigation)
        {
            Task.Run(async () =>
            {
                this._navigation = p_navigation;
                await CarregarDownloads();
            });
        }

        private Task CarregarDownloads()
        {
            return Task.Factory.StartNew(() =>
            {
                CarregarDownloadProduto();
            });
        }

        private Task CarregarDownloadProduto()
        {
            return Task.Factory.StartNew(async () =>
            {
                var c = new Database.ConfiguracaoDatabase().Configuracao;

                var sincs = await Services.Factory.GetServices().SIN_Download(c.IdUsuarioLogado.Value, c.Sin_UltimoProdutoBaixado);
                TotalItensParaSincronizar = sincs.Count;
                SincDownloads = sincs;
            });
        }
    }

The last executed method before throw the exception is CarregarDownloads.

Thanks.

Best Answer

  • Gsr1992Gsr1992 USMember ✭✭
    Accepted Answer

    @JohnHardman, thanks for the reply, i really was forgotten to await CarregarDownloadProduto(), but the problem was another.

    To find the solution, i had to select more options at "exception setting".

    Done that, the exception was throwed where the problem was ocurring.

    In the ViewModel that calls the PushAsync method of SincronizacaoView(), i had forgotten to assign the value of my instance of INavigation, so when i call await _Navigation.PushAsync(new NavigationPage(new SincronizacaoView()));, the variable __Navigation _was null, throwing the exception.

    Thank you all.

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    Add some try/catch ... maybe useful
  • Gsr1992Gsr1992 USMember ✭✭
    edited October 2017

    @AlessandroCaliaro said:
    Add some try/catch ... maybe useful

    Hi, @AlessandroCaliaro, thanks for you reply, but the exception is not throwed.

        public class SincronizacaoViewModel : ViewModel_Base
        {
            private INavigation _navigation;
    
            private ObservableCollection _sincDownloads;
            public ObservableCollection SincDownloads
            {
                get => _sincDownloads;
                set
                {
                    try
                    {
                        _sincDownloads = value;
                        base.OnPropertyChanged();
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }
    
            private int _totalItensParaSincronizar;
            public int TotalItensParaSincronizar
            {
                get => this._totalItensParaSincronizar;
                set
                {
                    try
                    {
                        this._totalItensParaSincronizar = value; OnPropertyChanged();
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }                
                }
            }
    
            private bool _carregando;
            public bool Carregando
            {
                get => _carregando;
                set
                {
                    try
                    {
                        _carregando = value;
                        base.OnPropertyChanged();
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }
    
    
            // Methods
    
    
            public SincronizacaoViewModel(INavigation p_navigation)
            {
                try
                {
                    Task.Run(async () =>
                    {
                        try
                        {
                            this._navigation = p_navigation;
                            await CarregarDownloads();
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    });
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
    
            private Task CarregarDownloads()
            {
                try
                {
                    return Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            CarregarDownloadProduto();
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }                   
                    });
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
    
            private Task CarregarDownloadProduto()
            {
                try
                {
                    return Task.Factory.StartNew(async () =>
                    {
                        try
                        {
                            var c = new Database.ConfiguracaoDatabase().Configuracao;
    
                            var sincs = await Services.Factory.GetServices().SIN_Download(c.IdUsuarioLogado.Value, c.Sin_UltimoProdutoBaixado);
                            TotalItensParaSincronizar = sincs.Count;
                            SincDownloads = sincs;
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    });
                }
                catch (Exception e)
                {
                    throw e;
                }
    
            }
        }
    
  • JohnHardmanJohnHardman GBUniversity mod

    @Gsr1992 - You are re-throwing exceptions in async blocks that are not being awaited. CarregarDownloadProduto() returns a Task, but you are not awaiting that Task.

    Where you are catching and immediately re-throwing exceptions, is that so that you can put breakpoints on the throw statements? If not, there is no point in the try/catch blocks that you have. To make them useful, decide what you want to do for each catch block - report an exception to an analytics system perhaps, then decide how best to recover from the exception.

  • Gsr1992Gsr1992 USMember ✭✭
    Accepted Answer

    @JohnHardman, thanks for the reply, i really was forgotten to await CarregarDownloadProduto(), but the problem was another.

    To find the solution, i had to select more options at "exception setting".

    Done that, the exception was throwed where the problem was ocurring.

    In the ViewModel that calls the PushAsync method of SincronizacaoView(), i had forgotten to assign the value of my instance of INavigation, so when i call await _Navigation.PushAsync(new NavigationPage(new SincronizacaoView()));, the variable __Navigation _was null, throwing the exception.

    Thank you all.

Sign In or Register to comment.