C# 已引发System.TypeNizilationException异常
我正在使用xamarin表单,它一直工作正常,但突然我的应用程序在启动时崩溃,出现错误:System.TypeNizilationException被抛出,它显示了我的app.Xaml.cs类:C# 已引发System.TypeNizilationException异常,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我正在使用xamarin表单,它一直工作正常,但突然我的应用程序在启动时崩溃,出现错误:System.TypeNizilationException被抛出,它显示了我的app.Xaml.cs类: using Xamarin.Forms; namespace MyApp { public partial class App : Application { public App() { InitializeComponent(
using Xamarin.Forms;
namespace MyApp
{
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new AppPage());
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
它突出显示行MainPage=newNavigationPage(newAppPage())代码>作为问题的根源,是什么导致了问题,如何解决
以下是AppPage类的代码:
using Xamarin.Forms;
using Microsoft.WindowsAzure.MobileServices;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyApp
{
public partial class AppPage : ContentPage
{
public AppPage()
{
InitializeComponent();
}
protected override async void OnAppearing()
{
base.OnAppearing();
//Code
}
}
}
App.Xaml类:
<?xml version="1.0" encoding="utf-8"?>
<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyApp.app">
<Application.Resources>
<!-- Application resource dictionary -->
</Application.Resources>
</Application>
内部异常:
System.PlatformNotSupportedException: The empty PCL implementation for Microsoft Azure Mobile Services was loaded. Ensure you have added nuget package to each of your platform projects.
at Microsoft.WindowsAzure.MobileServices.Platform.get_Instance () [0x00007] in <7910de25d05d49d9b3c2d648cd285e40>:0
at Microsoft.WindowsAzure.MobileServices.MobileServiceClient.GetApplicationInstallationId () [0x00004] in <7910de25d05d49d9b3c2d648cd285e40>:0
at Microsoft.WindowsAzure.MobileServices.MobileServiceClient..ctor (System.Uri mobileAppUri, System.Net.Http.HttpMessageHandler[] handlers) [0x0005f] in <7910de25d05d49d9b3c2d648cd285e40>:0
at Microsoft.WindowsAzure.MobileServices.MobileServiceClient..ctor (System.String mobileAppUri, System.Net.Http.HttpMessageHandler[] handlers) [0x00008] in <7910de25d05d49d9b3c2d648cd285e40>:0
at ChurchBuilder.ChurchBuilderPage..cctor () [0x00000] in /Users/taylordowns/Projects/ChurchBuilder/ChurchBuilder/ChurchBuilderPage.xaml.cs:24
System.PlatformNotSupportedException:已加载Microsoft Azure移动服务的空PCL实现。确保已将nuget包添加到每个平台项目中。
位于:0中的Microsoft.WindowsAzure.MobileServices.Platform.get_实例()[0x00007]
位于:0中的Microsoft.WindowsAzure.MobileServices.MobileServiceClient.GetApplicationInstallationId()[0x00004]
在Microsoft.WindowsAzure.MobileServices.MobileServiceClient..ctor(System.Uri mobileAppUri,System.Net.Http.HttpMessageHandler[]handlers)[0x0005f]中:0
位于:0中的Microsoft.WindowsAzure.MobileServices.MobileServiceClient..ctor(System.String mobileAppUri,System.Net.Http.HttpMessageHandler[]handlers)[0x00008]
在/Users/taylordowns/Projects/ChurchBuilder/ChurchBuilder/ChurchBuilderPage.xaml.cs:24中的ChurchBuilder.ChurchBuilderPage..cctor()[0x00000]
对于这些类型的Xamarin良性问题和错误,您需要经历一个消除过程,并获取尽可能多的信息来诊断问题
1。排除环境因素
- 关闭IDE(visualstudio或任何东西),删除所有项目的bin和obj目录
- 如果您使用的是Emulator,请在Emulator中手动删除项目并关闭Emulator
- 重新启动并重建,然后再次运行应用程序
我知道这似乎有点迂腐,但你会惊讶地发现你经常这样做
这很有帮助
2。获取尽可能多的错误信息
- 引发异常时,按continue并在output(输出)窗口中查找更多线索
- 另外(这应该是一个不需要思考的问题),在抛出错误的行周围放置一个try-catch,并在内部异常和堆栈跟踪中查找更多信息
例如
try
{
InitializeComponent();
MainPage = new NavigationPage(new AppPage());
}
catch (Exception ex)
{ // break point here and inspect ex
}
详细查看异常,查看所有内部异常和
当然,请深入查看堆栈跟踪
3。使用页面和主应用程序类上的compilation属性检查Xaml。
- 在大多数情况下,出于几个原因,它都应该打开,但是它会对Xaml进行编译检查,并确保没有明显的错误
属性
[XamlCompilation(XamlCompilationOptions.Compile)]
从
可以选择使用XAML编译器(XAMLC)将XAML直接编译成中间语言(IL)
XAMLC提供了许多好处:
- 它执行XAML的编译时检查,通知用户任何错误李>
- 它删除了XAML元素的一些加载和实例化时间李>
- 通过不再包含.xaml文件,它有助于减小最终部件的文件大小李>
默认情况下禁用XAMLC以确保向后兼容性。通过添加
XamlCompilation属性
4。开始在有问题的页面和应用程序中注释Xaml。Xaml
- 这也是一个不需要动脑筋的问题。如果每个页面上都抛出
TypeNizilizationException
,那么它可能是位于App.Xaml
- 开始对你的
App.Xaml
和页面的大部分内容进行注释,直到有东西开始工作
我知道这是极端的,但它很快就会发现问题,或者
至少要隔离它
阅读
- 然后再问问题。包括所有相关代码,仅此而已,请在您的问题中包含完整错误。给出错误类型是不够的。大多数情况下,错误消息(尽管有时含糊不清)和堆栈跟踪都包含您(和其他人)识别问题所需的信息
AppPage的构造器是什么样子的?@Jason更新了这个问题,将其包括在内。请尝试解决这个问题。内部异常中可能有更多信息。此外,如果您正在调试,您可以按continue并查看输出窗口,有时您可以从那里收集更多信息。最后,开始在你的应用页面上评论xmal。另外,我要做的第一件事是删除bin和obj目录并重新启动visual studio,以防万一XAML中可能有什么东西导致了它-如@Saruman所建议的,查看InnerException以获取更多信息,如果尚未启用XAML编译,通常只是做一些调试来获得更多info@Jason我知道它不是特定页面或xaml,因为我尝试将它设置为其他页面,但它仍然抛出相同的异常