编程语言
首页 > 编程语言> > c# – 如何以编程方式固定默认的实时图块?

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

作者:互联网

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

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

var result = await secondaryTile.RequestCreateAsync();

默认实时图块的等效值是多少?

解决方法:

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

标签:c,win-universal-app,windows-10-universal
来源: https://codeday.me/bug/20190608/1201044.html