繁体   English   中英

有什么方法可以在C#后端的页面中添加资源吗?

[英]Is there a way I can add resources to a page in the C# backend?

我的页面看起来像这样:

<?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>

应用程序中大约有二十个页面,所有页面都添加了相同的资源集,但并不是每个页面。

有没有一种方法可以将资源添加到我自己页面的C#后端中,然后enter code here使用enter code here这20个页面,因此我所需要做的就是这样:

<?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">

或者,是否有某种方法可以将所有这些资源合并为一个资源并添加,而不是添加三个单独的样式?

当然,在这方面您可以做一些事情,但是我认为知道隐式显式样式已经对您有很大帮助。

首先转到您的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

您可以在App.xaml中定义资源,并在整个项目中使用

Application.Current.Resources["AppColor"];

如果要在xaml页面中使用,请使用类似

<Label TextColor={Binding StaticResource AppColor}/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM