如何从 Xamarin Forms 自定义渲染器中的视图访问方法?

How to access method from view inside a Xamarin Forms custom renderer?

我有以下代码:

public partial class PhrasesFrameRendererClass : Frame
{
    .....
    void getRandomWords() {
       // more code here that involves getting random numbers 
       // and updating a grid's bindingcontext
    }
}

在我的自定义渲染器中,我希望能够在向左滑动手势时调用 getRandomWords,如下所示:

public class PhraseFrameCustomRenderer : FrameRenderer
{
   UISwipeGestureRecognizer leftSwipeGestureRecognizer;
   protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
   {
       base.OnElementChanged(e);
       leftSwipeGestureRecognizer = new UISwipeGestureRecognizer();
       leftSwipeGestureRecognizer.Direction = UISwipeGestureRecognizerDirection.Left;
       leftSwipeGestureRecognizer.NumberOfTouchesRequired = 1;
       leftSwipeGestureRecognizer.AddTarget((obj) =>
       {
          // Call getRandomWords() here
       });
   }
}

这可能吗?关于如何做到这一点有什么想法吗?

 base.OnElementChanged(e);
   leftSwipeGestureRecognizer = new UISwipeGestureRecognizer();
   leftSwipeGestureRecognizer.Direction = UISwipeGestureRecognizerDirection.Left;
   leftSwipeGestureRecognizer.NumberOfTouchesRequired = 1;
   leftSwipeGestureRecognizer.AddTarget((obj) =>
   {
      // Call getRandomWords() here
      var frame = Element as PhrasesFrameRendererClass ;
      if(frame!=null){
           frame.getRandomWords();
      }
   });

您可以在自定义框架中创建命令类型的 BindableProperty class,从渲染器调用该命令并将 ViewModel 的 getRandomWords 方法绑定为命令

//Your custom control in your PCL project
public partial class PhrasesFrameRendererClass : Frame
{
    public static readonly BindableProperty SwipeLeftCommandProperty = 
    BindableProperty.Create(nameof(SwipeLeftCommand), typeof(ICommand), typeof(PhrasesFrameRendererClass ), null);

    public ICommand SwipeLeftCommand
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }
}

//Your custom control renderer
public class PhraseFrameCustomRenderer : FrameRenderer
{
   UISwipeGestureRecognizer leftSwipeGestureRecognizer;
   protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
   {
       base.OnElementChanged(e);
       leftSwipeGestureRecognizer = new UISwipeGestureRecognizer();
       leftSwipeGestureRecognizer.Direction = UISwipeGestureRecognizerDirection.Left;
       leftSwipeGestureRecognizer.NumberOfTouchesRequired = 1;
       leftSwipeGestureRecognizer.AddTarget((obj) =>
       {
           var myFrame = Element as PhrasesFrameRendererClassl
           if(myFrame != null){
               if(myFrame.SwipeLeftCommand != null && myFrame.SwipeLeftCommand.CanExecute()){
                   myFrame.SwipeLeftCommand.Execute();
               }
           }
       });
   }
}

//Your ViewModel
public class PhrasesViewModel{

    public Command GetRandomWordsCommand {get;set;}

    public PhrasesViewModel(){
        GetRandomWordsCommand = new Command(ExecuteGetRandomWords);
    }

    private void ExecuteGetRandomWords(){

    //Your method goes here

    }

}

//Your XAML
<yourControls:PhrasesFrameRendererClass SwipeLeftCommand="{Binding GetRandomWordsCommand }"/>

这种方式可能看起来更复杂,但使用命令可以让您将应用程序代码(例如获取随机短语)与渲染代码分开