Reputation: 19484
I'm trying to do a very common usage of Xamarin.Forms ListView, where I have multiple types of items.
I'm using a DataTemplateSelector and defining the different (two at this point) views in my XAML file. That requires referencing the c# code from the XAML code through a namesspace definition. And, that's where I'm stuck.
The error I'm getting is
XFC0000 Cannot resolve type "local:NodeTemplateSelector".
Here is my XAML, condensed:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
...
xmlns:local="clr-namespace:varlist">
<ContentPage.Resources>
<ResourceDictionary>
<DataTemplate x:Key="NoteItem">
...
</DataTemplate>
<local:NodeTemplateSelector x:Key="NodeTemplateKey">
NoteTemplate = "{StaticResource NoteItem}"
ImageTemplate = "{StaticResource ImageItem}"
</local:NodeTemplateSelector>
</ResourceDictionary>
</ContentPage.Resources>
</ContentPage>
And, here is C#, also condensed:
namespace varlist
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ListViewPage : ContentPage
{
...
public class NodeTemplateSelector : DataTemplateSelector
{
public DataTemplate NoteTemplate { get; set; }
public DataTemplate ImageTemplate { get; set; }
protected override DataTemplate OnSelectTemplate (object item, BindableObject container)
{
ListView list = (ListView)container;
if (item is NoteData)
return NoteTemplate;
else // item is ImageData
return ImageTemplate;
}
}
}
}
What do I need to change to get the XAML to recognize NodeTemplateSelector ?
Upvotes: 0
Views: 193
Reputation: 19484
As Jason and Shaw suggested, the NodeTemplateSelector must be in top level class.
Also, another problem I ran into is the syntax in the DataTemplate XAML file. Might as well add this note, in case anyone else has the same trouble:
The NodeTemplateSelector in XAML must be defined as direct content; must be like this:
<local:NodeTemplateSelector
x:Key="NodeTemplate"
NoteItemTemplate="{StaticResource NoteTemplate}"
ImageItemTemplate="{StaticResource ImageTemplate}"
/>
Otherwise you get a rather undecipherable runtime error.
Upvotes: 0