Ios Xamarin从渲染器按钮更改页面

Ios Xamarin从渲染器按钮更改页面,ios,mono,xamarin.ios,xamarin,xamarin-studio,Ios,Mono,Xamarin.ios,Xamarin,Xamarin Studio,我在iOS中有一个渲染器按钮,我试图做的是在它得到滑动手势时触发堆栈中的另一个页面 如果我在我的主页上实现了这一点,那么对于被点击的用户来说,它会很安静。因为我可以用“这个” 但我如何在iOS中的渲染按钮中做到这一点呢 我的渲染器按钮如下所示: public class MYButtonRenderer : ButtonRenderer { UISwipeGestureRecognizer swipeGestureRecognizerUp; protec

我在iOS中有一个渲染器按钮,我试图做的是在它得到滑动手势时触发堆栈中的另一个页面

如果我在我的主页上实现了这一点,那么对于被点击的用户来说,它会很安静。因为我可以用“这个”

但我如何在iOS中的渲染按钮中做到这一点呢

我的渲染器按钮如下所示:

public class MYButtonRenderer : ButtonRenderer
    {
        UISwipeGestureRecognizer swipeGestureRecognizerUp;

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

            swipeGestureRecognizerUp = new UISwipeGestureRecognizer (() => onSwipeUp());
            swipeGestureRecognizerUp.Direction = UISwipeGestureRecognizerDirection.Up;

            if (e.NewElement == null) 
            {

                if (swipeGestureRecognizerUp != null) 
                {
                    this.RemoveGestureRecognizer (swipeGestureRecognizerUp);
                }
            }

            if (e.OldElement == null) 
            {
                this.AddGestureRecognizer (swipeGestureRecognizerUp);
            }
        }

        private void onSwipeUp()
        {
           //here it's where I would like to change the page to a new one.
        }
    }
公共类MYButtonRenderer:ButtonRenderer
{
UISweepGestureRecognizer swipeGestureRecognizerUp;
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
碱基。一个元素改变(e);
SwipegestureRecognitizerUp=新的UISwipegestureRecognitizer(()=>onSwipeUp());
SwipegestureRecognitzerUp.Direction=UISwipegestureRecognitzerDirection.Up;
if(e.NewElement==null)
{
如果(SwipegestureRecognitizerUp!=null)
{
此.removeSetureRecognizer(swipeGestureRecognizerUp);
}
}
if(e.OldElement==null)
{
此.addgestureRecognitizer(SwipegestureRecognitizerUp);
}
}
私有void-onswipup()
{
//在这里,我想换一个新的页面。
}
}
这可能吗??
谢谢您的时间。

一个很好的方法是将自定义渲染器与自定义按钮视图相结合。您的自定义视图可以有一个您可以订阅的滑动事件。当然,如果需要,也可以创建自定义委托来传递自定义事件数据,但我保持了这个示例的简单性

public class CustomButton : Button
{
    public event EventHandler OnSwipeUp;

    public void FireSwipeUp()
    {
        var swipeUp = OnSwipeUp;
        if (swipeUp != null)
            swipeUp(this, EventArgs.Empty);
    }
}
在自定义渲染器中,可以通过调用
FireSwipeUp
方法触发自定义事件

private void onSwipeUp()
{
    ((CustomButton)Element).FireSwipeUp();
}
现在,您可以从
MainPage
类中订阅自定义的
onswipup
事件,就像单击
一样

// Button bottom 1
var button = new CustomButton { 
    Text = "button",
    HeightRequest = 60,
    HorizontalOptions = LayoutOptions.FillAndExpand,
    VerticalOptions = LayoutOptions.CenterAndExpand,
};

button.Clicked += async (sender, e) => {
     await this.Navigation.PushModalAsync(new nextPage());
};

button.OnSwipeUp += async (sender, e) => {
     await this.Navigation.PushModalAsync(new nextPage());
};

谢谢你的回答。但是我不明白,如果我的项目中有我的CustomButton(button视图)和buttonRenderer,我怎么能在我的共享项目中有CustomButton类型的按钮。我错过什么了吗?我知道你想做什么。我不在我的mac电脑上,所以我无法测试它,但我已经编辑了我的答案。该按钮现在继承自button,即共享的Xamarin.Forms按钮类型。您可以将其放在共享项目中。我已经更改了iOS渲染器中的代码,以使用渲染器的Model属性访问共享的Xamarin.Forms控件。让我知道它是否有效。:)现在它在“((CustomButton)Model.FireSwipeUp();”中抱怨说“Model”在这个上下文中不存在。我知道它是以另一种丑陋的方式工作的。但是我想按照你建议的方式来做,它比我的要聪明得多。我刚刚更新到Xamarin.Forms的最新版本,现在我得到了同样的错误。当Xamarin正在完成表单时,API有点流动。它现在可以称为元素而不是模型。
// Button bottom 1
var button = new CustomButton { 
    Text = "button",
    HeightRequest = 60,
    HorizontalOptions = LayoutOptions.FillAndExpand,
    VerticalOptions = LayoutOptions.CenterAndExpand,
};

button.Clicked += async (sender, e) => {
     await this.Navigation.PushModalAsync(new nextPage());
};

button.OnSwipeUp += async (sender, e) => {
     await this.Navigation.PushModalAsync(new nextPage());
};