• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - Xamarin 从渲染器按钮更改页面

[复制链接]
菜鸟教程小白 发表于 2022-12-12 12:49:10 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在 iOS 中有一个渲染器按钮,我想做的是在获得滑动手势时触发另一个页面进入堆栈。

如果我在我的 MainPage 上实现它,对于 Clicked 来说,它是直接安静的。因为我可以使用“这个”

    public class MainPage : ContentPage
    {
        public MainPage ()
        {
            // Button bottom 1
            var button = new Button { 
                Text = "button",
                HeightRequest = 60,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.CenterAndExpand,

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

但我怎么能在 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.
        }
    }

这可能吗? 感谢您的宝贵时间。



Best Answer-推荐答案


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

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 类中订阅您的自定义 OnSwipeUp 事件,就像使用 Clicked 一样。

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

关于ios - Xamarin 从渲染器按钮更改页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26995629/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap