首页 > 解决方案 > 基于编译指令的 Xamarin 条件 xaml

问题描述

我需要与 XAML 是否有用于调试模式的条件编译器指令相同? 但在 Xamarin xaml 中;我已经尝试了建议的解决方案,但出现此错误:

Type mc:Choice not found in xmlns http://schemas.openxmlformats.org/markup-compatibility/2006

我尝试使用 xcc https://github.com/firstfloorsoftware/xcc,但它似乎不适用于 .netstandard 2.0 / Xamarin.Forms 4.5

标签: c#xamlxamarinxamarin.forms

解决方案


它在 Xamarin 的 XAML 中略有不同。Xamarin 中有设计时命名空间。

您将使用标记兼容性xml 名称空间并将设计名称空间标记为Ignorable

<?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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://xamarin.com/schemas/2014/forms/design" 
             xmlns:ref="clr-namespace:XamUtilities.Views;assembly=XamUtilities" 
             mc:Ignorable="d" 
             x:Class="Sample.MainPage">
    <ContentPage.Content>
        <d:Label Text="This will be displayed only in previewer"/>
        <Label Text="This will be displayed in actual app"/>
    </ContentPage.Content>
</ContentPage>

在后面的代码中,您也可以设置值

[DesignTimeVisible (true)]
public partial class MainPage : ContentPage
{
    public MainPage ()
    {
        InitializeComponent ();
        if (DesignMode.IsDesignModeEnabled) 
        {
            // Previewer only code  
            //add your bindings in here
        }
    }
}

推荐阅读