我为可滚动页面和不可滚动页面创建了一个模板。有没有一种方法可以组合这些并使用参数来设置可滚动或不可滚动?

Ala*_*an2 -3 xamarin xamarin.forms

我有我正在使用的代码:

[ContentProperty(nameof(InnerContent))]
public partial class ScrollHeadingView : ContentPage
{

    public ScrollHeadingView()
    {
        var outerGrid = new Grid();

        var scrollView = new Xamarin.Forms.ScrollView() { 
           VerticalScrollBarVisibility = ScrollBarVisibility.Always 
        };
        var contentView = new ContentView()
        .Bind(ContentProperty, nameof(InnerContent), source: this);
        scrollView.Content = contentView;
        outerGrid.AddChild(scrollView, 1, 0);
        Content = outerGrid;
    }
}
Run Code Online (Sandbox Code Playgroud)

和这个:

[ContentProperty(nameof(InnerContent))]
public partial class HeadingView : ContentPage
{

    public ScrollHeadingView()
    {
        var outerGrid = new Grid();

        var contentView = new ContentView()
        .Bind(ContentProperty, nameof(InnerContent), source: this);
        scrollView.Content = contentView;
        outerGrid.AddChild(contentView, 1, 0);
        Content = outerGrid;
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以组合这些并有一个参数:

"Scroll = true" or 
"Scroll = false" 
Run Code Online (Sandbox Code Playgroud)

默认为真?

下面是类的使用方式:

public partial class DecksTabPage : ScrollHeadingView
{
    public DecksTabViewModel vm;
    public DecksTabPage()
    {
        BindingContext = vm = new DecksTabViewModel();
        InitializeComponent();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        vm.OnAppearing();
    }

}
Run Code Online (Sandbox Code Playgroud)

<t:ScrollHeadingView
    x:Class="Views.DecksTab.DecksTabPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:t="clr-namespace:Templates"
    x:Name="ThisPage"
    GearIconTapCommand="{Binding GearTapComd, Mode=OneWay}"
    GearIconVisible="true" >
Run Code Online (Sandbox Code Playgroud)

Leo*_*SFT 5

您可以创建一个Bindable Properties

就像是

public static readonly BindableProperty IsScrollableProperty = BindableProperty.Create (nameof(IsScrollable), typeof(bool), typeof(ScrollHeadingView), null);

public bool IsScrollable
{
    get { return (bool)GetValue(IsScrollableProperty); }
    set { SetValue(IsScrollableProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以IsScrollable在你的xaml中设置:

<t:ScrollHeadingView
   ...
   IsScrollable = True
/>
Run Code Online (Sandbox Code Playgroud)