简体   繁体   English

基于编译指令的 Xamarin 条件 xaml

[英]Xamarin conditional xaml based on compilation directive

I need the same as Does XAML have a conditional compiler directive for debug mode?我需要与XAML 是否有用于调试模式的条件编译器指令相同 but in Xamarin xaml;但在 Xamarin xaml 中; i've tried the proposed solution but i get this error:我已经尝试了建议的解决方案,但出现此错误:

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

I've tried to use xcc https://github.com/firstfloorsoftware/xcc , but it seems not working with .netstandard 2.0 / Xamarin.Forms 4.5我尝试使用 xcc https://github.com/firstfloorsoftware/xcc ,但它似乎不适用于 .netstandard 2.0 / Xamarin.Forms 4.5

Its little different in XAML of Xamarin.它在 Xamarin 的 XAML 中略有不同。 There is Design time namespacing in Xamarin. Xamarin 中有设计时命名空间。

You would use markup-compatibility xml name space and mark design namespace as Ignorable您将使用标记兼容性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>

In code behind also you can set values在后面的代码中,您也可以设置值

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

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

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