我將 CustomDataTemplateSelector 添加到一個名為 DataTemplates.xaml 的單獨檔案中的 ResourceDictionary,隨后我在 App.xaml 中鏈接了該檔案
當我運行應用程式時,我收到 XamlParseException 說:Type local:CustomDataTemplateSelector not found in xmlns clr-namespace:MyProject
另一方面,當我將 DataTemplates.xaml 的內容放入 App.xaml 時,它作業得很好。
應用程式.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary Source="../DataTemplates.xaml"/>
資料模板.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}"/>
uj5u.com熱心網友回復:
我已經確認了問題。(為其他人澄清 - 嘗試運行應用程式時發生決議錯誤。在 App.InitializeComponent 期間。)我在 App.xaml 和 DataTemplates.xaml 上都有 xmlns:local ...。
要解決此問題,請使用替代語法來合并字典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>
如果重要,這是我對 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>
資料模板.xaml.cs:
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace XFSOAnswers
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DataTemplates : ResourceDictionary
{
public DataTemplates()
{
}
}
}
(原來的Source語法應該不需要這個.cs檔案,不知道我用的語法是否需要。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/402245.html
標籤:沙马林 xamarin.forms
上一篇:系結不會重繪標簽的內容
