首页 文章

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

提问于
浏览
10

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

<Button Text="Inquiry" TextColor="Blue" />

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

<Page.Resources>
    <Style TargetType="Button">
        <Setter Property="BorderThickness" Value="5" />
        <Setter Property="Foreground" Value="Blue" />
    </Style>
</Page.Resources>

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

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

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

5 回答

  • 5

    从Xamarin.Forms 1.3您可以获得更多样式选项 .

    Xamarin.Forms 1.3 Technology Preview

    • 支持XAML和代码中的样式

    • 允许样式通过Style.BaseResourceKey基于DynamicResources

    • 通过Device.Styles添加平台特定样式(支持iOS动态类型)

  • 0

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

    这是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>
    

    支持代码如下:

    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);
            }
        }
    }
    

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

    我相信它会有所帮助 .

  • 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>
    
  • 0

    我能够创建更接近我在WPF / Silverlight中更习惯的ResourceDictionary的代码,并将其从实际页面中删除(视图)

    • 我没有使用App.cs,而是创建了一个新的xaml文件.. App.xaml及其随附的App.xaml.cs代码隐藏 . 我拿了App.cs中的内容并将其放入App.xaml.cs.
    public partial class App : Application
    {
        public App()
        {
           var _mainView = new MainView();
           var _navPage = new NavigationPage(_mainView);
           MainPage = _navPage;
        }
    
       //.. the methods below are currently empty on mine, 
       //still figuring out some of the front end ..
       protected override void OnStart(){ }
       protected override void OnSleep(){ }
       protected override void OnResume(){ }
    

    }

    • 这允许我在App.xaml中声明我可以在其他视图上使用的应用程序资源:
    <Style x:Key="buttonStyle" TargetType="{x:Type Button}">
            <Setter Property="TextColor" Value="Blue"/>
            <!-- note: I tried Foreground and got errors, but once I 
                 used TextColor as the property, it ran fine and the 
                 text of the button I had changed color to what I expected -->
            <Setter Property="BackgroundColor" Value="White"/>
            <!-- note: I tried Background and got errors, but once I 
                 used BackgroundColor as the property, it ran fine and the 
                 background of the button I had changed color -->
        </Style>
    
    </ResourceDictionary>
    

    然后我可以使用StaticResource在我的页面上使用该样式

    <Button Text="Inquiry" Style="{StaticResource buttonStyle}"/>
    

    可能有很多种方式,甚至可能更好 - 但这有助于我将所有风格保存在一个位置(App.xaml)

    附:我没有格式化代码隐藏的例子,我已经尝试了几次但希望你仍然能理解它 .

  • 0

    我正在考虑使用Xamarin Forms作为Silverlight开发人员 .

    一种可选的方法是在XAML或代码中创建自己的控件(Xamarin术语中的“Views”),并在构建界面时使用它们 .

相关问题