我的页面看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Japanese;assembly=Japanese" xmlns:template="clr-namespace:Japanese.Templates" xmlns:t="clr-namespace:Japanese.Views.HelpTab.Xaml" x:Class="Japanese.Views.HelpTab.AssignMode" Title="Assign Mode Help" BackgroundColor="{DynamicResource PageBackgroundColor}">
<ContentPage.Content>
<ScrollView>
<StackLayout Spacing="0">
<StackLayout.Resources>
<Style TargetType="Frame" CanCascade="true">
<Setter Property="Style" Value="{StaticResource FrameBorder}" />
</Style>
<Style TargetType="Label" CanCascade="true">
<Setter Property="TextColor" Value="{DynamicResource HelpTextColor}" />
<Setter Property="Style" Value="{StaticResource HD}" />
</Style>
<Style TargetType="Grid" CanCascade="true">
<Setter Property="Style" Value="{StaticResource HG}" />
</Style>
</StackLayout.Resources>应用程序中大约有20个页面都添加了相同的资源集,但不是每个页面都添加了相同的资源集。
有没有一种方法可以在我自己的页面的C#后端添加资源,然后对这20个页面使用enter code here,这样我所需要做的就是这样:
<?xml version="1.0" encoding="UTF-8"?>
<MyContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Japanese;assembly=Japanese" xmlns:template="clr-namespace:Japanese.Templates" xmlns:t="clr-namespace:Japanese.Views.HelpTab.Xaml" x:Class="Japanese.Views.HelpTab.AssignMode" Title="Assign Mode Help" BackgroundColor="{DynamicResource PageBackgroundColor}">
<ContentPage.Content>
<ScrollView>
<StackLayout Spacing="0">或者,有没有什么方法可以将所有这些资源组合成一个,然后添加它,而不是添加三个单独的样式?
发布于 2019-02-04 17:19:38
当然,在这一领域你可以做一些事情,但我认为知道有implicit和explicit风格会对你有很大帮助。
首先转到您的App.xaml并在那里添加您的样式。您应该会看到<Application.Resources>节点已经存在。您可以在其中添加<ResourceDictionary>,现在可以根据需要定义任何资源或样式。这些将在整个应用程序中可用。
例如,在您的示例中,如下所示:
<Application.Resources>
<ResourceDictionary>
<Style TargetType="Frame" CanCascade="true">
<Setter Property="Style" Value="{StaticResource FrameBorder}" />
</Style>
<Style TargetType="Label" CanCascade="true">
<Setter Property="TextColor" Value="{DynamicResource HelpTextColor}" />
<Setter Property="Style" Value="{StaticResource HD}" />
</Style>
<Style TargetType="Grid" CanCascade="true">
<Setter Property="Style" Value="{StaticResource HG}" />
</Style>
</ResourceDictionary>
</Application.Resources>因为您指定的是TargetType而不是x:Key,所以这是一种隐式样式。它将应用于该目标类型的所有元素。现在有两件事可以做;或者创建控件的继承并将样式仅应用于该继承,或者创建显式样式。然后将x:Key="MyStyle"属性添加到样式,并将Style="{StaticResource MyStyle}"添加到要应用样式的每个元素。有关样式的更多信息可以在Microsoft Docs上阅读。
发布于 2019-02-04 17:54:43
您可以在App.xaml中定义资源并在整个项目中使用
Application.Current.Resources["AppColor"];如果要在xaml页面中使用,请使用类似
<Label TextColor={Binding StaticResource AppColor}/>https://stackoverflow.com/questions/54512838
复制相似问题