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 元素。有没有人成功地将布局与内容分开?

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

4

5 回答 5

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>

支持代码如下所示:

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

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

我相信它会有所帮助。

于 2014-06-26T12:40:30.230 回答
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>
于 2015-07-16T10:46:57.243 回答
1

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

Xamarin.Forms 1.3 技术预览

  • 支持 XAML 和代码中的样式
  • 通过 Style.BaseResourceKey 允许样式基于 DynamicResources
  • 通过 Device.Styles 添加平台特定样式(支持 iOS 动态类型)
于 2014-12-08T07:38:15.930 回答
0

作为 Silverlight 开发人员,我正在考虑使用 Xamarin Forms 解决这个问题。

一种可选方法是在 XAML 或代码中创建自己的控件(Xamarin 术语中的“视图”),并在构建界面时使用这些控件。

于 2014-10-02T08:12:22.123 回答
0

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

  1. 我不仅创建了 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(){ }    
    

    }

  2. 这允许我在 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)

PS我不知道为什么它没有格式化代码隐藏示例,我已经尝试了几次,但希望你仍然理解它。

于 2015-04-21T23:17:29.417 回答