就你而言,我个人会选择a templated view。
我将与您分享一个示例,因为该示例在发布到 nuget 时没有得到很好的记录,而且您可以找到的大多数文档都在博客上。
PCL:
public class ApplicationHeader : TemplatedView
{
protected override void OnChildAdded(Element child)
{
base.OnChildAdded(child);
SetInheritedBindingContext(child, BindingContext);
}
#region HomeButtonStyle
public static BindableProperty HomeButtonStyleProperty = BindableProperty.Create<ApplicationHeader, Style>(d => d.HomeButtonStyle, default(Style));
public Style HomeButtonStyle
{
get { return (Style) GetValue(HomeButtonStyleProperty); }
set { SetValue(HomeButtonStyleProperty, value); }
}
#endregion HomeButtonStyle
#region HomeButtonCommand
public static BindableProperty HomeButtonCommandProperty = BindableProperty.Create<ApplicationHeader, ICommand>(d => d.HomeButtonCommand, default(ICommand), defaultBindingMode: BindingMode.OneWay);
public ICommand HomeButtonCommand
{
get { return (ICommand) GetValue(HomeButtonCommandProperty); }
set { SetValue(HomeButtonCommandProperty, value); }
}
#endregion HomeButtonCommand
#region CommandAreaContent
public static BindableProperty CommandAreaContentProperty = BindableProperty.Create<ApplicationHeader, View>(d => d.CommandAreaContent, default(View), defaultBindingMode: BindingMode.OneWay);
public View CommandAreaContent
{
get { return (View) GetValue(CommandAreaContentProperty); }
set { SetValue(CommandAreaContentProperty, value); }
}
#endregion CommandAreaContent
#region HeaderTextLine1
public static BindableProperty HeaderTextLine1Property = BindableProperty.Create<ApplicationHeader, string>(d => d.HeaderTextLine1, default(string), defaultBindingMode: BindingMode.OneWay);
public string HeaderTextLine1
{
get { return (string) GetValue(HeaderTextLine1Property); }
set { SetValue(HeaderTextLine1Property, value); }
}
#endregion HeaderTextLine1
#region HeaderTextLine2
public static BindableProperty HeaderTextLine2Property = BindableProperty.Create<ApplicationHeader, string>(d => d.HeaderTextLine2, default(string), defaultBindingMode: BindingMode.OneWay);
public string HeaderTextLine2
{
get { return (string) GetValue(HeaderTextLine2Property); }
set { SetValue(HeaderTextLine2Property, value); }
}
#endregion HeaderTextLine2
#region HeaderTapCommand
public static BindableProperty HeaderTapCommandProperty = BindableProperty.Create<ApplicationHeader, ICommand>(d => d.HeaderTapCommand, default(ICommand), defaultBindingMode: BindingMode.OneWay);
public ICommand HeaderTapCommand
{
get { return (ICommand) GetValue(HeaderTapCommandProperty); }
set { SetValue(HeaderTapCommandProperty, value); }
}
#endregion HeaderTapCommand
}
App.xaml
请记住,当前的资源声明必须是为了避免 app.xaml 异常。
<controls:EnhancedButton Padding="24,0,24,0" IsVisible="{TemplateBinding HomeButtonCommand, Converter={StaticResource NullToBoolConverter}, ConverterParameter={x:Static x:Boolean.TrueString}}" Style="{StaticResource ButtonThemedDark}" Grid.Column="0" Text="=" Command="{TemplateBinding HomeButtonCommand}"></controls:EnhancedButton>
<Grid Grid.Column="1" extensions:GestureExtensions.TapCommand="{TemplateBinding HeaderTapCommand}" BackgroundColor="{StaticResource ColorThemeAccent}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Grid.Row="1" Text="{TemplateBinding HeaderTextLine1}" Style="{StaticResource LabelShellHeaderLine1}" IsVisible="{TemplateBinding HeaderTextLine1, Converter={StaticResource NullToBoolConverter}, ConverterParameter={x:Static x:Boolean.TrueString}}"></Label>
<Label Grid.Row="2" Text="{TemplateBinding HeaderTextLine2}" Style="{StaticResource LabelShellHeaderLine2}" IsVisible="{TemplateBinding HeaderTextLine2, Converter={StaticResource NullToBoolConverter}, ConverterParameter={x:Static x:Boolean.TrueString}}"></Label>
</Grid>
<controls:EnhancedContentPresenter Grid.Column="2" Content="{TemplateBinding CommandAreaContent}"></controls:EnhancedContentPresenter>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>