Issue
I added CustomDataTemplateSelector to a ResourceDictionary in a separated file called DataTemplates.xaml which I subsequently linked in App.xaml
When I run the app, I get XamlParseException saying: Type local:CustomDataTemplateSelector not found in xmlns clr-namespace:MyProject
On the other hand, when I put the content of DataTemplates.xaml to App.xaml, it works just fine.
App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary Source="../DataTemplates.xaml"/>
DataTemplates.xaml
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyProject">
<DataTemplate x:Key="FirstCell">
<Label Text="Some text"/>
</DataTemplate>
<DataTemplate x:Key="SecondCell">
<Label Text="Some other text"/>
</DataTemplate>
<local:CustomDataTemplateSelector
x:Key="CustomDataTemplateSelector"
SecondTemplate="{StaticResource SecondCell}"
FirstTemplate="{StaticResource FirstCell}"/>
Solution
I've confirmed the problem. (To clarify for others - the parse error occurs when attempt to run the app. During App.InitializeComponent.) I had xmlns:local ... on both App.xaml and DataTemplates.xaml.
To work-around this, use an alternative syntax for merging dictionaries, ResourceDictionary.MergedDictionaries
:
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
xmlns:local="clr-namespace:XFSOAnswers"
x:Class="XFSOAnswers.App">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="ContentPage" ApplyToDerivedTypes="True">
<Setter Property="ios:Page.UseSafeArea" Value="True"/>
</Style>
<ResourceDictionary.MergedDictionaries>
<local:DataTemplates />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
In case it matters, here is what I had for DataTemplates:
DataTemplates.xaml:
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XFSOAnswers">
<DataTemplate x:Key="FirstCell">
<Label Text="Some text"/>
</DataTemplate>
<DataTemplate x:Key="SecondCell">
<Label Text="Some other text"/>
</DataTemplate>
<local:CustomDataTemplateSelector
x:Key="CustomDataTemplateSelector"
SecondTemplate="{StaticResource SecondCell}"
FirstTemplate="{StaticResource FirstCell}"/>
</ResourceDictionary>
DataTemplates.xaml.cs:
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace XFSOAnswers
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DataTemplates : ResourceDictionary
{
public DataTemplates()
{
}
}
}
(The original Source
syntax should not need this .cs file. I don't know whether the syntax I used needs it or not.)
Answered By - ToolmakerSteve
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.