gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:02:00 27 4
gpt4 key购买 nike

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

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

var result = await secondaryTile.RequestCreateAsync();

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

最佳答案

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

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# - 如何以编程方式固定默认动态磁贴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36043566/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com