Xamarin shell正在初始化API控制器



我有一个正在运行的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());

在公共应用程序((中,我将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服务控制器吗?

无论何时使用控制器,都可以延迟加载控制器。

应用程序

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;

相关内容

  • 没有找到相关文章

最新更新