如何以编程方式固定默认的实时图块?

时间:2016-03-16 18:04:37

标签: c# win-universal-app windows-10-universal

我想要用户在Windows 10(通用Windows平台)中第一次运行应用程序时启动默认的实时磁贴。

我知道对于secondaryTile,您可以使用以下代码:

var result = await secondaryTile.RequestCreateAsync();

默认实时图块的等价物是什么?

2 个答案:

答案 0 :(得分:3)

无法以编程方式固定默认的实时图块。你只能钉二级瓷砖。

默认磁贴始终以编程方式可用,但不能由应用固定。只有用户自己从应用列表中。

您最好的解决方案是创建一个辅助磁贴并要求确定。 (甚至可以更好地使辅助磁贴转到应用程序的特定区域,因为这是辅助应用程序的用途)

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

答案 1 :(得分:2)

自从提出此问题以来,UWP API中又添加了一个新功能:V4中的StartScreenManager,可让您将应用的默认磁贴固定到开始屏幕。这是一个允许你这样做的命令 - 如果磁贴已经存在,则被禁用。它处理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());
            }
        }
    }
}