Home > Blockchain >  .NET MAUI - Error with shell navigation with dictionary as parameter
.NET MAUI - Error with shell navigation with dictionary as parameter

Time:12-23

I want to navigate to a Page with multiple parameters.

For that I use a Dictionary:

await Shell.Current.GoToAsync($"{nameof(DetailPage)}?",
                new Dictionary<string, string>
                {
                    ["Name"] = s.RecipeName,
                    ["Ingredients"] = s.Ingredients,
                    ["Steps"] = s.Steps
                });

But in Visual Studio I get this error:

Argument 2: cannot convert from 'System.Collections.Generic.Dictionary<string, string>' to 'bool'

Does anyone know how to solve this problem?

CodePudding user response:

The method signature of GoToAsync() expects a bool or a IDictionary<string, object> as the second argument, while you're passing it a Dictionary<string, string>.

Since the compiler cannot find any matching overloads, it just tries with first one. However, your Dictionary<string, string> cannot be typecast to bool, hence the compiler error.

More info on the different GoToAsync() variants: https://learn.microsoft.com/en-us/dotnet/api/microsoft.maui.controls.shell.gotoasync?view=net-maui-7.0

Note that Dictionary<string, string> is not the same as Dictionary<string, object>, only the latter is acceptable for the compiler since it has the same type arguments specified as the interface in the signature of the second variant of the method overloads (or the first one, if you provide a bool in second place).

In order to fix this, change your code to the following:

await Shell.Current.GoToAsync($"nameof(DetailPage)}?",
    new Dictionary<string, object>
    {
        ["Name"] = s.RecipeName,
        ["Ingredients"] = s.Ingredients,
        ["Steps"] = s.Steps
    }
);

This should work, because you can assign a string to object.

CodePudding user response:

Your code should be

await Shell.Current.GoToAsync(nameof(DetailPage), true, new Dictionary<string, object>
                {
                    {"Name", s.RecipeName },
                    {"Ingredients", s.Ingredients},
                    {"Steps", s.Steps}
                });
  • Related