Home > database >  notifyIcon in Microsoft.NET.Sdk.Web
notifyIcon in Microsoft.NET.Sdk.Web

Time:12-28

i have WASM app to request api to localhost for manage file and folder ... beside this i have another web application that handle WASM request ....

now I want add notifyIcon to localhost web application so user can see config and port usage for it ...

this is project file for web application

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>net7.0-windows</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <OutputType>WinExe</OutputType>
        <UseWindowsForms>true</UseWindowsForms>
    </PropertyGroup>

    <ItemGroup>
      <None Update="briefcase-solid.ico">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </None>
    </ItemGroup>

</Project>

and this is program.cs of web application

var builder = WebApplication.CreateBuilder(args);
int port = builder.Configuration.GetValue<int>("Port");

NotifyIcon notifyIcon = new NotifyIcon();

notifyIcon.Visible = true;
notifyIcon.Icon = new Icon("briefcase-solid.ico");
notifyIcon.ShowBalloonTip(5000, "Port", port.ToString(), ToolTipIcon.Info);
StringBuilder sb = new StringBuilder();
sb.AppendLine("localHost Service");
sb.AppendLine("Port : "   port);
notifyIcon.Text = sb.ToString();

    notifyIcon.Click  = (o, e) =>
{
    // not execute At all
};

builder.Services.AddControllers();

var app = builder.Build();
string url = "http://localhost:"   port;
app.UseCors(x => x.AllowAnyOrigin());
app.UseLocalAccessOnlyMiddleware();
app.MapControllers();

app.MapGet("/", () => "Hello World!");
await app.RunAsync(url);

now notifyIcon show Ballon tips and text show OK ... but i cant use event call back o click on it and so on .... why ???

notifyIcon.Click not work because this is a web application not a win application

CodePudding user response:

why ???

UI elements require a message loop to process their events. Your code starts a website but no UI processing loop.

At the very least, you would need to add a message loop, e.g., Application.Run. Presumably after you start (not Run) the web app (alternatively, run the web app in a different thread).

You may also need to create the UI elements from within your message loop so they can "see" the correct UI context.

Also, consider how shutdown is going to work; the message loop gives your app a way for the OS to request shutdown, so ideally you should write a lifetime component for the web part that responds to shutdown requests from the UI.

  • Related