简体   繁体   English

在C#后端中将资源声明为Class时,如何访问资源?

[英]How can I access a resource when it's declared as a Class in C# back end?

My main resources look like this: 我的主要资源如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:converters="clr-namespace:Japanese" 
             xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             x:Class="Japanese.App">
    <Application.Resources>
        <ResourceDictionary>
        <ResourceDictionary Source="/Resources/DetailRes.xaml" />

I have this resource file: 我有这个资源文件:

<?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="Japanese.DetailRes">
    <Style x:Key="DetailLabel" TargetType="Label">
        <Setter Property="FontSize">
            <Setter.Value>
                <OnPlatform x:TypeArguments="x:Double">
                    <On Platform="iOS" Value="35" />
                    <On Platform="Android" Value="35" />
                </OnPlatform>
            </Setter.Value>
        </Setter>
        <Setter Property="VerticalOptions" Value="Center" />
        <Setter Property="LineBreakMode" Value="WordWrap" />
        <Setter Property="HorizontalTextAlignment" Value="Center" />
    </Style>
</ResourceDictionary>

In XAML I access it like this: 在XAML中,我这样访问它:

<t:BaseFrameButtonTemplate xmlns="http://xamarin.com/schemas/2014/forms" 
                  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
                  xmlns:t="clr-namespace:Japanese.Templates" 
                  xmlns:local="clr-namespace:Japanese;assembly=Japanese" 
                  x:Class="Japanese.Templates.RoundButtonText" x:Name="this" >

    <Label Text="{Binding Text, Source={x:Reference this}}" 
           Style="{StaticResource DetailRes}" 
           x:Name="Label" />
</t:BaseFrameButtonTemplate>

I would like to access this in C# but do not know how to do it. 我想在C#中访问它,但不知道该怎么做。 I tried this but it does not find the resource: 我试过了,但是找不到资源:

Label.Style = (Style)Application.Current.Resources["DetailRes"];

You can use the TryGetValue to obtain a single from your ResourceDictionary : 您可以使用TryGetValueResourceDictionary获取一个:

if (Application.Current.Resources.TryGetValue("DetailLabel", out object style))
{
    someLabel.Style = (Style)style;
}

暂无
暂无

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

相关问题 使用 C#,当用作 static 资源时,如何访问 ViewModel 的属性和方法? - Using C#, how can I access the ViewModel's properties and methods when used as a static resource? 有什么办法可以在使用C#后端时加快向页面添加新元素的速度吗? - Is there any way that I can speed up the adding of new elements to a page when using the C# back end? 在C#后端,如何在分段控件中选择元素? - In the C# back end, how can I select an element in a Segmented Control? 如何将DynamicResource的值发送到自定义控件C#后端? - How can I send the value of a DynamicResource to my custom controls C# back end? 如何在后端C#和渲染器之间共享字段的值? - How can I share the value of a field between back-end C# and a renderer? 如何在具有C#后端而不是XAML的模板上绑定XAML元素? - How can I bind a XAML element on a template with the C# back end instead of in the XAML? 如何在C#后端的模板中更改标签的颜色? - How can I change the color of a Label in a template in the C# back end? 如果我在XAML中使用ContentPage.BindingContext定义绑定上下文,那么如何在C#后端访问它? - If I use ContentPage.BindingContext in XAML to define my binding context then how do I access that in the C# back end? 有没有办法可以在我的 C# 代码中访问静态资源? - Is there a way I can access a static resource in my C# code? 我可以将XAML网格的高度绑定回我的C#后端代码吗? - Can I bind the Height of a XAML Grid back to my C# back end code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM