mmvsbg
mmvsbg

Reputation: 3588

Getting a notification from another class

I have a WPF application that displays a window with various information in it. In my code I create an instance of a custom class that I created which reads information from RFID card reader. To keep it simple - every now and then someone would swipe their card using the card reader which would generate a string that I successfully capture using my custom class.

The problem that I have is that I need to return that value to the window application so that I can update the information displayed in the window based on the value read. This is not as simple as calling a function in the custom class and returning a value as I don't know when exactly someone would swipe their card.

One solution that I could think of was to make a timer and pool the custom class every second or so to check if someone swiped their card, however, I don't think that's an effective solution.

Since I'm relatively new to WPF I'm assuming that the right way to do it is using INotifyProperyChanged but I'm unsure how to do it. Open to any other suggestions as well, thank you!

Upvotes: 0

Views: 166

Answers (3)

Karol Czajkowski
Karol Czajkowski

Reputation: 82

Create an event on the class that reads the data from RFID.

public class CardSweepedEventArgs : EventArgs {
private readonly string _data;

public string Data { get { return _data; } }

 public CardSweepedEventArgs(string data) {

  _data = data;

  }
}

public class YourReadinClass { 

public EventHandler<CardSweepedEventArgs> CardSweeped; 

// rest of logic.

}

In your class then subscribe to the event and do the necessary.

Upvotes: 0

mmvsbg
mmvsbg

Reputation: 3588

Thank you all for your posts. Using events was definitely the way but it wasn't easy to understand how they worked. Your feedback definitely helped but this article helped me understand how events worked best and how to implement them so I could deal with the issue successfully: http://www.codeproject.com/Articles/9355/Creating-advanced-C-custom-events

Upvotes: 0

Barracoder
Barracoder

Reputation: 3764

Create an event on your CardReader class that you can listen to on your ViewModel.

    class CardInfo
    {
        public string CardDetails { get; set; }
    }

    class CardSwipedEventArgs 
        : EventArgs
    {
        public CardInfo SwipedCard { get; set; }
    }

    interface ICardReader
    {
        event EventHandler<CardSwipedEventArgs> CardSwiped;
    }

    class MyViewModel : INotifyPropertyChanged
    {
        private ICardReader _cardReader;

        private string _lastCardSwiped;

        public ICardReader CardReader
        {
            get
            {
                return _cardReader;
            }
            set
            {
                _cardReader = value;
                _cardReader.CardSwiped += OnCardSwiped;
            }
        }

        private void OnCardSwiped(object sender, CardSwipedEventArgs e)
        {
            LastCardSwiped = e.SwipedCard.CardDetails;
        }

        public string LastCardSwiped
        {
            get
            {
                return _lastCardSwiped;
            }
            set
            {
                _lastCardSwiped = value;
                this.OnPropertyChanged("LastCardSwiped");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

Upvotes: 1

Related Questions