Jason Richmeier
Jason Richmeier

Reputation: 1645

Odd Behavior When Using Generic Delegate Command In Prism / Xamarin Forms

I am attempting to use the DelegateCommand<T> type from the Prism assembly.

Suppose I have the following in my view model:

public DelegateCommand<int> TestCommand
{
    get;

    set;
}

public void TestCommandExecute(int parameter)
{
    return;
}

In the constructor of the view model I initialize the TestCommand property with the following:

TestCommand = new DelegateCommand<int>(TestCommandExecute);

If I do this, the application seems to crash and a blank screen is displayed. I also observed that the OnNavigatedTo method is never called in this case.

I have observed that if I change the type of TestCommand from DelegateCommand<int> to DelegateCommand (and then adjust the signature of the TestCommandExecute method accordingly, the application does not crash and behaves as expected.

I do not see any errors being written to the output window so I am at a complete loss.

Upvotes: 0

Views: 398

Answers (1)

Dan Siegel
Dan Siegel

Reputation: 5799

The most likely culprit here is that a null is being passed in as the Command Parameter. You might try making the command use a nullable int like so:

public class FooViewModel
{
    public FooViewModel()
    {
        // Prevent execution of null int
        MyCommand = new DelegateCommand<int?>(OnMyCommandExecuted, p => p != null);
    }
}

As for the exception this is likely due to the fact that you aren't checking the NavigationResult when you navigated to the new View. You can do this easily like shown below:

var result = await NavigationService.NavigateAsync("Foo");

if(!result.Success)
{
    Console.WriteLine(result.Exception);
    System.Diagnostics.Debugger.Break();
}

Upvotes: 3

Related Questions