从C#访问样式资源 - Xamarin.Forms

时间:2018-02-07 07:00:30

标签: c# xaml xamarin xamarin.forms

我正在使用Xamarin.Forms应用。我在App.xaml中定义了以下样式

<Application.Resources>
    <ResourceDictionary>
        <Style x:Key="blueButton" TargetType="Button">
            <Setter Property="BackgroundColor"
                    Value="Blue" />
            <Setter Property="TextColor"
                    Value="White" />
        </Style>            
    </ResourceDictionary>
</Application.Resources>

当我想使用MainPage.xaml中的Style时,它工作得很好。

<Button x:Name="CreateGridButton" 
            Margin="0,15,0,0"
            Clicked="CreateGridButton_Clicked"
            Text="Create Grid Layout" Style="{StaticResource blueButton}" />

但是当我想在MainPage.xaml.cs中执行相同操作时,它会显示错误消息&#34;名称&#39; blueButton&#39;在当前上下文中不存在&#34;。

Button createSL = new Button();
        createSL.Text = "Create Stack Layout";
        createSL.Style = (Style)Resources["blueButton"];

我也试过以下,也显示了同样的错误。

createSL.Style = bluebutton;

根据我的要求,我无法在XAML中创建此按钮。所以请帮助我从Code背后做到这一点。

4 个答案:

答案 0 :(得分:4)

因为你在App.xaml中定义了你的风格:

{{1}}

答案 1 :(得分:4)

请尝试这样做

在App构造函数中创建样式,并将其添加到资源中,如下所示:

public App ()
    {
        var buttonStyle = new Style (typeof(Button)) {
            Setters = {
                ...
                new Setter { Property = Button.TextColorProperty,   Value = Color.Teal }
            }
        };

        Resources = new ResourceDictionary ();
        Resources.Add ("blueButton", buttonStyle);
        ...
    }

之后使用此样式并设置为按钮:

Button createSL = new Button();
createSL.Text = "Create Stack Layout";
createSL.Style = (Style)Application.Current.Resources ["blueButton"];

答案 2 :(得分:2)

样式可以在本地和应用程序级别定义:

column_a      unique_column_b_vals
101                  4
102                  3

答案 3 :(得分:0)

上下文:Xamarin表单,C#。

我开发了GetStyleFromApplicationResource()函数,用于在我的项目中从代码加载标签样式。

下面是一个从本地或应用程序ResourceDictionary加载样式的类。随附示例XAML和XAML.cs文件,用于回答原始发布者的问题。

使用@Benl指出本地资源和应用程序资源之间的区别。

//GenFunc.cs
namespace MyApp
{
    public static class GenFunc
    {
        public static Style GetStyleFromLocalResource(ResourceDictionary localresource, String ResourceNameKey)
        {
            if( (localresource.TryGetValue(ResourceNameKey, out Object retValue)) &&
                (retValue is Style value))
            {
                return value;
            }
            throw new Exception($"Style '{ResourceNameKey}' not found in local ResourceDictionary");
        }

        public static Style GetStyleFromApplicationResource(String ResourceNameKey)
        {
            if( (Application.Current.Resources.TryGetValue(ResourceNameKey, out Object retValue)) &&
                (retValue is Style value))
            {
                return value;
            }
            throw new Exception($"Style '{ResourceNameKey}' not found in Application ResourceDictionary");
        }
    }
}

//Test.xaml.cs
using MyApp;
using System;
using System.Reflection;
using Xamarin.Forms;

namespace MyApp.Pages
{
    public partial class Test : ContentPage
    {
        public Test()
        {
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

            Style btnStyle = new Style(typeof(Button));

            try
            {
                lblStyle = GenFunc.GetStyleFromLocalResource(Resources, "blueButton");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            try
            {
                lblStyle = GenFunc.GetStyleFromApplicationResource("blueButton");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Button createSL = new Button();
            createSL.Text = "Create Stack Layout";
            createSL.Style = btnStyle;

            SL0.Children.Add(createSL);
        }
    }
}

//Test.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage Title="Test"
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core" 
             x:Class="MyApp.Pages.Test"
             ios:Page.UseSafeArea="true">
    <ContentPage.Content>
        <StackLayout x:Name="SL0" Margin="10,0">
        </StackLayout>
    </ContentPage.Content>
</ContentPage>