对 Xamarin Forms 中所有按钮的影响 [英] Effect on all buttons in Xamarin Forms

查看:20
本文介绍了对 Xamarin Forms 中所有按钮的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对所有按钮产生影响,但似乎无法正确配置.

I'm trying to have a effect on all my buttons, but I can't seem to get the configuration right.

这就是我现在所拥有的:

this is what I have now:

<ResourceDictionary>
    <ControlTemplate x:Key="ButtonTemplate" >
        <Grid RowSpacing="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
            <ContentPresenter Grid.Row="0" Grid.Column="0" >
                <ContentPresenter.Effects>
                    <effects:ShadowEffect Radius="5" DistanceX="5" DistanceY="5">
                        <effects:ShadowEffect.Color>
                            <OnPlatform x:TypeArguments="Color">
                                <On Platform="iOS" Value="Black" />
                                <On Platform="Android" Value="White" />
                                <On Platform="UWP" Value="Red" />
                            </OnPlatform>
                        </effects:ShadowEffect.Color>
                    </effects:ShadowEffect>
                </ContentPresenter.Effects>
            </ContentPresenter>

        </Grid>
    </ControlTemplate>

    <Style TargetType="{x:Type Button}">
        <Setter Property="ControlTemplate" Value="{StaticResource ButtonTemplate}"></Setter>
    </Style>
</ResourceDictionary>

但是这会抛出一个无法解析按钮上的 ControlTemplateProperty.

有人知道如何做到这一点吗?

Anybody has any idea on how to do this?

推荐答案

正如您在 Xamarin.Forms 指南 控件模板不适用于按钮.

As you can see at Xamarin.Forms Guides the Control Templates isn't applicable to Button.

A ControlTemplate 可以通过设置应用于以下类型它们的 ControlTemplate 属性:

A ControlTemplate can be applied to the following types by setting their ControlTemplate properties:

  • 内容页面
  • 内容视图
  • 模板化页面
  • 模板化视图

错误无法解析按钮上的 ControlTemplateProperty 是有道理的.Button 继承自 View,而不是需要的 TemplatedView.

The error Can't resolve ControlTemplateProperty on Button make sense. Button inherites from View, not TemplatedView as would be needed.

关于您的主要观点,效果不能直接被样式消耗,但是您可以通过附加属性来实现这一点.以下是一些很好的参考资料:

So about your main point, effects cann't consumed directly by style, but you can achieve this thru attached properties. Here is some good references to you:

希望能帮到你(抱歉我的英语不好)

I hope it help you (and sorry for my bad english)

这篇关于对 Xamarin Forms 中所有按钮的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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