Icons don't work after publish

2019-06-21 05:46发布

问题:

I have a simple application. When you click a button, the tasbar icon changes. When I Run this app from visual studio, everything works fine, but when I publish the WPF app, the taskbar Icon does not work (there is none).

The build action is set to "embedded resource/copy always", I have tested "Resource" as well but it doesn't work.

var iconUri = new Uri("pack://application:,,,/images/internet_connection.ico", UriKind.RelativeOrAbsolute);
        this.Icon = BitmapFrame.Create(iconUri);

the icon in the top left corner of the frame changes, but the one in the taskbar doesn't.

Can anyone help me please ?

@Edit,

I got it to work thanks to @Pavel's comment. But now one problem remains:

When I run it in visual studio, and I do this:

var iconUri = UriHelper.GetUri(this.GetType(), "images/local_network.ico");
        this.Icon = BitmapFrame.Create(iconUri);

The Icon changes. But with the published version, it doens't change.

@@Edit,

Ok so this is my code when I press a button:

  var iconUri = UriHelper.GetUri(this.GetType(), "images/internet_connection.ico");
        this.Icon = BitmapFrame.Create(iconUri);
        mNotifyIcon = new NotifyIcon
        {
            BalloonTipText = "The app has been minimised. Click the tray icon to show.",
            BalloonTipTitle = "The App",
            Text = "The App",
            Icon = BitmapFrame.Create(iconUri)
        };

        BitmapImage image = new BitmapImage();
        image.BeginInit();
        image.UriSource = UriHelper.GetUri(this.GetType(), "images/internet_connection.png");
        image.EndInit();
        TaskbarItemInfo = new System.Windows.Shell.TaskbarItemInfo() { Overlay = image };

what does it do: When running from VS: the icon in the taskbar changes, the overlay works, the icon in the top corner of the application changes.

After build running the exe: the icon in the taskbar DOES NOT change, the overlay works, the icon in the top corner of the application changes.

Can anyone explain this ?

回答1:

i think this line works for you (you have forgot the ...;component/....):

var iconUri = new Uri("pack://application:,,,/YourProjectName;component/images/internet_connection.ico", UriKind.RelativeOrAbsolute);

for the icon use

Build Action = Resource
Copy to OutputDirectory = Do not copy


回答2:

Try to set build action in 'Resource' and use a helper:

public static class UriHelper
{
    /// <summary>
    /// Gets absulute URI for provided relative path
    /// </summary>
    /// <param name="baseType">Base type for ussage as URI root</param>
    /// <param name="relativePath">Relative path</param>
    /// <returns>Absolute Uri</returns>
    public static Uri GetUri(Type baseType, string relativePath)
    {
        Assembly oAssembly = Assembly.GetAssembly(baseType);
        AssemblyName oName = oAssembly.GetName();
        return new Uri( 
                String.Format(
                    "pack://application:,,,/{0};v{1};component/{2}",
                    oName.Name,
                    oName.Version.ToString(),
                    relativePath), 
                UriKind.Absolute);
    }
}


标签: c# wpf icons