実現したいこと
.NET6のフォームアプリケーションで、タスクトレイにアイコンを表示させようとしています。
(.NET Frameworkで動かしていたプログラムから流用しています)
発生している問題・エラーメッセージ
「現在のコンテキストに 'Properties' という名前は存在しません」となり、ビルドできない
該当のソースコード
C#
1// タスクトレイアイコン 2private NotifyIcon? NotifyIcon; 3 4<Form_Load内> 5 // タスクトレイアイコンを準備 6 NotifyIcon = new System.Windows.Forms.NotifyIcon(this.components); 7 NotifyIcon.Icon = Properties.Resources.TestIcon; 8 ↑ ここがエラー 9 NotifyIcon.Text = "情報サーバ"; 10 NotifyIcon.Visible = true; 11 NotifyIcon.ContextMenuStrip = this.mnuPop; 12 //DoubleClickイベントハンドラを追加 13 NotifyIcon.DoubleClick += NotifyIcon_DoubleClick!; 14
試したこと
アイコンがリソールに入っていることは、EXEのアイコンを指定したり、フォームのアイコンを設定して確認済み。
なにかリソース情報を参照する方法があると思うのですが、うまく検索で見つけることが出来ていません。
補足情報
開発環境 : VisualStudio2022 .NET6
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。

回答2件
0
解決されたようですが、誤解がありそうなので回答します。
まず大前提として.NET Core(.NET 5・ .NET 6)でも、デザイナでNotifyIcon
やリソースの作成は可能です。
しかし.NET Core(.NET 5・ .NET 6)では、Properties\Resources.resx
等は(デフォルトでは)作られなくなりました。
.NET Frameworkからの移行であれば、追加作業が必要です。
Windows フォーム アプリを .NET 5 に移行する - Windows Forms .NET | Microsoft Docs
なにかリソース情報を参照する方法があると思うのですが、うまく検索で見つけることが出来ていません。
Form1.resx
やForm1.Designer.cs
を、メモ帳ででも見てみるとヒントが書かれています(編集しないように十分注意してください)
Form1に設定したアイコンを取得するならこうでしょうか。
cs:Form1.cs
1using System.ComponentModel; 2 3namespace Q7s6n1s05k8go7b 4{ 5 public partial class Form1 : Form 6 { 7 private readonly NotifyIcon NotifyIcon; 8 9 public Form1() 10 { 11 InitializeComponent(); 12 13 var resources = new ComponentResourceManager(typeof(Form1)); 14 components ??= new Container(); 15 NotifyIcon = new NotifyIcon(components) 16 { 17 Icon = (Icon)resources.GetObject("$this.Icon")!, 18 Text = "notifyIcon1", 19 Visible = true, 20 }; 21 } 22 } 23}
cs:Form1.Designer.cs
1namespace Q7s6n1s05k8go7b 2{ 3 partial class Form1 4 { 5 /// <summary> 6 /// Required designer variable. 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// Clean up any resources being used. 12 /// </summary> 13 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region Windows Form Designer generated code 24 25 /// <summary> 26 /// Required method for Designer support - do not modify 27 /// the contents of this method with the code editor. 28 /// </summary> 29 private void InitializeComponent() 30 { 31 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); 32 this.SuspendLayout(); 33 // 34 // Form1 35 // 36 this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 24F); 37 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 38 this.ClientSize = new System.Drawing.Size(800, 450); 39 this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); 40 this.Name = "Form1"; 41 this.Text = "Form1"; 42 this.ResumeLayout(false); 43 44 } 45 46 #endregion 47 } 48}
xml:Form1.resx
1<root> 2 <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> 3 <xsd:import namespace="http://www.w3.org/XML/1998/namespace" /> 4 <xsd:element name="root" msdata:IsDataSet="true"> 5 <xsd:complexType> 6 <xsd:choice maxOccurs="unbounded"> 7 <xsd:element name="metadata"> 8 <xsd:complexType> 9 <xsd:sequence> 10 <xsd:element name="value" type="xsd:string" minOccurs="0" /> 11 </xsd:sequence> 12 <xsd:attribute name="name" use="required" type="xsd:string" /> 13 <xsd:attribute name="type" type="xsd:string" /> 14 <xsd:attribute name="mimetype" type="xsd:string" /> 15 <xsd:attribute ref="xml:space" /> 16 </xsd:complexType> 17 </xsd:element> 18 <xsd:element name="assembly"> 19 <xsd:complexType> 20 <xsd:attribute name="alias" type="xsd:string" /> 21 <xsd:attribute name="name" type="xsd:string" /> 22 </xsd:complexType> 23 </xsd:element> 24 <xsd:element name="data"> 25 <xsd:complexType> 26 <xsd:sequence> 27 <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> 28 <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" /> 29 </xsd:sequence> 30 <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" /> 31 <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" /> 32 <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" /> 33 <xsd:attribute ref="xml:space" /> 34 </xsd:complexType> 35 </xsd:element> 36 <xsd:element name="resheader"> 37 <xsd:complexType> 38 <xsd:sequence> 39 <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> 40 </xsd:sequence> 41 <xsd:attribute name="name" type="xsd:string" use="required" /> 42 </xsd:complexType> 43 </xsd:element> 44 </xsd:choice> 45 </xsd:complexType> 46 </xsd:element> 47 </xsd:schema> 48 <resheader name="resmimetype"> 49 <value>text/microsoft-resx</value> 50 </resheader> 51 <resheader name="version"> 52 <value>2.0</value> 53 </resheader> 54 <resheader name="reader"> 55 <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> 56 </resheader> 57 <resheader name="writer"> 58 <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> 59 </resheader> 60 <assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 61 <data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> 62 <value> 63 AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAwAAAAAAAAAAAAAAAAAAAAAAA 64 AAAAAAAAAACAAACAAAAAgIAAgAAAAIAAgACAgAAAgICAAMDAwAAAAP8AAP8AAAD//wD/AAAA/wD/AP// 65 AAD///8A///////////4iIiP+IiIj/h4h4/4eIeP+IiIj/h4iI/4iIiP+IiIj/h4h4/4eIeP+IiIj/iI 66 iI////////////////////////+Ij/iI/////3eP+Hf/////d4/4d///////h3j///////+HeP////// 67 //iI//////////////8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 68 AAAAAAAAAAAAAAAAAAAAAAAA 69</value> 70 </data> 71</root>
投稿2022/04/21 08:56
編集2023/07/30 06:58総合スコア10004
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。

0
自己解決
Zuishin さんのご指摘に従い、.NET の埋め込みリソースを使ってやりたいことが出来ました。。
https://dobon.net/vb/dotnet/programing/bitmapresource.html
Zuishin さん ありがとうございました。
投稿2022/04/21 07:24
総合スコア40
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。