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>
https://bugzilla.xamarin.com/show_bug.cgi?id=27295
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