我有一个具有以下结构的Xamarin项目:
MyApp的
-转换器
-模型
-样式
-Colors.xaml
-的ViewModels
-视图
-ListPage.xaml
-ListPage.xaml.cs
我正在尝试将Colors.xaml用作我的整个应用程序配色方案的外部资源字典。我已经将源代码添加到“ ListPage.xaml”资源中,如下所示:
<ContentPage.Resources>
<ResourceDictionary Source="Styles/Colors.xaml"/>
<!--<ResourceDictionary>
<converter:StatusToColorConverter x:Key="StatusToColorConverter" />
</ResourceDictionary>-->
</ContentPage.Resources>
当我尝试构建项目时,出现了一些似乎以该错误开头的构建错误
Resource "Styles/Colors.xaml not found.
我已经尝试根据此文档从Colors.xaml文件以及代码隐藏(显然在我的结构中没有一个)文件中删除类标签,但这仍然不能解决问题。我也将相同的内容添加<ResourceDictionary Source="Styles/Colors.xaml"/>
到我的App文件中无济于事。为了将ListPage.xaml定向到Colors.xaml文件,我缺少什么?
以下所有相关代码:
Colors.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">
<!--Color Scheme-->
<Color x:Key="ColorBackground">#333333</Color>
<Color x:Key="ColorObjectBackground">#FFFFFF</Color>
<Color x:Key="ColorTierOneText">#3c3c3c</Color>
<Color x:Key="ColorTierTwoText">#999999</Color>
</ResourceDictionary>
ListPage.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converter="clr-namespace:MyApp"
mc:Ignorable="d"
x:Class="MyApp.Views.ListPage"
Title="this is the title">
<ContentPage.BackgroundColor>
<OnPlatform x:TypeArguments="Color">
<On Platform="GTK" Value="#3c3c3c" />
</OnPlatform>
</ContentPage.BackgroundColor>
<ContentPage.Resources>
<ResourceDictionary Source="Styles/Colors.xaml"/>
<!--<ResourceDictionary>
<converter:StatusToColorConverter x:Key="StatusToColorConverter" />
</ResourceDictionary>-->
</ContentPage.Resources>
<StackLayout>
<Label x:Name="NotFoundMessage"
IsVisible="False"
Text="No results found."
HorizontalTextAlignment="Center"
TextColor="ColorTierTwoText"
Padding="5"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
应用程式
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="MyApp.App">
<Application.Resources>
<ResourceDictionary Source="Styles\Colors.xaml"/>
</Application.Resources>
</Application>
您提供的路径似乎错误Styles\Colors
的路径类型仅适用于Android。
要添加外部文件,您需要添加xmlns参考。
并且您缺少x:Class属性。只需像ContentPage一样添加它,然后更改Base类。它必须进行编译才能正常工作。
添加x:Class="MyApp"
到Colors.xaml ResourceDictionary属性
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp">
后面的代码
public partial class Colors : ResourceDictionary
{
public Colors()
{
InitializeComponent();
}
}
在App.xaml中
xmlns:local="clr-namespace:WhereColorsNamespace."
另外你需要增加使用 MergedDictionary
<ResourceDictionary.MergedDictionaries>
<local:Colors x:Key="colors"/>
</ResourceDictionary.MergedDictionaries>
参考:https : //xamarinhelp.com/merged-dictionaries-xamarin-forms/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句