acr.userdialogs alerasync with mvvmcross

CingCing NLMember ✭✭

I am trying to get an alert message with the following code:

`
private readonly IUserDialogs _userDialogs;

    public AddItemViewModel(IMvxNavigationService navigationService, IMvxLog log, IUserDialogs userdialog)
    {
        _navigationService = navigationService;
        _log = log;
        _userDialogs = userdialog;

}

 //go back to homeViewModel and save new item
    public IMvxAsyncCommand SaveNewItem => new MvxAsyncCommand(backToHVM);
    private async Task backToHVM()
    {
        if (Name.Length == 0) //Cant have an empty name
        {
            //warn user
           await _userDialogs.AlertAsync(new AlertConfig {
           Title="Name is Required",
           Message="",
           OkText="Ok"
           });
        }
        else
        {
              await _navigationService.Close(this, newItem);
        } 
    }`

But as soon as I press the button witch uses the command. I get an System.NullReferenceException.
I thought maybe the _userDialogs variable is empty but that isnt true because if have de following line in my app.cs
Mvx.IoCProvider.RegisterSingleton<IUserDialogs>(() => UserDialogs.Instance);

I searched online but there is no documentation and I couldnt find any examples that worked in my case.
Would you know how to correctly implement a alertDialog or where my mistake lies.

Any help is much appreaciated.

Best Answers

  • CingCing NL ✭✭
    edited January 7 Accepted Answer

    Whooooo I am such an id**t.
    It was the Name variable that was null, it didn't had anything to do with userdialogs that worked fine after I fixed the Name.

    The Name variable was just a normal get set :

    private string _name; public string Name { get { return _name; } set { SetProperty(ref _name, value); } }

    Although jezh still thanks you because after trying to debug it I noticed that it never reached the await _userdialogs function.

Answers

  • CingCing NLMember ✭✭
    edited January 7 Accepted Answer

    Whooooo I am such an id**t.
    It was the Name variable that was null, it didn't had anything to do with userdialogs that worked fine after I fixed the Name.

    The Name variable was just a normal get set :

    private string _name; public string Name { get { return _name; } set { SetProperty(ref _name, value); } }

    Although jezh still thanks you because after trying to debug it I noticed that it never reached the await _userdialogs function.

Sign In or Register to comment.