简体   繁体   English

如何在Template10中将UWP ShellBackButton显示为按钮?

[英]How to show the UWP ShellBackButton as a button in Template10?

I need to show the UWP ShellBackButton as a button in my user control in Template10. 我需要在Template10的用户控件中将UWP ShellBackButton显示为按钮。

The ShellBackButton is the back button on top left in the application but I need to show this as a button in the main screen so the user can click on it. ShellBackButton是应用程序左上方的后退按钮,但是我需要在主屏幕上将其显示为按钮,以便用户可以单击它。

I have researched this, but could not find how to do this. 我已经对此进行了研究,但是找不到如何执行此操作。

There a property in App.xaml.cs to show the button on top left, that is ShowShellBackButton and I want to have this as a button in my user control view. App.xaml.cs有一个属性可在左上角显示按钮,即ShowShellBackButton ,我想在用户控件视图中将其作为按钮。

Template10 provides a navigation service through the Bootstrapper (and surfaces it through the ViewModelBase.NavigationService property) that you can use to handle backwards navigation in your button: Template10通过Bootstrapper提供导航服务(并通过ViewModelBase.NavigationService属性将其浮出水面),您可以使用该服务来处理按钮中的向后导航:

if ( NavigationService.CanGoBack ) NavigationService.GoBack();

See https://github.com/Windows-XAML/Template10/wiki/Bootstrapper#navigation-service for more details about INavigationService and the Bootstrapper. 有关INavigationService和Bootstrapper的更多详细信息,请参见https://github.com/Windows-XAML/Template10/wiki/Bootstrapper#navigation-service

As from the comments, i thought it's completely irrelevant to keep the old answer. 根据评论,我认为保留旧答案完全不相关。 So The updated answer is below: 因此,更新后的答案如下:

The Control 控制

This is just a basic dummy version of the control and you will need to add visual states and other resources, assets and custom styles but the skeleton would look like below: 这只是控件的基本虚拟版本,您将需要添加视觉状态以及其他资源,资产和自定义样式,但其骨架如下所示:

The C# 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


}

Once created the Control, now you need to add the default style to it: The Resource Dictionary 创建控件后,现在需要向其添加默认样式: 资源字典

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

The above are the two necessary things required for the control to function properly. 以上是控件正常运行所需的两个必要条件。

Later on you can edit the resource dictionary to personalize the control and once you have it all figured out you can freeze it and when you use it in other apps, you can simply override the default style instead of manually changing the resource dictionary each time. 稍后,您可以编辑资源字典以个性化控件,一旦弄清所有内容,便可以冻结它,并在其他应用程序中使用它时,您可以覆盖默认样式,而不必每次手动更改资源字典。

I hope this helps. 我希望这有帮助。 I've uploaded a copy of a template 10 version of the solution on GitGub 我已在GitGub上上传了该解决方案的模板10版本的副本

Finally, I got this working. 终于,我开始工作了。 Here is the solution for future reference, if you ever stuck in this situation. 如果您遇到这种情况,这里是解决方案,供以后参考。

The following will need to be inside your ViewModel inheriting the ViewModelBase in Template10. 在继承Model10的ViewModelBase的ViewModel中,需要包含以下内容。

var nav = Template10.Common.WindowWrapper.Current().NavigationServices.FirstOrDefault();
        var frame = nav.Frame;
        if (frame.CanGoBack)
            frame.GoBack();

You can also make it so that the back button is visible or not by using the CanGoBack property. 您也可以使用CanGoBack属性使后退按钮可见或不可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM