我正在开发一个c#桌面应用程序。
我们需要根据条件数取消固定应用程序磁贴。这可能发生在应用程序生命周期的任何时间,而不仅仅是在安装过程中。
我看到了this关于如何在CPP中取消固定拼贴的问题。我也尝试在C#中做到这一点但没有成功。
任何帮助?
更新
我能够编写一个C#代码,将AppUserModel_StartPinOption设置为APPUSERMODEL_STARTPINOPTION_NOPINONINSTALL,但它没有帮助:(
这是代码:
private static void InstallShortcut(string linkPath)
{
// Find the path to the current executable
// String exePath = Process.GetCurrentProcess().MainModule.FileName; //Path to the current exe file that is running. C:\\...
IShellLinkW newShortcut = (IShellLinkW)new CShellLink();
// Create a shortcut to the exe
ErrorHelper.VerifySucceeded(newShortcut.SetPath(targetPath));
ErrorHelper.VerifySucceeded(newShortcut.SetArguments(""));
// Open the shortcut property store, set the AppUserModelId property
IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut;
var APPUSERMODEL_STARTPINOPTION_NOPINONINSTALL = new PropVariant(0);
var StartPinOption = new PropertyKey(new Guid("{9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3}"), 12);
ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(StartPinOption, APPUSERMODEL_STARTPINOPTION_NOPINONINSTALL));
ErrorHelper.VerifySucceeded(newShortcutProperties.Commit());
// Commit the shortcut to disk
IPersistFile newShortcutSave = (IPersistFile)newShortcut;
ErrorHelper.VerifySucceeded(newShortcutSave.Save(linkPath, true));
}
我尝试了两种方法:删除磁贴,然后重新创建它,并更改现有磁贴的参数,但没有任何效果,磁贴仍然固定在开始菜单上。
答案 0 :(得分:2)
您是在谈论主应用磁贴还是辅助磁贴?如果您指的是辅助磁贴,则可以在this article中找到取消固定的示例代码。它的核心是(为了简单起见我修改了一下;请参阅文章了解完整代码):
// Check to see if this restaurant exists as a secondary tile and then unpin it
string restaurantKey = this.m_ViewModel.Restaurant.Key;
Button button = sender as Button;
if (button != null)
{
if (Windows.UI.StartScreen.SecondaryTile.Exists(restaurantKey))
{
SecondaryTile secondaryTile = new SecondaryTile(restaurantKey);
bool isUnpinned = await secondaryTile.RequestDeleteForSelectionAsync(GetElementRect((FrameworkElement)sender), Windows.UI.Popups.Placement.Above);
if (!isUnpinned)
{
// Do error handling here
}
}
else
{
// If we ever get to this point, something went wrong or the user manually
// removed the tile from their Start screen.
Debug.WriteLine(restaurantKey + " is not currently pinned.");
}
}
答案 1 :(得分:0)
根据the MSDN article: System.AppUserModel.StartPinOption (Windows),No pin install选项应为(1)
var APPUSERMODEL_STARTPINOPTION_NOPINONINSTALL = new PropVariant(1);
我这样做了。