- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试弄清楚如何将数据发送到我的 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/
我有一个 winform 应用程序和一个可观察的设置,如下所示: Form form = new Form(); Label lb = new Label(); form.Controls.Add(l
在 Windows 窗体中实现多项选择选项的最佳方法是什么?我想从列表中强制执行单个选择,从默认值开始。 看起来 ComboBox 是一个不错的选择,但是有没有办法指定一个非空白的默认值? 我可以在代
如何在 WinForm 应用程序中保护我的 ConnectionString? 最佳答案 你不能。尽管您可以在 app.config 文件中加密连接字符串,但应用程序需要能够解密它,因此,始终可以检索
有谁知道像 DotNetBar 那样的 Winforms 面包屑样式导航。 http://www.devcomponents.com/dotnetbar/BreadCrumbHorizontalTre
我正在寻找在 Windows 窗体 C# 中实现多选启用列表框的方法。 有什么建议? 谢谢。 最佳答案 只需添加一个 ListBox控制和设置属性:SelectionMode = SelectionM
我有一个简单的 WinForms 应用程序,用于输入测试用例。自从我将此应用程序升级到 .NET 4.0 并将新的标签页添加到标签页控件以根据 XSD 架构验证 XML 以来,该应用程序一直随机崩溃。
在老式的 MFC 中,DDX 例程内置了表单条目的验证。例如,可以自动检查用户是否在用于数字输入的文本框中输入了字符串。 Winforms中有这样的机制吗?显然,可以为“onChange”等类型的事件
我主要具有 ASP.Net 背景,并具有一些 MVC 知识。我也做了一些 Silverlight 和 MVVM,但是我现在即将转向 Winforms,我对它的经验很少,所以我想知道如何处理 MVP。
简单的问题,虽然办公室里似乎没有人知道,而且我找不到一个好的方法来问谷歌这个问题。在 winforms 中,如果您有一个处理事件的函数(在本例中是在 focusLost 上),那么该函数是否与触发该事
在 Winform 中,我有一个组框,其中有几个文本框控件。如果我删除组框,文本框也会被删除。它们以某种方式与 Groupbox 联系在一起,尽管我没有故意做任何事情来实现这种情况。问题 - 如何删除
我可以在哈希表中设置表单元素: $Hash = @{} $Hash.Main = New-Object System.Windows.Forms.Form $Hash.Main.Left = 0 $H
我是 Windows 开发新手。我开发了一个 WinForm 应用程序,它与串行设备通信并在图表上绘制数据。该应用程序应每天 7/7 24 小时运行。代码执行正确,但执行几个小时后,UI 卡住,操作系
有没有办法记录 Win Forms 应用程序中的所有点击?我想拦截点击并记录该操作以及导致该操作的控件的名称。 这可能吗? 提前致谢。 更新:我正在寻找一个应用程序范围的解决方案,是否没有办法将监听器
我不知道这是否会影响其他控件,但对于列表框和选中列表框,列表框的底部仅以一定的间隔随表单调整大小。 假设我有一个表单和一个列表框,该列表框与表单边缘的所有边都有 2px 间隙,并锚定在所有四个边上。现
我在大多数 WinForms 控件的设计 View 中看到“Tag”属性。我从未使用过此标签,并且想知道为什么我要使用它。 最佳答案 它允许您使用控件存储一些自己的数据。它在树控件中最有用,您可能希望
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
如果有,有人吗?鱼眼 (菜单/ Pane )窗体在 Ajax 应用程序中常见的控件: 例如:http://interface.eyecon.ro/demos/fisheye.html 用 google
这是 Determine Label Size based upon amount of text and font size in Winforms/C# 的倒数. 给定一个高度固定但宽度可变的矩形
我们公司正在研究为我们的开发团队采购 22"显示器。当前唯一的问题是我们的用户将使用较小的屏幕。 我们尝试使用屏幕网格工具(gridmove 和 nvidia 的 utils),但它们并不完全现实。
我有一个 winforms 应用程序,它在网络服务请求期间被锁定 我已经尝试使用 doEvents 来保持应用程序解锁,但它仍然不够响应, 我怎样才能绕过这个锁定,让应用程序始终响应? 最佳答案 最好
我是一名优秀的程序员,十分优秀!