发布后图标不起作用

发布于 2024-12-17 07:30:04 字数 1534 浏览 0 评论 0原文

我有一个简单的应用程序。单击按钮时,任务栏图标会发生变化。当我从 Visual Studio 运行此应用程序时,一切正常,但是当我发布 WPF 应用程序时,任务栏图标不起作用(没有)。

构建操作设置为“嵌入资源/始终复制”,我也测试了“资源”,但它不起作用。

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

框架左上角的图标发生变化,但任务栏中的图标没有变化。

有人可以帮我吗?

@编辑,

感谢@Pavel 的评论,我让它正常工作。但现在仍然存在一个问题:

当我在 Visual Studio 中运行它时,我这样做:

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

图标发生了变化。但对于发布的版本来说,它并没有改变。

@@编辑,

好的,这是我按下按钮时的代码:

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

它会做什么: 从 VS 运行时:任务栏中的图标发生变化,覆盖层起作用,应用程序右上角的图标发生变化。

构建后运行 exe:任务栏中的图标不会改变,覆盖层有效,应用程序右上角的图标发生变化。

谁能解释一下吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染火枫林 2024-12-24 07:30:04

我认为这条线适合你(你忘记了......;组件/......):

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

用于图标使用

Build Action = Resource
Copy to OutputDirectory = Do not copy

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
涫野音 2024-12-24 07:30:04

尝试在“资源”中设置构建操作并使用助手:

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

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文