Xamarin - 页面渲染器(在特定页面上应用)

时间:2018-03-07 08:11:00

标签: xamarin xamarin.forms xamarin.ios

我有一个关于为内容页面创建自己的页面渲染器的问题。它适用于我的所有页面会发生什么,即使我在特定页面上专门实现它。

到目前为止,我已尝试过两种方法:

CustomRenderer(Shared)

public class CustomPage : ContentPage {}

CustomRenderer (iOS)

[assembly: ExportRenderer(typeof(CustomPage), typeof(CustomPageRenderer))]
namespace testApp.iOS.CustomRenderers.CustomPage
{
public class CustomPageRenderer: PageRenderer {}
}

第一种方法:通过XAML

<customPage:CustomPage
x:Class="testApp.Views.testPage" 
xmlns:customPage="clr-namespace:OSMSeafarerApp.CustomControls.CustomPage">
</customPage:CustomPage>

第二种方法:通过直接内容页面名称(页面名称 - testPage):我将程序集CustomPage更改为testPage,结果将是:|

[assembly: ExportRenderer(typeof(testPage), typeof(CustomPageRenderer))]
namespace testApp.iOS.CustomRenderers.CustomPage
{
    public class CustomPageRenderer: PageRenderer {}
}

我做错了什么或创建页面渲染器会影响所有页面吗?

EDIT1示例:

[assembly: ExportRenderer(typeof(testPage), typeof(CustomPageRenderer))]
namespace testApp.iOS.CustomRenderers.CustomPage
{
    public class CustomPageRenderer: PageRenderer 
    {
         NSObject _observerKeyboardShow;
         double _overlappedView;

         public override void ViewDidAppear(bool animated)
         {
             base.ViewDidAppear(animated);

             var page = Element as testPage;

             if (page != null)
             {
                 var contentAsScrollView = page.Content as ScrollView;
                 if(contentAsScrollView != null)
                 {
                     return;
                 }

                 RegisterKeyboardShowNotifications();
             }

         }

         private void RegisterKeyboardShowNotifications()
         {
             if(_observerKeyboardShow == null)
             {
                 _observerKeyboardShow = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, whenKeyboardShow);
             }
         }

         private void whenKeyboardShow(NSNotification obj)
         {
             //ExampleOnly;

             var firstResponder = View.FindFirstResponder();
             var keyboardArea = UIKeyboard.FrameEndFromNotification(obj);
             _overlappedView = firstResponder.GetViewRelativeBottom(View);
             SlidePageUp(keyboardArea.Height, _overlappedView);
         }

         private void SlidePageUp(nfloat height, double overlappedView)
         {
             var pageSize = Element.Bounds;
             var _newY = pageSize.Y - CalculateNewSize(pageSize.Height, height, overlappedView);
         }


       }
}

0 个答案:

没有答案