简体   繁体   English

如何调试 Xamarin.Forms XAML

[英]How to debug Xamarin.Forms XAML

I'm finding Xamarin.Forms XAML very frustrating.我发现 Xamarin.Forms XAML 非常令人沮丧。

If I use this syntax...如果我使用这种语法...

<ContentView.Resources>
    <local:MyConverter1 x:Key="MyConverter1"/>
</ContentView.Resources>

I will get a System.NullReferenceException from InitializeComponent().我将从 InitializeComponent() 得到 System.NullReferenceException。 Nothing in the stack trace or output window or anywhere else tells me what is wrong.堆栈跟踪或输出窗口或其他任何地方都没有告诉我出了什么问题。
Note: this syntax works fine in WPF.注意:此语法在 WPF 中运行良好。

After a lot of struggle I discovered I need this syntax...经过一番努力,我发现我需要这种语法......

<ContentView.Resources>
    <ResourceDictionary>
        <local:MyConverter1 x:Key="MyConverter1"/>
    </ResourceDictionary>
</ContentView.Resources>

Likewise for ListView DataTemplate.对于 ListView DataTemplate 也是如此。 This throws null reference exception...这会引发空引用异常...

            <ListView.ItemTemplate>
                <DataTemplate>
                    <Label Text="{Binding Converter={StaticResource MyConverter1}}"/>
                </DataTemplate>
            </ListView.ItemTemplate>

Because the proper syntax is this...因为正确的语法是这样的......

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Label Text="{Binding Converter={StaticResource MyConverter1}}"/>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>

Again this syntax works fine in WPF.同样,此语法在 WPF 中也能正常工作。 I fully realize that Xamarin.Forms is not WPF but I getting weary of being sucker punched by null reference exceptions whenever I use a XAML construct that is valid in WPF.我完全意识到 Xamarin.Forms 不是 WPF,但是每当我使用在 WPF 中有效的 XAML 构造时,我都厌倦了被空引用异常打击。

What is the best way to debug Xamarin.Forms XAML issues?调试 Xamarin.Forms XAML 问题的最佳方法是什么?

Right now I am simply commenting stuff out until it starts working.现在我只是在评论内容,直到它开始工作。 This is akin to putting print statements in imperative code.这类似于在命令式代码中放置打印语句。 Declarative code is supposed to be superior to imperative code.声明式代码应该优于命令式代码。

What am I doing wrong?我究竟做错了什么?

I'm finding Xamarin Forms XAML very frustrating.我发现Xamarin Forms XAML非常令人沮丧。

If I use this syntax...如果我使用这种语法...

<ContentView.Resources>
    <local:MyConverter1 x:Key="MyConverter1"/>
</ContentView.Resources>

I will get a System.NullReferenceException from InitializeComponent().我将从InitializeComponent()得到一个System.NullReferenceException。 Nothing in the stack trace or output window or anywhere else tells me what is wrong.堆栈跟踪或输出窗口或其他任何地方都没有告诉我什么地方不对。 Note: this syntax works fine in WPF.注意:此语法在WPF中可以正常工作。

After a lot of struggle I discovered I need this syntax...经过大量的努力,我发现我需要这种语法...

<ContentView.Resources>
    <ResourceDictionary>
        <local:MyConverter1 x:Key="MyConverter1"/>
    </ResourceDictionary>
</ContentView.Resources>

Likewise for ListView DataTemplate.对于ListView DataTemplate也是如此。 This throws null reference exception...这会引发空引用异常...

            <ListView.ItemTemplate>
                <DataTemplate>
                    <Label Text="{Binding Converter={StaticResource MyConverter1}}"/>
                </DataTemplate>
            </ListView.ItemTemplate>

Because the proper syntax is this...因为正确的语法是这样的...

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Label Text="{Binding Converter={StaticResource MyConverter1}}"/>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>

Again this syntax works fine in WPF.同样,此语法在WPF中也能正常工作。 I fully realize that Xamarin Forms is not WPF but I getting weary of being sucker punched by null reference exceptions whenever I use a XAML construct that is valid in WPF.我完全意识到Xamarin Forms不是WPF,但是每当我使用在WPF中有效的XAML构造时,我都会因被空引用异常困扰而感到厌倦。

What is the best way to debug Xamarin Forms XAML issues?调试Xamarin Forms XAML问题的最佳方法是什么? Right now I am simply commenting stuff out until it starts working.现在,我只是将内容注释掉,直到它开始起作用。 This is akin to putting print statements in imperative code.这类似于将打印语句放入命令性代码中。 Declarative code is supposed to be superior to imperative code.声明性代码应该比命令性代码优越。 What am I doing wrong?我究竟做错了什么?

I'm finding Xamarin Forms XAML very frustrating.我发现Xamarin Forms XAML非常令人沮丧。

If I use this syntax...如果我使用这种语法...

<ContentView.Resources>
    <local:MyConverter1 x:Key="MyConverter1"/>
</ContentView.Resources>

I will get a System.NullReferenceException from InitializeComponent().我将从InitializeComponent()得到一个System.NullReferenceException。 Nothing in the stack trace or output window or anywhere else tells me what is wrong.堆栈跟踪或输出窗口或其他任何地方都没有告诉我什么地方不对。 Note: this syntax works fine in WPF.注意:此语法在WPF中可以正常工作。

After a lot of struggle I discovered I need this syntax...经过大量的努力,我发现我需要这种语法...

<ContentView.Resources>
    <ResourceDictionary>
        <local:MyConverter1 x:Key="MyConverter1"/>
    </ResourceDictionary>
</ContentView.Resources>

Likewise for ListView DataTemplate.对于ListView DataTemplate也是如此。 This throws null reference exception...这会引发空引用异常...

            <ListView.ItemTemplate>
                <DataTemplate>
                    <Label Text="{Binding Converter={StaticResource MyConverter1}}"/>
                </DataTemplate>
            </ListView.ItemTemplate>

Because the proper syntax is this...因为正确的语法是这样的...

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Label Text="{Binding Converter={StaticResource MyConverter1}}"/>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>

Again this syntax works fine in WPF.同样,此语法在WPF中也能正常工作。 I fully realize that Xamarin Forms is not WPF but I getting weary of being sucker punched by null reference exceptions whenever I use a XAML construct that is valid in WPF.我完全意识到Xamarin Forms不是WPF,但是每当我使用在WPF中有效的XAML构造时,我都会因被空引用异常困扰而感到厌倦。

What is the best way to debug Xamarin Forms XAML issues?调试Xamarin Forms XAML问题的最佳方法是什么? Right now I am simply commenting stuff out until it starts working.现在,我只是将内容注释掉,直到它开始起作用。 This is akin to putting print statements in imperative code.这类似于将打印语句放入命令性代码中。 Declarative code is supposed to be superior to imperative code.声明性代码应该比命令性代码优越。 What am I doing wrong?我究竟做错了什么?

I normally have 2 steps我通常有 2 个步骤

  1. add a try catch in code behind在后面的代码中添加一个 try catch

    using System;使用系统; using System.Diagnostics;使用 System.Diagnostics; using Xamarin.Forms;使用 Xamarin.Forms;

    namespace TestApp.Views { public partial class AssetsPage : ContentPage { public WelcomePage() { try { InitializeComponent();命名空间 TestApp.Views { 公共部分类 AssetsPage : ContentPage { public WelcomePage() { try { InitializeComponent(); } catch (Exception ex) { Debug.WriteLine(ex); } catch (Exception ex) { Debug.WriteLine(ex); } } } } } } } }

  2. Remove code and keep on checking if the problem disappears删除代码并继续检查问题是否消失

not really a magic solution but it helps the most especially when you have large groups of complicated code不是真正的神奇解决方案,但它最有帮助,尤其是当您有大量复杂的代码时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM