有没有办法在Xamarin.Forms中将样式与我的XAML分开

Yoe*_*eri 10 c# xaml xamarin xamarin.forms

我在带有XAML页面的PCL中使用Xamarin.Forms.我想出的控制样式的唯一方法是使用内联语法.

<Button Text="Inquiry" TextColor="Blue" />
Run Code Online (Sandbox Code Playgroud)

我更喜欢使用像这样的结构:

<Page.Resources>
    <Style TargetType="Button">
        <Setter Property="BorderThickness" Value="5" />
        <Setter Property="Foreground" Value="Blue" />
    </Style>
</Page.Resources>
Run Code Online (Sandbox Code Playgroud)

(http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh465381.aspx)

但是,尚未支持Style元素.有没有人成功地将布局与内容分开?

仅供参考:我也在Xamarin论坛上发布了这个问题,所以任何通过谷歌到这里的人都可能想查看这个页面:http://forums.xamarin.com/discussion/19287/styling-of-xamarin-xaml #最新

Ste*_*oix 5

风格并不那么难[引证需要].您可以实现自己的,就像我刚才为了这个答案所做的那样.

这是Xaml的样子:

<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:YourNS;assembly=YourAssembly">
    <ContentPage.Resources>
        <ResourceDictionary>
            <local:Style x:Key="buttonStyle">
                <local:Setter Property="BorderWidth" Value="5"/>
            </local:Style>
        </ResourceDictionary>
    </ContentPage.Resources>

    <Button Text="Foo" local:Style.Style="{StaticResource buttonStyle}" x:Name="button"/>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

支持代码如下:

namespace YourNS
{

    public class Setter {
        public string Property { get; set; }
        public string Value { get; set; }
    }

    [ContentProperty ("Children")]
    public class Style
    {
        public Style ()
        {
            Children = new List<Setter> ();
        }

        public IList<Setter> Children { get; private set; }

        public static readonly BindableProperty StyleProperty = 
            BindableProperty.CreateAttached<Style, Style> (bindable => GetStyle (bindable), default(Style), 
                propertyChanged: (bindable, oldvalue, newvalue)=>{
                    foreach (var setter in newvalue.Children) {
                        var pinfo = bindable.GetType().GetRuntimeProperty (setter.Property);
                        pinfo.SetMethod.Invoke (bindable,new [] {Convert.ChangeType (setter.Value, pinfo.PropertyType.GetTypeInfo())});
                    }

                });

        public static Style GetStyle (BindableObject bindable)
        {
            return (Style)bindable.GetValue (StyleProperty);
        }

        public static void SetStyle (BindableObject bindable, Style value)
        {
            bindable.SetValue (StyleProperty, value);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

显然,执行分配的代码非常轻,您可能必须根据需要进行调整(支持枚举等),但它适用于这种简单的情况.

我相信它会有所帮助.


Sco*_*rod 3

样式支持现已可用:

<ContentPage.Resources>
   <ResourceDictionary>

  <Style TargetType="Label">
    <Setter Property="HorizontalOptions" Value="LayoutOptions.Center" />
    <Setter Property="VerticalOptions" Value="LayoutOptions.Center" />
    <Setter Property="FontSize" Value="48" />
    <Setter Property="FontAttributes" Value="Bold, Italic" />
    <Setter Property="Opacity" Value=".5" />
    <Setter Property="TextColor" Value="Black" />
    <Setter Property="Text" Value="Copied" />
    <Setter Property="IsVisible" Value="False" />
  </Style>

  </ResourceDictionary>
</ContentPage.Resources>
Run Code Online (Sandbox Code Playgroud)