【问题标题】:How can I pin a default live tile programmatically?如何以编程方式固定默认动态磁贴?
【发布时间】:2016-03-16 18:04:37
【问题描述】:

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

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

var result = await secondaryTile.RequestCreateAsync();

默认动态磁贴的等价物是什么?

【问题讨论】:

  • 这是否也适用于 Windows 10?
  • @HappyCoding 另一个问题是针对 Windows Phone 8 Silverlight 而这个问题针对 UWP 是完全不同的
  • @RicardoPons 感谢您的澄清。我删除了重复的评论。

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


【解决方案1】:

自从提出这个问题以来,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());
            }
        }
    }
}

【讨论】:

  • 谢谢@Michael,我将把参考留在这里:blogs.msdn.microsoft.com/tiles_and_toasts/…
  • 非常感谢!我看到运行此代码需要用户的批准“您想将此磁贴固定到开始吗?” - 没有用户交互就没有办法吗?
  • 正确 - 用户负责他们的开始菜单。
【解决方案2】:

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

默认磁贴始终以编程方式可用,但无法由应用固定。仅由用户自己从应用列表中获取。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 2016-05-24
    • 1970-01-01
    • 2011-10-06
    相关资源
    最近更新 更多