【问题标题】:XAML shared resourcesXAML 共享资源
【发布时间】:2015-10-27 03:36:16
【问题描述】:

我正在尝试实现与 CSS 样式等效的 XAML。我想为 ContentPage 创建一个自定义布局,我可以在我的应用程序的所有页面中使用它,并且每个平台都有不同的值。

具体来说,我从自定义填充开始:我正在尝试将此代码放在我的 App.xaml 文件中:

<Application.Resources>
    <ResourceDictionary>
        <OnPlatform x:Key="MyPadding"
             x:TypeArguments="Thickness"
            iOS="0, 20, 0, 0"
            Android="0, 0, 0, 0"/>

        <Style
            x:Key="labelGreen"
            TargetType="Entry">

            <Setter
                Property="TextColor" 
                Value="Green"/>
        </Style>

    </ResourceDictionary>
</Application.Resources>

在单独的 ContentPage 中,我正在执行以下操作,但它不起作用:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
        x:Class="MyApp.LoginScreen" 
        Style="{DynamicResource MyPadding}"
>

自定义条目样式工作正常。但填充没有。我收到错误消息:“SetValue: Can not convert Xamarin.Forms.OnPlatform`1[Xamarin.Forms.Thickness] to type 'Xamarin.Forms.Style'”

我做错了什么?

【问题讨论】:

    标签: c# css xaml cross-platform xamarin.forms


    【解决方案1】:

    正如错误所说,Thickness 不是Style。改成:

    <Application.Resources>
        <ResourceDictionary>
            <OnPlatform x:Key="MyPadding"
                 x:TypeArguments="Thickness"
                iOS="0, 20, 0, 0"
                Android="0, 0, 0, 0"/>
    
            <Style
                x:Key="pageStyle"
                TargetType="ContentPage">
    
                <Setter
                    Property="Padding" 
                    Value="{StaticResource MyPadding}"/>
            </Style>
    
            <Style
                x:Key="labelGreen"
                TargetType="Entry">
    
                <Setter
                    Property="TextColor" 
                    Value="Green"/>
            </Style>
    
        </ResourceDictionary>
    </Application.Resources>
    
    
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
            x:Class="MyApp.LoginScreen" 
            Style="{StaticResource pageStyle}">
    

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
            x:Class="MyApp.LoginScreen" 
            Padding="{StaticResource MyPadding}">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2022-12-12
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      • 2020-05-17
      相关资源
      最近更新 更多