简体   繁体   English

Xamarin.Forms,创建可点击的框架(背景)

[英]Xamarin.Forms, Create clickable Frame (background)

As Button is limited to use images inside it in Xamarin.Forms And StackLayout does not support CornerRadius property, We decided to use Frame instead of them, the Tapped event is implemented but the problem is background does not change on clicking. 由于Button只能在Xamarin.Forms中使用图像,并且StackLayout不支持CornerRadius属性,我们决定使用Frame代替它们,实现了Tapped事件,但问题是单击背景不会改变。 So how can I create and set a clickable background to frame? 那么如何创建可点击背景并设置框架呢?

AFAIK you can't handle visual state in xamarin forms. 抱歉,您无法处理Xamarin形式的视觉状态。 But you can do something like below code to achieve the state transition using custom renderer. 但是您可以执行以下代码来使用自定义渲染器实现状态转换。

public class CustomFrameRenderer : Xamarin.Forms.Platform.Android.AppCompat.FrameRenderer
{
    private Color backgroundColor;

    protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
    {
        base.OnElementChanged(e);
        backgroundColor = Element.BackgroundColor;
        Control.Touch += Control_Touch;
    }

    private void Control_Touch(object sender, TouchEventArgs e)
    {
        switch (e.Event.Action)
        {
            case MotionEventActions.Down:
                Element.BackgroundColor = Color.Green;
                break;
            case MotionEventActions.Up:
                Element.BackgroundColor = backgroundColor;
                break;
        }
    }
}

Note: Above code would affect all the frames so better subclass and create a new frame to avoid background color transition in the entire app. 注意:上面的代码会影响所有框架,因此会更好地继承子类,并创建一个新框架以避免整个应用程序中的背景色过渡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM