c# - xamarin forms InitializeComponent 挂起

标签 c# android xamarin xamarin.forms

我正在使用 visual studio 2017 在 C# 中创建默认的 Xamarin Forms PCL 项目。如果我XAML 中出现任何错误,InitializeComponent() 调用将挂起。没有编辑器、编译器、构建器或运行时错误。有没有办法研究错误的原因?我很确定 Visual Studio 2015 引发了运行时错误。

这是一个引用了缺失样式的示例。这会导致 InitializeComponent() 挂起。我正在使用 VisualStudio_android-23_x86_phone 模拟器,VS 对我尝试过的每个模拟器以及我的三星 Note 4 都做同样的事情。谢谢。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Broken"
             x:Class="Broken.MainPage">
    <Label Text="Welcome to Xamarin Forms!" 
           VerticalOptions="Center" 
           HorizontalOptions="Center"
           Style="{StaticResource myStyle}"/>

</ContentPage>

最佳答案

答案非常简单。将 InitializeComponent 放在 try/catch 中。即使 InitializeComponent 在检测到 XAML 解析错误时没有返回,您也可以捕获异常并且 ex.message 很有用。

try
            {
                InitializeComponent();
            }
            catch (Exception ex)
            {
                throw ex;
            }

显示消息“Xamarin.Forms.Xaml.XamlParseException:位置 9:12。找不到键 myStyle 的 StaticResource”。我没想到异常会被捕获,但我偶然错了。

感谢 Stephane 提供的“燕子”线索。

关于c# - xamarin forms InitializeComponent 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44036565/

相关文章:

c# - String.Format 某些参数

c# - Linq Groupby 多个同名列(匿名类型不能有多个同名属性)

android - Android Plugin for Gradle 中的 "minifyEnabled"和 "useProguard"有什么区别?

c# - Xamarin Forms OAuth 2.0 和 Azure Active Directory - 错误 AdalServiceException : AADSTS7000218

xamarin - 如何将可观察集合绑定(bind)到 xamarin 中的 flexlayout?

c# - 在 C# 中从当前窗体调用新窗体

c# - 使用Entity Framework 为什么要重新启动DbContext?

java - 我需要在 Android 中关闭来自 FileReader() 的流吗?

Android:组件的 IntentFilters

android - 非 Activity 中的 Xamarin ContentResolver/CursorLoader 访问