WPF应用程序不更新任务栏图标 [英] WPF app doesn't update taskbar icon

查看:383
本文介绍了WPF应用程序不更新任务栏图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个应用程序,应该在运行时更新其任务栏图标. 我正在尝试通过更改应用程序的主窗口图标来做到这一点,例如:

I have this app that should update its taskbar icon at runtime. I'm trying to do this by changing app's main window icon, like this:

var image = new WebClient().DownloadData("url_of_an_ico.ico");
this.Icon = App.Current.MainWindow.Icon = (BitmapSource)new ImageSourceConverter().ConvertFrom(image);

其中thisMainWindow.上面的代码在单击按钮时执行.它会更新窗口"图标(左上角的那个),但不会更新任务栏图标.

Where this is the MainWindow. The code above is executed on a button click. It updates the Window icon (the one from the top left corner) but it doesn't update the taskbar icon.

奇怪的是,我在另一个测试应用程序中尝试了完全相同的代码,并且效果很好.

The strange part is that I've tried the exact same code in a another test app and that worked just fine.

我在测试应用程序和应用程序之间看到的唯一区别是,我手动定义了Main方法,并在其中创建了应用程序实例.

The only difference I see between the test app and my app is that i manually defined my Main method, where I create the app instance.

[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
    public static void Main(string[] args) 
    {
       var a = new App();
       a.InitializeComponent();
       a.Run();
    }

我已经看到有关重新启动explorer.exe和删除Windows图标缓存的建议.他们似乎都没有帮助.

I've seen suggestions about relaunching explorer.exe and deleting Windows' icon cache. None of them seems to help.

任何帮助将不胜感激.

更新: 使用Release配置进行构建时,任务栏图标似乎已按预期更新...,这使我更加困惑.

UPDATE: The taskbar icon seems to update as expected when built with Release configuration... which confuses me even more.

推荐答案

似乎我错过了一个重要线索:不想更改任务栏图标的应用程序是使用ClickOnce部署的.

It seems I've missed an important clue: the application that didn't want to change the taskbar icon was deployed by using ClickOnce.

经过将近一天的调试/试用,我注意到,如果将应用程序文件(.exe和任何其他依赖项)移动到另一个文件夹,一切将按预期开始工作.

After almost a day of debugging/trials I've noticed that if the application files are moved (.exe and any other dependencies) to another folder, everything starts to work as expected.

为了解决此问题,在不删除ClickOnce部署机制的情况下,我创建了一个方法,该方法在应用程序的Main方法中被调用,该方法执行4件事:

In order to fix this, without removing the ClickOnce deployment mechanism, I've created a method, that is called in the Main method of the application, which does 4 things:

  1. 检查应用程序是否通过ClickOnce启动,如果是:
  2. 将应用程序文件从原始" ClickOnce文件夹(%localappdata%\Apps)复制到另一个位置,在我的情况下为%appdata%内的文件夹;
  3. 从上面创建的文件夹中启动该过程;
  4. 杀死当前进程(由ClickOnce启动);
  1. Checks if the application was launched via ClickOnce, if yes:
  2. Copies application files from "original" ClickOnce folder (%localappdata%\Apps) to another location, in my case a folder within %appdata%;
  3. Launches the process from the folder created above;
  4. Kills the current process (that was launched by ClickOnce);

另一种解决方案(我认为是正确的)是放弃ClickOnce.选择其他部署机制将节省大量时间和精力.

Another solution, which I think is the right one, is to ditch ClickOnce all together. Choosing another deployment mechanism will save you a lot of time and effort.

希望这会对某人有所帮助.

Hope this will help someone.

这篇关于WPF应用程序不更新任务栏图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆