Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 1-Sep-16 3:41am
Comments
Foothill 31-Aug-16 17:17pm    
Can you specify which type of event you are trying to trigger on? WPF has two different types of events that can be loosely referred to Top Down (uses EventArgs) and Bottom Up (uses RoutedEventArgs). Top down events, like MouseDown, are raised from the view layer but bottom up events are raised from the Dispacher layer (Windows Message Pump), like PreviewMouseDown.
FlywheelJack 31-Aug-16 17:46pm    
I couldn't begin to guess, but I can try :D
The application is using Microsoft.PointOfService to get the printer information etc. and implements its own StatusUpdateEventHandler to handle the events. This almost sounds like it would be the Bottom Up scenario since it is monitoring for status events, not necessarily triggered by the user in the UI.
Foothill 31-Aug-16 17:58pm    
Took a minute to find but your Status event is raised by the application layer. Are you registering a delegate for the event? e.g. OnStatusChanged += HandleStatusChange; where the the handler's signature is HandleStatusChange(object sender, StatusUpdateEventArgs e){ // do something }
FlywheelJack 31-Aug-16 18:10pm    
I tried different ways throughout the day;
mostly they look like this,

protected void AddStatusUpdateEvent(object eventSource)
{
EventInfo statusUpdateEvent = eventSource.GetType().GetEvent("StatusUpdateEvent");
if (statusUpdateEvent != null)
{
statusUpdateEvent.AddEventHandler(eventSource, new StatusUpdateEventHandler(OnStatusUpdateEvent));
}
}


FlywheelJack 31-Aug-16 18:11pm    
Will have to leave now, Ill try this again tomorrow, thank you for your assistance.

1 solution

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.
C#
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...
    }
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900