首页 文章

Xamarin表单在Visual Studio 2017 XAML验证中

提问于
浏览
2

我是Xamarin.Forms的新手,并尝试使用this nice tutorial学习 .

我已设法在Visual Studio 2017 Community Edition中设置所有内容,并成功部署到模拟和物理设备(仅限Android) .

在各种试验中,我注意到一些XAML错误未突出显示并且构建成功执行,但XAML在运行时失败,导致应用程序崩溃 . 例如 . 拼写样式名称或任何属性值 .

在调试模式下运行只会挂起应用程序 . 打破它表示以下行:

public partial class MainPage : global::Xamarin.Forms.ContentPage {  [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
    private void InitializeComponent() {
        // this is the last line from my code that hands when XAML is invalid
        this.LoadFromXaml(typeof(MainPage));
    }
}

恢复执行会显示出错误的提示(没有新的VS2017异常接口提供的花哨细节):

未处理的异常:Xamarin.Forms.Xaml.XamlParseException:发生

Question: Is there a way to catch invalid XAML errors earlier? Or at least obtain more relevant information? 或者这是Xamarin.Forms的已知限制吗?

更糟糕的是,属性值的自动完成似乎在VS2017中无效(它在VS2015中有效),但这是另一个问题的另一个问题 .

有关安装软件的更多细节:

Microsoft Visual Studio Community 2017
Version 15.0.26228.9 D15RTWSVC
Microsoft .NET Framework
Version 4.6.01586

Installed Version: Community

Xamarin   4.3.0.795 (aece090)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android SDK   7.1.0.41 (9578cdc)
Xamarin.Android Reference Assemblies and MSBuild support.

Xamarin.iOS and Xamarin.Mac SDK   10.4.0.123 (35d1ccd)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

1 回答

  • 2

    要在编译时捕获XAML错误,您必须向AssemblyInfo.cs添加一行:

    [assembly: XamlCompilation(XamlCompilationOptions.Compile)]
    

    有关这方面的更多信息,请访问here .

    不幸的是,VS中的Xamarin.Forms XAML intellisense支持不像WPF或SilverLight,它目前非常有限 . 一个建议可以是在代码中编写你的ui,这样你就可以更快地获得反馈,如果你是Xamarin.Forms XAML的新手,你可以更容易熟悉它,因为代码背后的intellisense .

    另一个选择是使用IDE扩展,在我的情况下,因为我知道它是VS 2017 for Windows所以它不会帮助你,但对于Xamarin Studio for MC,你有像mfractor这样的工具,可以解决你解决的问题 .

相关问题