简体   繁体   English

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

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

I have the following code: 我有以下代码:

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

In my custom renderer I want to be able to call the getRandomWords on swipe left gesture like below: 在我的自定义渲染器中,我希望能够在滑动左手势上调用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
       });
   }
}

Is this possible? 这可能吗? Any ideas on how this could be done? 关于如何做到这一点的任何想法?

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

You can create a BindableProperty of type Command in your custom frame class, call that Command from your renderer and bind your ViewModel's getRandomWords method as a Command 您可以在自定义框架类中创建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 }"/>

It may seem more complicated this way, but using commands allows you to separate your application code (Such as getting random phrases) from your rendering code 这种方式看起来可能更复杂,但使用命令可以将应用程序代码(例如获取随机短语)与渲染代码分开

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

相关问题 无法从Custom Renderer Xamarin表单加载视图 - Can not load view from Custom Renderer Xamarin Forms Xamarin Forms 自定义控件:在渲染器中实现一个方法 - Xamarin Forms custom control: implement a method in the renderer 如何从自定义渲染器设置 Xamarin.Forms 元素 BindableProperties? - How to set Xamarin.Forms Elements BindableProperties from a Custom Renderer? 如何从Xamarin Forms iOS中的自定义渲染器滑块中删除拇指? - How to remove thumb from custom renderer slider in Xamarin forms iOS? 如何在Xamarin表单中从自定义渲染器导航到内容页面 - How to navigate from custom renderer to content page in xamarin forms 自定义渲染器不渲染Xamarin表单 - Custom Renderer not Rendering Xamarin Forms 从Xamarin Forms元素共享代码访问自定义渲染器实例 - Accessing a Custom Renderer Instance from Xamarin Forms Element Shared Code 如何从 Xamarin Forms 中的另一个页面的 XAML 访问自定义视图的字段或控件? - How to access fields or controls of Custom View from another Page's XAML in Xamarin Forms? 如何获得对从Xamarin.Forms中的自定义渲染器渲染的控件的引用? - How to get a reference to the control being rendered from a custom renderer in Xamarin.Forms? 如何在Xamarin.Forms中为ToolbarItem创建自定义渲染器? - How can I make a custom renderer for ToolbarItem in Xamarin.Forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM