我有一個正在運行的 XF 應用程式,它通過 Azure API 進行大量資料庫訪問。到目前為止,一切都運行得很好。由于布局更改,我更改為基于 shell 的導航。我完成了整個作業,但面臨一個巨大的問題。我的 app.xaml.cs 加載了很多控制器:
Public partical class App : Xamarin.Forms.Application, INotifyPropertyChanged {public static CampaignController CampaignController { get; private set; }}
在 OnStart()
CampaignController = new CampaignController(new RestService());
在公共 App() 中,我將 AppShell() 作為主頁加載
MainPage = new AppShell();
這是我的 AppShell.xaml
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:EY365OCMobileApp"
x:Class="EY365OCMobileApp.AppShell">
<ShellContent Route="WelcomePage" ContentTemplate="{DataTemplate local:WelcomePage}">
</ShellContent>
WelcomePage.xaml.cs 如下所示:
protected async override void OnAppearing()
{
try
{
base.OnAppearing();
Campaigns = await App.CampaignController.GetCampaignsAsync(364840001);
CarouselView.ItemsSource = Campaigns;
BindingContext = Campaigns;
}
catch (Exception ex)
{
await CreateNewBug.CreateANewBug(ex.Message,"Error in Module " ex.Source,"\nMessage---\n{ 0}" ex.Message "\nInnerException---\n{ 0}" ex.InnerException "\nStackTrace---\n{ 0}" ex.StackTrace "\nTargetSite---\n{ 0}" ex.TargetSite);
ToastOptions toastOptions = Message.ShowMessage("An error was raised and a new bug created in our system.", "error");
await this.DisplayToastAsync(toastOptions);
}
}
此行會產生錯誤:
Campaigns = await App.CampaignController.GetCampaignsAsync(364840001);
你呼叫的物件是空的。
當我除錯時,app.xaml.cs 的初始化似乎沒有運行,因為它跳轉到 AppShell.xaml.cs。當我將代碼從 App.xaml.cs 移動到 AppShell.xaml.cs 時,它會帶來同樣的錯誤。知道如何在 Xamarin 的 shell 環境中初始化 rest-service 控制器嗎?
uj5u.com熱心網友回復:
您可以在使用時延遲加載控制器。
應用程式
CampaignController _controller;
Public CampaignController controller{
get
{
if(_controller == null)
{
_controller = new CampaignController(new RestService());
}
return _controller;
}
}
歡迎頁面
Campaigns = await (App.Current as App).controller.GetCampaignsAsync(364840001);
CarouselView.ItemsSource = Campaigns;
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/428108.html
標籤:xamarin xamarin.forms xamarin.shell
上一篇:XamarinForms5.0的Auth0NuGet包
下一篇:將exisingmapinstance分配給面向物件的新MapView.map時已擁有:xamarin表單中已擁有例外
