我的应用程序使用许多这样的ContentPages:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
xmlns:template="clr-namespace:Japanese.Templates"
x:Class="Japanese.Views.HelpTab.SettingsPage"
Title="Settings Screen Help"
BackgroundColor="{DynamicResource PageBackgroundColor}">
<ContentPage.Content>
<ScrollView>
<StackLayout Spacing="0">
<Content goes here/>
</StackLayout>
</ScrollView>
</ContentPage.Content>
有没有一种方法可以创建一种新型的名为ScrollableContentPage
的页面,该页面已经具有<ScrollView>
和<StackLayout Spacing="0">
元素?
类似这样的东西:
<ScrollableContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
xmlns:template="clr-namespace:Japanese.Templates"
x:Class="Japanese.Views.HelpTab.SettingsPage"
Title="Settings Screen Help"
BackgroundColor="{DynamicResource PageBackgroundColor}">
<ContentPage.Content>
<Content goes here/>
</ContentPage.Content>
答案 0 :(得分:2)
我相信有几种可能性。我们正在使用绑定到内部内容的嵌套ContentView
。只需添加您要重用的页面的ContentView
XAML即可
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
xmlns:template="clr-namespace:Japanese.Templates"
x:Class="Japanese.Templates.ScrollableContentPage"
BackgroundColor="{DynamicResource PageBackgroundColor}"
x:Name="ContentPage">
<ContentPage.Content>
<ScrollView>
<ContentView Content="{Binding Source={x:Reference ContentPage}, Path=InnerContent}" />
</ScrollView>
</ContentPage.Content>
</ContentPage>
在后面的代码中,您必须定义一个可绑定的属性(实际上我不知道它是否具有是可绑定的属性,但是无论如何您都需要更改通知,因此,BindableProperty
应该可以)
public static readonly BindableProperty InnerContentProperty = BindableProperty.Create(nameof(InnerContent), typeof(View), typeof(ScrollableContentPage));
public View InnerContent
{
get => (View)this.GetValue(InnerContentProperty);
set => this.SetValue(InnerContentProperty, value);
}
现在,您可以像使用其他任何页面一样使用ScrollableContentPage
(嗯,您需要导入名称空间,但是...)
<template:ScrollableContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
xmlns:template="clr-namespace:Japanese.Templates"
x:Class="Japanese.Views.HelpTab.SettingsPage"
Title="Settings Screen Help">
<template:ScrollableContentPage.InnerContent>
<!-- You content goes here -->
</template:ScrollableContentPage.InnerContent>
</template:ScrollableContentPage>
您甚至可以通过将属性添加到ScrollableContentPage
来简化此操作。
[ContentProperty(nameof(InnerContent))]
public class ScrollableContentPage : ContentPage
{
// ...
}
这样,您可以省略明确指定InnerContent
并使用ScrollableContentPage
的方式,如下所示:
<template:ScrollableContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
xmlns:template="clr-namespace:Japanese.Templates"
x:Class="Japanese.Views.HelpTab.SettingsPage"
Title="Settings Screen Help">
<!-- You content goes here -->
</template:ScrollableContentPage>