Xamarin conditional xaml based on compilation directive

Viewed 314
1 Answers

Its little different in XAML of Xamarin. There is Design time namespacing in Xamarin.

You would use markup-compatibility xml name space and mark design namespace as 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
        }
    }
}
Related