合并字典中的资源未加载到标签上



我已经在项目中创建了尺寸和颜色资源字典

根:MyProject.Resources.Values/Dimens/颜色

颜色:

<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyProject.Resources.Values.Colors">
<!-- Colours Light Theme -->

<Color x:Key="BlackColor">#000000</Color>
</ResourceDictionary>

Dimens:

<?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="MyProject.Resources.Values.Dimens">
<x:Double x:Key="FontSize18">18</x:Double>
</ResourceDictionary>

在App类中添加为:

<unity:PrismApplication  xmlns:unity="clr-namespace:Prism.Unity;assembly=Prism.Unity.Forms"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:resources="clr-namespace:MyProject.Resources.Values"
x:Class="MyProject.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<resources:Dimens />
<resources:Colors />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</unity:PrismApplication>

并尝试在页面上使用它们:

<Label Text="Welcome to Xamarin.Forms!"
FontSize="{StaticResource FontSize18}"
TextColor="{StaticResource PrimaryColor}"
VerticalOptions="CenterAndExpand" 
HorizontalOptions="CenterAndExpand" />

但是标签上没有加载任何内容。

您应该从ResourceDictionary文件的根标记中删除x:Class属性。

颜色:

<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
>
<!-- Colours Light Theme -->
<Color x:Key="BlackColor">#000000</Color>
</ResourceDictionary>

Dimens:

<?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:Double x:Key="FontSize18">18</x:Double>
</ResourceDictionary>

then in your app.xaml:

<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Colors.xaml"></ResourceDictionary>
<ResourceDictionary Source="Dimens.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

相关内容

  • 没有找到相关文章

最新更新