Skip to content

Instantly share code, notes, and snippets.

@wislon
Last active November 18, 2020 13:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wislon/c072d0b103282865ef35 to your computer and use it in GitHub Desktop.
Save wislon/c072d0b103282865ef35 to your computer and use it in GitHub Desktop.
Xam.Forms XAML conditional compilation& design-time bindings intellisense in VS

Xam.Forms XAML conditional compilation& design-time bindings intellisense

Original article:

http://adventuresinxamarinforms.com/2015/03/17/xaml-v-code/

add (to each xaml file, near the top with the other xmlns definitions)

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:yourviewmodels="clr-namespace:MyApp.ViewModels;assembly=MyApp"
    d:DataContext="{d:DesignInstance Type=yourviewmodels:NoteViewModel, IsDesignTimeCreatable=False}"

(note that you can provide dummy data and stuff as per http://adventuresinxamarinforms.com/2015/05/20/material-designer-for-xamarin-forms/ by setting IsDesignTimeCreatable=True)

If you have multiple types of layouts or templates (e.g. inside a resource dictionary) you can use the d:DataContext multiple times with different yourviewmodels:blah types, provided you put them on the View aspect of the layout (i.e. inside the StackLayout or GridView blahView).

For example this works (Body and Author.FullName light up):


  <DataTemplate x:Key="NoteItemTemplate" >
  
    <StackLayout x:Name="sl1" Padding="4" Spacing="4" Orientation="Vertical" 
                 d:DataContext="{d:DesignInstance Type=viewModels:NoteViewModel, IsDesignTimeCreatable=True}">
      
      <ContentView HeightRequest="4" />
      <!-- Hack for margin -->
      <Label VerticalOptions="Start" Text="{Binding Body}"/>
      <Label VerticalOptions="Start" Text="{Binding Author.FullName}" />
    </StackLayout>
    
  </DataTemplate>

whereas this does not (Body and Author.FullName are just magic strings):

  <DataTemplate x:Key="NoteItemTemplate" 
                d:DataContext="{d:DesignInstance Type=viewModels:NoteViewModel, IsDesignTimeCreatable=True}">
  
    <StackLayout x:Name="sl1" Padding="4" Spacing="4" Orientation="Vertical" >
      
      <ContentView HeightRequest="4" />
      <!-- Hack for margin -->
      <Label VerticalOptions="Start" Text="{Binding Body}"/>
      <Label VerticalOptions="Start" Text="{Binding Author.FullName}" />
    </StackLayout>
    
  </DataTemplate>

Xaml design-time binding & intellisense (xamarin forums):

http://forums.xamarin.com/discussion/33707/xaml-bindings-not-resolved-in-visual-studio-editor-but-could-be-with-resharper

Bugzilla link for it (maybe one day they'll fix it!)

https://bugzilla.xamarin.com/show_bug.cgi?id=27295

Workaround (there's a nuget package for it)

https://github.com/firstfloorsoftware/xcc (read their small wiki, really useful stuff here!) and check out the xamarin forms howto at https://github.com/firstfloorsoftware/xcc/wiki/How-to:-Xamarin-Forms

(add nuget package to any project containing XAML using mc:Ignorable stuff as per the links above), this strips out any custom attributes you've defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment