WPF使用资源文件设置托盘(NotifyIcon)图标

WPF中使用Winform中的NotifyIcon类展示托盘。在网上大部分的例子中,使用程序的外部图片文件作为托盘图标。并且只能使用ico格式。

如何使用资源文件设置托盘图标呢?

方法如下:

Icon = new Icon(Application.GetResourceStream(new Uri("pack://application:,,,/icon.ico", UriKind.RelativeOrAbsolute)).Stream)



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF 中,我们可以使用独立资源文件(.xaml)来定义应用程序中的样式、模板、图像和字符串等资源。这些资源文件可以在多个页面或窗口中共享,并可以动态地加载和卸载,提高了应用程序的可维护性和可扩展性。 下面是一个简单的独立资源文件的示例: ```xml <!-- AppResources.xaml --> <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush x:Key="ButtonBackgroundBrush" Color="#FF006699" /> <SolidColorBrush x:Key="ButtonForegroundBrush" Color="White" /> <Style TargetType="Button" x:Key="MyButtonStyle"> <Setter Property="Background" Value="{StaticResource ButtonBackgroundBrush}" /> <Setter Property="Foreground" Value="{StaticResource ButtonForegroundBrush}" /> <Setter Property="FontSize" Value="16" /> <Setter Property="Padding" Value="10,5" /> <Setter Property="Margin" Value="5" /> </Style> </ResourceDictionary> ``` 在上面的示例中,我们定义了两个 SolidColorBrush 类型的资源,分别用于按钮的背景和前景色。同时,我们还定义了一个名为 MyButtonStyle 的按钮样式,它使用了刚才定义的两个资源。 要在应用程序中使用这个独立资源文件,可以在 App.xaml 文件中引用它,如下所示: ```xml <!-- App.xaml --> <Application x:Class="MyApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="AppResources.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> ``` 在上面的示例中,我们将 AppResources.xaml 文件引用到了 Application.Resources 中,并使用了 MergedDictionaries 属性来合并多个资源文件。这样,在应用程序的任何页面或窗口中,都可以使用 AppResources.xaml 中定义的资源了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值