问题
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
. 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.
[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. 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.
回答1:
It seems I've missed an important clue: the application that didn't want to change the taskbar icon was deployed by using 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.
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:
- Checks if the application was launched via ClickOnce, if yes:
- Copies application files from "original" ClickOnce folder (
%localappdata%\Apps
) to another location, in my case a folder within%appdata%
; - Launches the process from the folder created above;
- Kills the current process (that was launched by 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.
来源:https://stackoverflow.com/questions/50389433/wpf-app-doesnt-update-taskbar-icon