簡體   English   中英

.NET MAUI - 以字典作為參數的 shell 導航錯誤

[英].NET MAUI - Error with shell navigation with dictionary as parameter

我想導航到具有多個參數的頁面。

為此,我使用字典:

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

但是在 Visual Studio 中我得到了這個錯誤:

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

有誰知道如何解決這個問題?

GoToAsync()的方法簽名需要一個boolIDictionary<string, object>作為第二個參數,而您向它傳遞的是Dictionary<string, string>

由於編譯器找不到任何匹配的重載,它只嘗試第一個。 但是,您的Dictionary<string, string>不能類型轉換為bool ,因此會出現編譯器錯誤。

有關不同GoToAsync()變體的更多信息: https ://learn.microsoft.com/en-us/dotnet/api/microsoft.maui.controls.shell.gotoasync?view=net-maui-7.0

請注意, Dictionary<string, string>Dictionary<string, object>不同,編譯器只能接受后者,因為它具有與方法重載的第二個變體的簽名中指定的接口相同的類型參數(或第一個,如果您在第二位提供bool )。

為了解決這個問題,請將您的代碼更改為以下內容:

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

這應該可行,因為您可以將string分配給object

你的代碼應該是

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM