是否有可能有在XAML样式的静态列表 [英] Is it possible to have a static list of Styles in Xaml

查看:188
本文介绍了是否有可能有在XAML样式的静态列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 XAML中有样式的静态列表

I would like to have a static list of Styles in Xaml

到目前为止,我曾尝试:

So far I have tried:

<local:Styles xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MyApp.Core;assembly=MyApp.Core">
  <Style x:Key="labelStyle" TargetType="Label">
    <Setter Property="TextColor" Value="Green" />
  </Style>
</local:Styles>



代码隐藏

public partial class Styles : List<Style>
{
    public Styles()
    {
    }
}

但是当我做

var styles = new Styles();



类是空的。

The class is empty.

由于顺便说一句,我不能用应用程序资源 ResourceDictionary中

As an aside I can't use Application Resources or ResourceDictionary

推荐答案

您可以将您的样式在 ResourceDictionary中(添加 - >新建项目 - >资源词典):

You can place your styles in a ResourceDictionary (Add -> New Item -> Resource Dictionary):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!-- Your styles here -->
    <Style ...

</ResourceDictionary>



不要忘了,你需要在添加到它的引用App.xaml中

<Application x:Class="Your.App.Namespace"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Your.App.Namespace;component/Path/To/Dictionary.xaml"/>
            ...

要获取这些样式代码隐藏不放,可以使用在 FindResource 方法:

To get hold of these styles in code-behind, you can use the FindResource method:

Style myStyle = App.Current.FindResource("MyStyleKey") as Style;

这篇关于是否有可能有在XAML样式的静态列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆