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



我正在使用以下代码:

[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;
}
}

这个:

[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;
}
}

有没有什么方法可以把这些结合起来,并有一个参数:

"Scroll = true" or 
"Scroll = false" 

默认值为true?

这是类的使用方式:

public partial class DecksTabPage : ScrollHeadingView
{
public DecksTabViewModel vm;
public DecksTabPage()
{
BindingContext = vm = new DecksTabViewModel();
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
vm.OnAppearing();
}
}

<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" >

您可以创建一个可绑定属性。

类似的东西

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); }
}

然后在中,您可以在xaml:中设置IsScrollable

<t:ScrollHeadingView
...
IsScrollable = True
/>
public ScrollHeadingView(bool scroll = true)
{
if (scroll) {
...
} else { 
...
}
}

我们可以使用ScrollView的orientation属性使内容可滚动。因此,如果我们不想滚动,那么我们可以设置scroll=False,它将作为正常视图工作,当我们设置scroll=True时,它将用作滚动视图。

[ContentProperty(nameof(InnerContent))]
public partial class ScrollHeadingView : ContentPage
{
ScrollView scrollView;
public static readonly BindableProperty ScrollProperty = BindableProperty.Create(nameof(Scroll), typeof(bool), typeof(ScrollHeadingView), true,BindingMode.TwoWay, propertyChanged: (obj, oldValue,newValue)=> {
OnScrollPropertyChange((ScrollHeadingView)obj,(bool)newValue);
});
private static void OnScrollPropertyChange(ScrollHeadingView view,bool newValue)
{
if (newValue)
{
view.scrollView.Orientation = ScrollOrientation.Vertical;
view.scrollView.VerticalScrollBarVisibility = ScrollBarVisibility.Always;
}
else
{
view.scrollView.Orientation = ScrollOrientation.Neither;
view.scrollView.VerticalScrollBarVisibility = ScrollBarVisibility.Never;
}
}
public bool Scroll
{
get { return (bool)GetValue(ScrollProperty); }
set { SetValue(ScrollProperty, value); }
}
public ScrollHeadingView()
{
var outerGrid = new Grid();
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;
}
}

Xaml

<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" 
Scroll="False">

相关内容

  • 没有找到相关文章

最新更新