2017-04-12 46 views
0

由於可用的分組和其他功能,我正在使用SfListView。 但是,我需要禁用滾動Android和iOS中的列表視圖。 如何爲SfListView編寫自定義渲染器? 對於Xamarin表單ListView,我只能擴展ListViewRenderer類並重寫OnElementChanged方法。如何爲SfListView編寫自定義渲染器來禁用滾動?

另外,如何導出自定義渲染器?

例如,下面的代碼爲Xamarin將工作窗體ListView(與相應的修改):

[assembly: ExportRenderer(typeof(SfListViewWithNoScroll), typeof(SfListViewWithNoScrollRenderer))] 
namespace MyApp.Mobile.Droid.CustomRenderers 
    { 
    public class SfListViewWithNoScrollRenderer : //which class do I need to inherit? 
    { 
     protected override void OnElementChanged(ElementChangedEventArgs<SfListView> e) 
     { 

      //base.OnElementChanged(e); 
      //if (Control != null) 
      //{ 
      // Control.VerticalScrollBarEnabled = false; 
      //} 
      //what do I write here? 
     } 
    } 
    } 

回答

1

這樣做會影響的ListView但像選擇相互作用的滾動將仍起作用的。您無法查看視口區域下方的項目。

[assembly: ExportRenderer(typeof(SfListView), typeof(CustomSfListViewRenderer))] 
    namespace XamarinSfListViewDemo.Droid 
    { 
     public class CustomSfListViewRenderer : ViewRenderer<SfListView,Android.Views.View> 
     { 
      private Xamarin.Forms.ScrollView scroller; 

      protected override void OnElementChanged(ElementChangedEventArgs<SfListView> e) 
      { 
       base.OnElementChanged(e); 
       var element = e.NewElement; 
       scroller = (Xamarin.Forms.ScrollView)typeof(SfListView).GetField("scrollView", BindingFlags.NonPublic | BindingFlags.Instance) 
        .GetValue(element); 
       scroller.InputTransparent = true; 
      } 
     } 
    } 

我還沒有嘗試過這段代碼在iOS中,但它應該工作90%。試一試,讓我知道它是否有幫助!

+0

在'scroller =(Xamarin.Forms.ScrollView)typeof(SfListView).GetField(「scrollView」,BindingFlags.NonPublic)行中給出'null reference exception' .GetValue(element);'。即使在將此行更改爲這裏提到的那個之後:https://www.syncfusion.com/forums/129969/how-can-i-write-a-custom-renderer-for-sflistview,我得到了相同的錯誤 –

+0

scroller =(Xamarin.Forms.ScrollView)typeof(SfListView).GetField(「scrol lView」,BindingFlags.NonPublic | BindingFlags.Instance).GetValue(element); – Dilmah

+0

你可以請看看這個問題:http://stackoverflow.com/questions/43679889/multiple-behaviors-in-entrybindable-property-change-is-not-reflected –

0

在SfListView中,我們爲您的要求提供屬性「IsScrollBarVisible」「在ListView中啓用/禁用ScrollBar可見性」。無需爲此創建渲染器。默認情況下,IsScrollBarVisible屬性值爲true。您可以啓用/禁用特定平臺的滾動條可見性,如下面的代碼示例所示。

<sync:SfListView x:Name="listView" ItemsSource="{Binding BookInfo}"> 
    <sync:SfListView.IsScrollBarVisible> 
    <OnPlatform x:TypeArguments="x:Boolean"> 
     <OnPlatform.Android> 
     <OnIdiom x:TypeArguments="x:Boolean" Phone="false" Tablet="false"/> 
     </OnPlatform.Android> 
     <OnPlatform.iOS> 
     <OnIdiom x:TypeArguments="x:Boolean" Phone="false" Tablet="false"/> 
     </OnPlatform.iOS> 
     <OnPlatform.WinPhone> 
     <OnIdiom x:TypeArguments="x:Boolean" Phone="true" Tablet="true"/> 
     </OnPlatform.WinPhone> 
    </OnPlatform> 
    </sync:SfListView.IsScrollBarVisible> 
</sync:SfListView> 

但它有一些限制。由於Xamarin Forms中原生ScrollView渲染器的一些限制,您無法在運行時更改IsScrollBarVisible屬性。它只能在初始化SfListView時定義。