简体   繁体   English

WPF 应用程序不更新任务栏图标

[英]WPF app doesn't update taskbar icon

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);

Where this is the MainWindow . thisMainWindow 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.奇怪的是,我在另一个测试应用程序中尝试了完全相同的代码,并且效果很好。

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.我在测试应用程序和我的应用程序之间看到的唯一区别是我手动定义了我的Main方法,我在其中创建了应用程序实例。

[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();
    }

I've seen suggestions about relaunching explorer.exe and deleting Windows' icon cache.我已经看到有关重新启动explorer.exe和删除 Windows 图标缓存的建议。 None of them seems to help.他们似乎都没有帮助。

Any help will be highly appreciated.任何帮助将不胜感激。

UPDATE: The taskbar icon seems to update as expected when built with Release configuration... which confuses me even more.更新:使用Release配置构建时,任务栏图标似乎按预期更新......这让我更加困惑。

It seems I've missed an important clue: the application that didn't want to change the taskbar icon was deployed by using ClickOnce .我似乎错过了一个重要线索:不想更改任务栏图标的应用程序是通过使用ClickOnce部署的。

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

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:为了解决这个问题,在不删除 ClickOnce 部署机制的情况下,我创建了一个方法,该方法在应用程序的Main方法中调用,它执行 4 件事:

  1. Checks if the application was launched via ClickOnce, if yes:检查应用程序是否通过 ClickOnce 启动,如果是:
  2. Copies application files from "original" ClickOnce folder ( %localappdata%\\Apps ) to another location, in my case a folder within %appdata% ;将应用程序文件从“原始”ClickOnce 文件夹( %localappdata%\\Apps%localappdata%\\Apps到另一个位置,在我的例子中是%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.另一种解决方案,我认为是正确的,是完全放弃 ClickOnce。 Choosing another deployment mechanism will save you a lot of time and effort.选择另一种部署机制将为您节​​省大量时间和精力。

Hope this will help someone.希望这会帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM