根據您的問題,您在 Page 構造函數上調用 API,這就是為什么加載 Web API 然后在 page2 上導航需要時間。如果您想在加載 api 之前在 page2 上導航。檢查下面的代碼 public partial class Page2 : ContentPage { bool IsLoading{ get; set; } public Page2() { InitializeComponent(); IsLoading = false; } protected async override void OnAppearing() { base.OnAppearing(); if (!IsLoading) { IsLoading=true **Call the Web API Method Here** } IsLoading=false } }Startup.cs / ConfigureServices() 中的哈希碼:任何控制器/api 調用中的哈希碼: 該GetHashCode()方法未被篡改。這導致來自 appsettings.json 的配置未在控制器/api 調用中應用,所有屬性都使用它們的默認值/null 進行實例化。我希望使用該AddSingleton()方法可以注入相同的實例并在整個應用程序生命周期中重用它。有人能告訴我為什么要創建一個新的 RuntimeServices 實例嗎?我將如何存檔我的目標,即在 Startup.cs中擁有一個可用的對象實例并仍然訪問我的控制器中的相同對象實例?我的首選解決方案是通常的單例模式。但我希望使用 asp.net core 提供的內置功能來解決這個問題。
1 回答

拉莫斯之舞
TA貢獻1820條經驗 獲得超10個贊
因為這個調用:
services.AddSingleton(runtimeServices);
注冊 的實例RuntimeServices
,它不配置IOptions<RuntimeServices>
. 因此,當您請求 an 時IOptions<RuntimeServices>
,卻沒有,您將獲得一個具有所有默認值的新實例。
您想要:
保持
AddSingleton
和使用public ApplicationController(RuntimeServices services)
刪除
AddSingleton
調用并使用services.Configure<RuntimeServices>(_configuration)
- 1 回答
- 0 關注
- 625 瀏覽
添加回答
舉報
0/150
提交
取消