FrameRenderer在Android上避免使用Frame.GestureRecognizers命令

时间:2019-05-30 02:57:19

标签: xamarin.forms

我用文字和图像创建了自己的按钮。为此,我使用了一个框架,其中封装了所有内容。在Android中,应按下按钮。为此,我使用了FrameRenderer,在Android的情况下,我拥有Touch事件,并且效果很好。

问题是我还想使用Frame.GestureRecognizers在按下命令时发送命令,但这显然取消了事件的调用,因为未执行命令。应当指出,我对UWP所做的完全相同,并且不会发生此问题。我想我在Android上吃东西。我将与您共享代码,看看您是否可以帮助我。谢谢。

Xaml:

            <local:MyFrameButton  
                x:Name="ButtonFrame"
                Grid.Column="0"
                Grid.Row="6"
                Grid.ColumnSpan="2"
                Grid.RowSpan="4"
                CornerRadius="5" 
                BackgroundColor="{StaticResource ButtonColor}"
                HorizontalOptions="CenterAndExpand" 
                VerticalOptions="CenterAndExpand"  
                HeightRequest="172"
                WidthRequest="172"
                Padding="0" >
                <Grid Padding="8">
                    <Label                              
                          Text="{Binding IconCustomer}" 
                          FontFamily="{DynamicResource MaterialFontFamilyID}"                                  
                          FontSize="{OnPlatform Android='45', iOS='45', UWP='55'}"
                          TextColor="White"   
                          HorizontalOptions="CenterAndExpand"
                          VerticalOptions="CenterAndExpand" >
                    </Label>
                    <Label 
                          Grid.Row="1"
                          Text="Clientes" 
                          FontSize="17"  
                          TextColor="White"  
                          HorizontalOptions="Center"
                          VerticalOptions="Center" />
                </Grid>
                <Frame.GestureRecognizers>
                    <TapGestureRecognizer 
                            Command="{Binding CustomerListCommand}"
                            />
                </Frame.GestureRecognizers>
            </local:MyFrameButton>

ViewModel:

  public ICommand CustomerListCommand => new RelayCommand(GoCustomerList);

    private async void GoCustomerList()
    {
        isRunning = true;
        MainViewModel.Instance.CustomersV = new CustomersViewModel(CustomerList);
        await MainViewModel.Instance.CustomersV.LoadConditionInitialsAsync();
        await navigationService.Navigate("CustomersView");
        isRunning = false;
    }

FrameRenderer:


 class MyFrameButtonRenderer : FrameRenderer
    {

        public MyFrameButtonRenderer(Context context) : base(context)
        {

        }

        protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                var drawable = new GradientDrawable();
                ViewGroup.SetBackground(drawable);
                UpdateBackgroundColor(drawable);
                UpdateCornerRadius(drawable);
                UpdateOutlineColor(drawable);
                UpdateShadow();

                this.Touch += (v, me) => 
                {
                    switch (me.Event.Action & MotionEventActions.Mask)
                    {
                        case MotionEventActions.Down:
                            Element.Scale = 1.05;
                            break;
                        case MotionEventActions.Up:
                            Element.Scale = 1;
                            break;
                        case MotionEventActions.PointerDown:
                            break;
                        case MotionEventActions.PointerUp:
                            break;
                        case MotionEventActions.Move:
                            break;
                    }
                };
            }
        }
}

1 个答案:

答案 0 :(得分:0)

解决方案是通过公共重写布尔OnTouchEvent(MotionEvent e)在FrameRender Touch事件中进行更改,如下所示:

public override bool OnTouchEvent(MotionEvent e)
        {
            switch (e.Action & MotionEventActions.Mask)
            {
                case MotionEventActions.Down:
                    Element.Scale = 1.05;
                    break;
                case MotionEventActions.Up:
                    Element.Scale = 1;
                    break;
                case MotionEventActions.PointerDown:
                    break;
                case MotionEventActions.PointerUp:
                    break;
                case MotionEventActions.Move:
                    break;
            }
            return base.OnTouchEvent(e);   
        }

这个想法是FaizalSaidali先生在Xamarin论坛上给我的:

https://forums.xamarin.com/discussion/comment/376599#Comment_376599