我可以为内容页面使用rightSwipeGestureRecognizer吗?

时间:2017-07-12 06:22:59

标签: xamarin xamarin.forms

我正在查看我的应用程序。目前它有这个:

C#代码

namespace Japanese
{
    public partial class PhrasesPage : ContentPage
    public PhrasesFrame phrasesFrame = new PhrasesFrame();

    public PhrasesPage()
    {
        InitializeComponent();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        phrasesStackLayout.Children.Add(phrasesFrame);

XAML

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
        xmlns:local="clr-namespace:Japanese;assembly=Japanese"
        x:Class="Japanese.PhrasesPage"
        x:Name="PhraseContentPage">
    <ContentPage.Content>
        <StackLayout x:Name="phrasesStackLayout">
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

然后在其中:

C#代码

namespace Japanese
{
    public partial class PhrasesFrame : Frame

XAML

<?xml version="1.0" encoding="UTF-8"?>
<Frame xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="Japanese.PhrasesFrame" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="Transparent" Padding="0" HasShadow="false">
    <StackLayout x:Name="phrasesFrameStackLayout" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">

实现rightSwipeGestureRecognizer的框架的自定义渲染器

[assembly: ExportRenderer(typeof(PhrasesFrame),         typeof(PhrasesFrameCustomRenderer))]
namespace Japanese.iOS
{
public class PhrasesFrameCustomRenderer : FrameRenderer
{
    UISwipeGestureRecognizer leftSwipeGestureRecognizer;
    UISwipeGestureRecognizer rightSwipeGestureRecognizer;
    PhrasesFrame frame;
    bool rightSwipeEnabled = false;

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

        frame = Element as PhrasesFrame;

        rightSwipeGestureRecognizer = new UISwipeGestureRecognizer();
        rightSwipeGestureRecognizer.Direction = UISwipeGestureRecognizerDirection.Right;
        rightSwipeGestureRecognizer.NumberOfTouchesRequired = 1;
        rightSwipeGestureRecognizer.AddTarget((obj) =>
        { });

我的问题是,我是否需要在内容页面中设置一个框架才能实现SwipeGestureRecognizer?

1 个答案:

答案 0 :(得分:1)

我认为您的代码也适用于任何给定的其他元素。由于您目前正在从CustomRenderer继承FrameRenderer,因此它只适用于Frame个对象,更具体地说是PhrasesFrame个对象(如ExportRenderer中所定义您可以轻松地将此更改为例如PageRenderer,例如:

[assembly: ExportRenderer(typeof(MyContentPage), typeof(ContentPageRenderer))]

namespace Japanese.iOS
{
    public class ContentPageRenderer : PageRenderer
    {

然后,您可以更改引用此渲染器中Frame的每段代码,以引用ContentPage,但它可能会起作用。这确实意味着每个 ContentPage对象都会获得此功能,因此您很可能必须创建自己继承自ContentPage的对象并将CustomRenderer应用于该对象

public class MyContentPage : ContentPage
{

}

并相应地更新您的XAML:

<?xml version="1.0" encoding="UTF-8"?>
<local:MyContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
        xmlns:local="clr-namespace:Japanese;assembly=Japanese"
        x:Class="Japanese.PhrasesPage"
        x:Name="PhraseContentPage">
    <ContentPage.Content>
        <StackLayout x:Name="phrasesStackLayout">

        </StackLayout>
    </ContentPage.Content>
</local:MyContentPage>