gpt4 book ai didi

c# - 使用 WinForm 作为 UWP 的 AppService

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:40 24 4
gpt4 key购买 nike

我正在尝试弄清楚如何将数据发送到我的 WinForm 组件,而不必总是回复 WinForm 发送的消息。我知道可以在 UWP App 的 package.appxmanifest 中设置 AppService 名称等。但是,在像 WinForms 这样的 Win32 环境中,它的等价物是什么。

是否需要任何代码来帮助获得答案?

谢谢

编辑

目前,我的 UWP 应用每隔几毫秒就会响应从我的 WinForm 组件发送的消息。

App.xaml.cs

protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
{
base.OnBackgroundActivated(args);
if (args.TaskInstance.TriggerDetails is AppServiceTriggerDetails)
{
appServiceDeferral = args.TaskInstance.GetDeferral();
args.TaskInstance.Canceled += OnTaskCanceled; // Associate a cancellation handler with the background task.

AppServiceTriggerDetails details = args.TaskInstance.TriggerDetails as AppServiceTriggerDetails;
Connection = details.AppServiceConnection;
Connection.RequestReceived += (new MainPage()).Connection_OnRequestReceived;
}
}

MainPage.xaml.cs

public async void Connection_OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
// write setting to stop timer
localSettings.Values["Win32Working"] = "True";

// read content
if (args.Request.Message.ContainsKey("content"))
{
object message = null;
args.Request.Message.TryGetValue("content", out message);
// if message is an int[]
if (message is int[])
{
// init field vars
int indexInArray = 0;
bool newTest1On = false;
bool newTest2On = false;
bool newTest3On = false;

foreach (int trueorfalse in (int[])message)
{
// set bool state based on index
switch (indexInArray)
{
case 0:
newCapsOn = Convert.ToBoolean(trueorfalse);
localSettings.Values["Test1"] = (Convert.ToBoolean(trueorfalse)).ToString();
break;
case 1:
newNumOn = Convert.ToBoolean(trueorfalse);
localSettings.Values["Test2"] = (Convert.ToBoolean(trueorfalse)).ToString();
break;
case 2:
newScrollOn = Convert.ToBoolean(trueorfalse);
localSettings.Values["Test3"] = (Convert.ToBoolean(trueorfalse)).ToString();
break;
default:
break;
}
indexInArray++;
}

if (newTest1On != Test1On || newTest2On != Test2On || newTest3On != Test3On)
localSettings.Values["updateUI"] = true.ToString();

// update bools
Test1On = newTest1On;
Test2On = newTest2On;
Test3On = neTest3On;

// if exit requested
if (Convert.ToBoolean(localSettings.Values["sendExit"]))
{
// tell WinForm to exit
ValueSet messageExit = new ValueSet();
messageExit.Add("exit", null);
AppServiceResponseStatus responseStatus = await args.Request.SendResponseAsync(messageExit);
localSettings.Values["sendExit"] = false.ToString();
localSettings.Values["Win32Working"] = false.ToString();
}
}
}
else if (args.Request.Message.ContainsKey("request"))
{
if (!Convert.ToBoolean(localSettings.Values["sendExit"]))
{
// send current settings as response
AppServiceResponseStatus responseStatus = await args.Request.SendResponseAsync(UpdateWin32());
}
else
{
// tell WinForm to exit
ValueSet message = new ValueSet();
message.Add("exit", null);
AppServiceResponseStatus responseStatus = await args.Request.SendResponseAsync(message);
localSettings.Values["sendExit"] = false.ToString();
localSettings.Values["Win32Working"] = false.ToString();
}
}
else if (args.Request.Message.ContainsKey("exit"))
{
// exit
Application.Current.Exit();
}
}

我的 WinForm 代码:​​

private async void threadCommunicationWinFrmAndUWP_Run()
{
bool askForInfo = false;

DoWork:

ValueSet message = new ValueSet();
if (!askForInfo) { message.Add("content", notifyIconsLogic.GetStatuses()); askForInfo = true; }
else { message.Add("request", ""); askForInfo = false; }

#region SendToUWP

// if connection isn't inited
if (connection == null)
{
// init
connection = new AppServiceConnection();
connection.PackageFamilyName = Package.Current.Id.FamilyName;
connection.AppServiceName = "NotifyIconsUWP";

// attempt connection
AppServiceConnectionStatus connectionStatus = await connection.OpenAsync();

// if UWP isn't running
if (connectionStatus == AppServiceConnectionStatus.AppUnavailable) return;
}

AppServiceResponse serviceResponse = await connection.SendMessageAsync(message);

// if UWP isn't running
if (serviceResponse.Status == AppServiceResponseStatus.Failure) return;

// get response
if (serviceResponse.Message.ContainsKey("content"))
{
object newMessage = null;
serviceResponse.Message.TryGetValue("content", out newMessage);
// if message is an int[]
if (newMessage is int[])
{
// init field vars
int indexInArray = 0;
bool showTest1 = false;
bool showTest2 = false;
bool showTest3 = false;

foreach (int trueorfalse in (int[])newMessage)
{
// set bool state based on index
switch (indexInArray)
{
case 0:
showTest1 = Convert.ToBoolean(trueorfalse);
break;
case 1:
showTest2 = Convert.ToBoolean(trueorfalse);
break;
case 2:
showTest3 = Convert.ToBoolean(trueorfalse);
break;
default:
break;
}
indexInArray++;
}

notifyIconsLogic.SetChecker(showTest1, showTest2, showTest3);
}
}
if (serviceResponse.Message.ContainsKey("exit")) Exit();
#endregion

goto DoWork;
}

这会过度增加 CPU 使用率。关键是,目前我从 UWP 应用程序获取信息到 WinForm 的唯一方法是响应循环发送的消息。

问题

我如何才能将消息从 UWP 应用程序发送到 WinForm 而不仅仅是响应循环中发送的消息?因为,我想消除对循环的依赖(CPU Purposes)。

换句话说:如何让 package.appxmanifest 的结果在 WinForms 中“工作”?

package.appxmanifest

<Extensions>
<uap:Extension Category="windows.appService">
<uap:AppService Name="NotifyIconsUWP" />
</uap:Extension>
<desktop:Extension Category="windows.fullTrustProcess" Executable="Win32\NotifyIconsComponent.exe" />
</Extensions>

最佳答案

您应该能够像这样从 UWP 应用对已建立的应用服务连接发送请求:

AppServiceResponse response = await App.Connection.SendMessageAsync(valueSet);

然后通过附加事件处理程序在 Windows 窗体应用程序中接收此消息:

connection.RequestReceived += Connection_RequestReceived;

查看 this sample它演示了控制台应用程序和 UWP 应用程序之间的两种通信方式。

关于c# - 使用 WinForm 作为 UWP 的 AppService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49895094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com