我是否误解了C#WPF中的事件? [英] Am I misunderstanding events in C# WPF?

查看:56
本文介绍了我是否误解了C#WPF中的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有SDK的打印机在WPF中处理应用程序,该SDK表明它具有StatusUpdate事件。到目前为止,即使我执行一个我知道应该触发的动作,我也无法恢复状态。



我在Windows窗体中使用了代码应用程序,它的工作原理应该如此。

我怀疑使用MVVM设计在WPF中编码它是一个问题。我已经尝试将事件处理程序代码放入MainView代码后面,然后是MainViewModel。我创建了一个单独的clss,看看我是否也可以从那里开火。



有没有人对我能对触发的事件做些什么有任何建议WPF?



或者我错过了关于活动的其他内容?



我尝试过什么:



我尝试将事件处理程序代码放入MainView代码后面,然后是MainViewModel。我创建了一个单独的clss,看看我是否也可以从那里开始。

解决方案

要从打印机监听事件,你需要在事件中工作系统。

鉴于我对.Net的这一部分不熟悉,我只能让你进入球场。

  using  Microsoft.PointOfService; 

命名空间 EventHandlers
{
public class PrinterEventHandler
{
private PosPrinter _printer;

public PrinterEventHandler(PosPrinter printer)
{
_printer = printer;
_printer.OnStatusUpdate + = HandleStatusUpdate; // 添加侦听器功能。 OnStatusUpdate应该是一个函数,在调用时会引发状态更新事件
}

public void HandleStatusUpdate( object sender,StatusUpdateEventArgs e)
{
// 回复活动......
}
}
}


I am working on an application in WPF using a printer with an SDK that says it has a StatusUpdate event. So far I can't get the status to come back even when I perform an action that I know should trigger one.

I have used the code in a windows forms application and it works like it should.
I suspect there is an issue with coding it in WPF using an MVVM design. I have tried putting the event handler code into the MainView code behind, then the MainViewModel. I created a separate clss to see if I could get it to fire from there too.

Does anyone have any suggestions on what I could do to the events to trigger in WPF?

Or am I missing something else about Events?

What I have tried:

I have tried putting the event handler code into the MainView code behind, then the MainViewModel. I created a separate clss to see if I could get it to fire from there too.

解决方案

To listen for events from the printer, you need to work within the Event system.
Given my unfamiliarity to this part of .Net, I can only get you within the ballpark.

using Microsoft.PointOfService;

namespace EventHandlers
{
  public class PrinterEventHandler
  {
    private PosPrinter _printer;

    public PrinterEventHandler(PosPrinter printer)
    {
      _printer = printer;
      _printer.OnStatusUpdate += HandleStatusUpdate;  // Add a listener function.  OnStatusUpdate should be a function that when called which raises that status updated event
    }

    public void HandleStatusUpdate(object sender, StatusUpdateEventArgs e)
    {
      // Respond to event...
    }
  }
}


这篇关于我是否误解了C#WPF中的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆