我相信你现在已经找到答案了,但是并没有“解决方案”,而且有很多观点和看法,所以让我给你提供一个解决方法的选项。
实际上,这是一种常见的架构,Windows 希望你使用新的 Windows 11 MAUI API 的其他技术,但是用户并不喜欢“平铺式”设计,时间线充满了点击诱饵,不是与用户“交流”的可靠方式。
你可以通过在服务代码中启动托盘图标,使其成为仅限 Windows 的服务的几种方式之一。
基本上,你需要将服务与 System.Windows.Forms.NotifyIcon 和 TrayIcon.Visible 属性耦合起来。
通过 NotifyIcon,你可以做到以下操作:
class MyTray:IDisposable
{
NotifyIcon ni;
public ()
{
ni= new NotifyIcon()
ni.ContextMenuStrip = new MyContextMenus(menuDataContext).Create();
}
}
然后当你调用它时,你执行:
ni.Icon = Resources.Icon_red
ni.Text = "Some ballon Text"
//让托盘图标可见
ni.Visible = true;
用户可以在托盘菜单上与图标交互
这是MyContextMenu(backingField)创建的示例:
public ContextMenuStrip Create()
{
menu = new ContextMenuStrip();
ToolStripMenuItem item;
ToolStripSeparator sep;
item = new ToolStripMenuItem
{
Text = "License",
Image = Resources.contract
};
item.Click += new EventHandler(License_Click);
menu.Items.Add(item);
item = new ToolStripMenuItem
{
Text = "Service Status",
Image = Resources.data_green1
};
item.Click += new EventHandler(Status_Click);
menu.Items.Add(item);
sep = new ToolStripSeparator();
menu.Items.Add(sep);
item = new ToolStripMenuItem
{
Text = "Rule Engine Editor",
Image = Resources.data_edit1
};
item.Click += new System.EventHandler(Editor_Click);
menu.Items.Add(item);
sep = new ToolStripSeparator();
menu.Items.Add(sep);
item = new ToolStripMenuItem
{
Name = "mnuClose",
Text = "Close",
Image = Resources.data_down
};
item.Click += new EventHandler(Exit_Click);
menu.Items.Add(item);
return menu;
}
或者将其解耦,就像在这个示例中,服务可以在支持.net并通过像ProtoBuf、Sockets WCF或命名管道这样的协议进行通信的任何操作系统上运行。
也许有一种“更好”的方法来实现这个
看一下这篇文章
此示例使用命名管道(一种网络连接)与应用程序交流,使用NuGet包和WPF作为演示平台。
服务器像这样与任何监听Pipe的人交谈:
using H.Pipes;
using H.Pipes.Args;
using NamedPipesSample.Common;
namespace NamedPipesSample.WindowsService
{
public class NamedPipesServer : IDisposable
{
const string PIPE_NAME = "samplepipe";
private PipeServer<PipeMessage> server;
public async Task InitializeAsync()
{
server = new PipeServer<PipeMessage>(PIPE_NAME);
server.ClientConnected += async (o, args) => await OnClientConnectedAsync(args);
server.ClientDisconnected += (o, args) => OnClientDisconnected(args);
server.MessageReceived += (sender, args) => OnMessageReceived(args.Message);
server.ExceptionOccurred += (o, args) => OnExceptionOccurred(args.Exception);
await server.StartAsync();
}
private void OnClientConnected(ConnectionEventArgs<PipeMessage> args)
{
Console.WriteLine($"Client {args.Connection.Id} is now connected!");
await args.Connection.WriteAsync(new PipeMessage
{
Action = ActionType.SendText,
Text = "Hi from server"
});
}
private void OnClientDisconnected(ConnectionEventArgs<PipeMessage> args)
{
Console.WriteLine($"Client {args.Connection.Id} disconnected");
}
}
}
如果您按照示例进行操作,作为托盘图标的WPF应用程序将被“修剪”为以下样式:
public async Task InitializeAsync()
{
if (client != null && client.IsConnected)
return;
client = new PipeClient<PipeMessage>(pipeName);
client.MessageReceived += (sender, args) => OnMessageReceived(args.Message);
client.Disconnected += (o, args) => MessageBox.Show("Disconnected from server");
client.Connected += (o, args) => MessageBox.Show("Connected to server");
client.ExceptionOccurred += (o, args) => OnExceptionOccurred(args.Exception);
await client.ConnectAsync();
await client.WriteAsync(new PipeMessage
{
Action = ActionType.SendText,
Text = "Hello from client",
});
}
HavenDV/H.NotifyIcon.WPF
NuGetеҢ…дҪңдёәи§ЈеҶіж–№жЎҲпјҹ - undefined