从 cmets 开始,我认为保留旧答案完全无关紧要。所以更新的答案如下:
控制
这只是控件的基本虚拟版本,您需要添加视觉状态和其他资源、资产和自定义样式,但骨架如下所示:
C#
public sealed class MyDummyControl : Control
{
#region fields
private const string primaryIconName = "PrimaryIcon";
#endregion fields
#region UIElements
private AppBarButton PrimaryIcon;
#endregion UIElements
#region Events
public event Action PrimaryButtonClicked;
#endregion Events
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
PrimaryIcon = this.GetTemplateChild(primaryIconName) as AppBarButton;
//in cases with c# versions lower than 6.0
//consider replacing the null conditional check(?) with the tradional
//if(BackRequested!=null) and
//the lambda's and annonymous methods with { } and methodName()
if (PrimaryIcon != null)
PrimaryIcon.Click += (s, args) =>
{
PrimaryButtonClicked?.Invoke();
};
}
public MyDummyControl()
{
this.DefaultStyleKey = typeof(MyDummyControl);
}
#region Dependancy Properties
public UIElement HeaderContent
{
get { return (UIElement)GetValue(HeaderContentProperty); }
set { SetValue(HeaderContentProperty, value); }
}
// Using a DependencyProperty as the backing store for HeaderContent. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeaderContentProperty =
DependencyProperty.Register("HeaderContent", typeof(UIElement), typeof(MyDummyControl), new PropertyMetadata(null));
public bool IsPrimaryIconCompact
{
get { return (bool)GetValue(IsPrimaryIconCompactProperty); }
set { SetValue(IsPrimaryIconCompactProperty, value); }
}
// Using a DependencyProperty as the backing store for IsPrimaryIconCompact. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsPrimaryIconCompactProperty =
DependencyProperty.Register("IsPrimaryIconCompact", typeof(bool), typeof(MyDummyControl), new PropertyMetadata(false));
public UIElement Content
{
get { return (UIElement)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
// Using a DependencyProperty as the backing store for Content. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register("Content", typeof(UIElement), typeof(MyDummyControl), new PropertyMetadata(null));
public SolidColorBrush HeaderBackground
{
get { return (SolidColorBrush)GetValue(HeaderBackgroundProperty); }
set { SetValue(HeaderBackgroundProperty, value); }
}
// Using a DependencyProperty as the backing store for HeaderBackground. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeaderBackgroundProperty =
DependencyProperty.Register("HeaderBackground", typeof(SolidColorBrush), typeof(MyDummyControl), new PropertyMetadata(new SolidColorBrush(Windows.UI.Colors.Gray)));
public SymbolIcon Icon
{
get { return (SymbolIcon)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}
// Using a DependencyProperty as the backing store for Icon. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register("Icon", typeof(SymbolIcon), typeof(MyDummyControl), new PropertyMetadata(new SymbolIcon(Symbol.Cancel)));
public string IconLabel
{
get { return (string)GetValue(IconLabelProperty); }
set { SetValue(IconLabelProperty, value); }
}
// Using a DependencyProperty as the backing store for IconLabel. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IconLabelProperty =
DependencyProperty.Register("IconLabel", typeof(string), typeof(MyDummyControl), new PropertyMetadata(string.Empty));
#endregion Dependancy Properties
}
创建控件后,现在您需要为其添加默认样式:
资源字典
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="using:ShellBackButtonDummy.Controls">
<Style TargetType="Controls:MyDummyControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Controls:MyDummyControl">
<Grid x:Name="layoutRoot" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Name="HeaderBanner" Background="{TemplateBinding HeaderBackground}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ContentPresenter Content="{TemplateBinding HeaderContent}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<AppBarButton x:Name="PrimaryIcon" Icon="{TemplateBinding Icon}" Label="{TemplateBinding IconLabel}" IsCompact="{TemplateBinding IsPrimaryIconCompact}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</Grid>
<ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
以上是控件正常运行所需的两个必要条件。
稍后你可以编辑资源字典来个性化控件,一旦你弄清楚了你可以冻结它,当你在其他应用程序中使用它时,你可以简单地覆盖默认样式而不是手动更改资源字典每一次。
我希望这会有所帮助。我已经在GitGub上上传了一个模板10版本的解决方案的副本