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

时间:2017-04-01 06:43:48

标签: c# 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
       });
   }
}

这可能吗?关于如何做到这一点的任何想法?

2 个答案:

答案 0 :(得分:2)

 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();
      }
   });

答案 1 :(得分:1)

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

//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 }"/>

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