如何以编程方式固定默认动态磁贴?

How can I pin a default live tile programmatically?

我想请用户在 Windows 10(通用 Windows 平台)中首次运行应用程序时固定以启动默认动态磁贴。

我知道对于 secondaryTile 你可以使用下面的代码:

var result = await secondaryTile.RequestCreateAsync();

默认动态磁贴的等效项是什么?

无法以编程方式固定默认动态磁贴。您只能固定辅助磁贴。

默认图块始终以编程方式提供,但不能由应用程序固定。仅由用户自己从应用列表中选择。

您最好的解决方案是创建一个辅助磁贴并要求固定它。 (最好让辅助磁贴转到应用程序的特定区域,因为这是辅助应用程序的用途)

这里是关于如何实施辅助应用程序以及如何固定它们的指南:https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868249.aspx

自从提出这个问题后,UWP API: StartScreenManager 在 V4 中新增了一个功能,可让您将应用程序的默认磁贴固定到开始屏幕。这是一个可以让您执行此操作的命令 - 如果该图块已经存在,则该命令将被禁用。它处理 Window Activated 事件,因为用户可以手动删除固定的磁贴:

using System;
using System.Linq;
using System.Windows.Input;
using Windows.ApplicationModel;
using Windows.Foundation.Metadata;
using Windows.UI.Core;
using Windows.UI.StartScreen;
using Windows.UI.Xaml;

namespace Synergist
{
    /// <summary>
    ///     Pin the first entry in the package's app list to the start screen
    /// </summary>
    public class PinToStartCommand : ICommand
    {
        private bool _canExecute;

        /// <summary>
        ///     Initializes a new instance of the PinToStartCommand class.
        /// </summary>
        public PinToStartCommand()
        {
            Window.Current.Activated += Current_Activated;
        }

        /// <summary>
        ///     Can execute changed event handler
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        ///     returns true if the StartScreenManager exists
        /// </summary>
        /// <param name="parameter">the parameter is not used</param>
        /// <returns>true if the app is not pinned to the start screen and the API is available</returns>
        public bool CanExecute(object parameter)
        {
            return _canExecute;
        }

        /// <summary>
        ///     Pin the app to the start screen
        /// </summary>
        /// <param name="parameter">the parameter is not used.</param>
        public async void Execute(object parameter)
        {
            if (ApiInformation.IsTypePresent("Windows.UI.StartScreen.StartScreenManager"))
            {
                var entries = await Package.Current.GetAppListEntriesAsync();

                var firstEntry = entries.FirstOrDefault();

                if (firstEntry == null)
                    return;

                var startScreenmanager = StartScreenManager.GetDefault();

                var containsEntry = await startScreenmanager.ContainsAppListEntryAsync(firstEntry);

                if (containsEntry)
                    return;

                if (await startScreenmanager.RequestAddAppListEntryAsync(firstEntry))
                {
                    _canExecute = false;

                    CanExecuteChanged?.Invoke(this, new EventArgs());
                }
            }
        }

        private async void Current_Activated(object sender, WindowActivatedEventArgs e)
        {
            var entries = await Package.Current.GetAppListEntriesAsync();

            var firstEntry = entries.FirstOrDefault();

            if (firstEntry == null)
            {
                _canExecute = false;

                return;
            }

            if (ApiInformation.IsTypePresent("Windows.UI.StartScreen.StartScreenManager"))
            {
                var startScreenmanager = StartScreenManager.GetDefault();

                _canExecute = !await startScreenmanager.ContainsAppListEntryAsync(firstEntry);

                CanExecuteChanged?.Invoke(this, new EventArgs());
            }
        }
    }
}