ios - Xamarin 从渲染器按钮更改页面
<p><p>我在 iOS 中有一个渲染器按钮,我想做的是在获得滑动手势时触发另一个页面进入堆栈。</p>
<p>如果我在我的 MainPage 上实现它,对于 Clicked 来说,它是直接安静的。因为我可以使用“这个”</p>
<pre><code> 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());
};
}
}
</code></pre>
<p>但我怎么能在 iOS 的渲染按钮中做到这一点。</p>
<p>我的渲染器按钮是这样的:</p>
<pre><code>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.
}
}
</code></pre>
<p>这可能吗?
感谢您的宝贵时间。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>一个很好的方法是将您的自定义渲染器与自定义按钮 View 相结合。您的自定义 View 可能有一个您可以订阅的滑动事件。当然,如果需要,您也可以创建自定义委托(delegate)来传递自定义事件数据,但我让这个示例保持简单。</p>
<pre><code>public class CustomButton : Button
{
public event EventHandler OnSwipeUp;
public void FireSwipeUp()
{
var swipeUp = OnSwipeUp;
if (swipeUp != null)
swipeUp(this, EventArgs.Empty);
}
}
</code></pre>
<p>从您的自定义渲染器中,您可以通过调用 <code>FireSwipeUp</code> 方法来触发自定义事件。</p>
<pre><code>private void onSwipeUp()
{
((CustomButton)Element).FireSwipeUp();
}
</code></pre>
<p>现在您可以在 <code>MainPage</code> 类中订阅您的自定义 <code>OnSwipeUp</code> 事件,就像使用 <code>Clicked</code> 一样。</p>
<pre><code>// 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());
};
</code></pre></p>
<p style="font-size: 20px;">关于ios - Xamarin 从渲染器按钮更改页面,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/26995629/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/26995629/
</a>
</p>
页:
[1]