Effect on all buttons in Xamarin Forms

别说谁变了你拦得住时间么 提交于 2021-01-28 09:11:47

问题


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>

But this throws a Can't resolve ControlTemplateProperty on Button.

Anybody has any idea on how to do this?


回答1:


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

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

  • ContentPage
  • ContentView
  • TemplatedPage
  • 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:

  • How to create an effect
  • How to consume it with styles
  • A good example

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



来源:https://stackoverflow.com/questions/48340655/effect-on-all-buttons-in-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!