Pass data from class to Form through different class - c#

Is there a better solution to send data from one class to another through different class? I have image in class ImageProccessing and I would like to send him to class MainForm.
MainForm.cs (GUI)
VideoProccessing.cs
ImageProccessing.cs
I have this pseudocode:
class MainForm : Form
{
VideoProccessing _video = new VideoProccessing();
}
class VideoProccessing
{
ImageProccessing _image = new ImageProccessing();
}
class ImageProccessing
{
Bitmap _bmp = null;
public void SomeBadassProcess()
{
_bmp = new Bitmap(); //How to send this to MainForm (GUI)
}
}
My solution:
class MainForm : Form
{
VideoProccessing _video = new VideoProccessing();
_video.SendAgain += (ShowImage);
private void ShowImage(Bitmap bmp)
{
SomePictureBox.Image = bmp;
}
}
class VideoProccessing
{
ImageProccessing _image = new ImageProccessing();
_image.Send += (ReceivedImage)
public delegate void SendAgainImage(Bitmap bmp);
public event SendAgainImage SendAgain;
private void ReceivedImage(Bitmap bmp)
{
SendAgain(bmp);
}
}
class ImageProccessing
{
Bitmap _bmp = null;
public delegate void SendImage(Bitmap bmp);
public event SendImage Send;
public void SomeBadassProcess()
{
_bmp = new Bitmap(); //How to send this to MainForm (GUI)
Send(_bmp);
}
}

If your VideoProcessing class has a dependency on the ImageProcessing class, however, you only wish to make your MainForm aware of VideoProcessing, then propagating your message in the manner that you are is probably acceptable. It does however create a strong coupling between your objects, which, you may or may not care about at this stage.
Another method could be to use some sort of message bus such as an EventAggregator or a Mediator. Both of these behavioural patterns are designed to decouple objects involved in the messaging process and provide a publish/subscribe mechanism.
As an example if you were to implement the EventAggregator your MainForm would be a subscriber, listening out for notifications sent from a publisher which in your scenario is the ImageProcessing class.
A contrived code example might look like the following:
public class MainForm : Form
{
private readonly EventAggregator eventAggregator;
private readonly VideoProcessing videoProcessing;
public MainForm()
{
this.eventAggregator = new EventAggregator();
this.eventAggregator.Subscribe<NewBitmapEvent>(HandleNewBitMap);
this.videoProcessing = new VideoProcessing(this.eventAggregator);
}
private void HandleNewBitmap(Bitmap args)
{
// Do something
}
}
public class VideoProcessing
{
private readonly EventAggregator eventAggregator;
private readonly ImageProcessing imageProcessing;
public VideoProcessing(EventAggregator eventAggregator)
{
this.eventAggregator = eventAggregator;
this.imageProcessing = new ImageProcessing(this.eventAggregator);
}
}
public class ImageProcessing
{
private readonly EventAggregator eventAggregator;
public ImageProcessing(EventAggregator eventAggregator)
{
this.eventAggregator = eventAggregator;
this.eventAggregator.Publish<NewBitmapEvent>(new Bitmap(...));
}
}
When the ImageProcessing class publishes a NewBitmapEvent, only the MainForm is notified as only it, and not the VideoProcessing class is subscribed to the event. Obviously if other classes were subscribed then they too would be notified when the event is published.

Related

Published Event not being subscribed or Published

When I am trying to publish an events from SimpleFreeDrawModifier class to FieldSettingViewModel class, Subscriber class not able to get subscribe the events or vice versa.
Publishing Events Code:
public SimpleFreeDrawModifier(RegionManager regionManager, EventAggregator eventAggragator) // Constructor
{
_regionManager = regionManager;
_eventAggragator = eventAggragator;
CorodinatesMapDictionary =new Dictionary<object, object>();
}
private void AppendPoint(Point mousePoint)
{
// On Mouse-down, append another point
if (_dataSeries != null)
{
CorodinatesMapDictionary.Clear();
_dataSeries.Append((double)XAxis.GetDataValue(mousePoint.X), (double)YAxis.GetDataValue(mousePoint.Y));
GlobalVar.XCoordinate=(double)_dataSeries.XValues[0];
GlobalVar.YCoordinate=(double)_dataSeries.YValues[0];
CorodinatesMapDictionary.Add("X", GlobalVar.XCoordinate);
CorodinatesMapDictionary.Add("Y", GlobalVar.YCoordinate);
var calc = Math.Abs(GlobalVar.XCoordinate) * 1.0913;
if (GlobalVar.YCoordinate < 0)
{
if (GlobalVar.YCoordinate >= -(calc))
{
_eventAggragator.GetEvent<MapCoOrdinateChangedEvents>().Publish();
}
}
else
{
if(Math.Abs(GlobalVar.XCoordinate) <=25 && GlobalVar.YCoordinate <=25)
{
_eventAggragator.GetEvent<MapCoOrdinateChangedEvents>().Publish();
}
}
}
}
Subscribing Events Code
public FieldSettingViewModel(RegionManager regionManager,IEventAggregator eventAggragator)
{
_regionManager = regionManager;
_eventAggragator = eventAggragator;
ClearCommand = new DelegateCommand(Clear);
ApplyFieldSettingCommand = new DelegateCommand(ApplyFieldSetting, canApplyFieldSetting);
ScanAreaDataSeries = new XyDataSeries<double, double>();
GetScanSeriesData();
_eventAggragator.GetEvent<MapCoOrdinateChangedEvents>().Subscribe(MapValue);
}
private void MapValue()
{
//throw new NotImplementedException();
}
Could you please provide any suggestions?
Correct me if I'm wrong but it looks like instantiation of SimpleFreeDrawModifier and FieldSettingViewModel is achieved through dependency injection.
Are you sure that an instance of FieldSettingViewModel has been created and the it's receiving the same instance of IEventAggregator?
This minimal example without DI works
using System.Drawing;
using Prism.Events;
var eventAggregator = new EventAggregator();
var pub = new Publisher(eventAggregator);
var sub = new Subscriber(eventAggregator);
pub.AppendPoint(new Point());
public class Publisher
{
private IEventAggregator _eventAggregator;
public Publisher(IEventAggregator eventAggregator) // Constructor
{
_eventAggregator = eventAggregator;
}
public void AppendPoint(Point mousePoint)
{
_eventAggregator.GetEvent<MapCoOrdinateChangedEvents>().Publish();
}
}
public class Subscriber
{
private IEventAggregator _eventAggregator;
public Subscriber(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
_eventAggregator.GetEvent<MapCoOrdinateChangedEvents>().Subscribe(MapValue);
}
private void MapValue()
{
Console.WriteLine("Map value called");
}
}
internal class MapCoOrdinateChangedEvents : PubSubEvent
{
}
Check the DI lifetime of your EventAggregator is Singleton and not Transient, also try putting a breakpoint in the constructor of FieldSettingViewModel to ensure it is run.
If it's not being run and you're registering it for DI like services.AddSingleton<FieldSettingViewModel>(), try instead using services.AddSingleton<FieldSettingViewModel>(new FieldSettingViewModel()) to ensure there is one instantiated.
The constructor of SimpleFreeDrawModifier use EventAggregator instead of the interface IEventAggregator therefore as precised by hwoodiwiss they are not using the same instance even if it has been registered as singleton.
It already happenned to me and it took me quite some time to see the missing I

Detect Windows 10 theme changing in c#

I want to detect Windows 10 theme and I wrote this code:
public class Class1
{
public Class1()
{
SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
}
private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
switch (e.Category)
{
case UserPreferenceCategory.General:
setTheme();
break;
}
}
private static bool ThemeIsLight()
{
RegistryKey registry =
Registry.CurrentUser.OpenSubKey(
#"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize");
return (int)registry.GetValue("SystemUsesLightTheme") == 1;
}
public static void setTheme()
{
var br = ThemeIsLight() ? Brushes.White : Brushes.Black;
MessageBox.Show(br.ToString());
}
}
Usage:
public MainWindow()
{
InitializeComponent();
Class1.setTheme();
}
The problem is that the code only works when the program is initially run, and the code does not run when the Windows theme is changed.
It works well if I write all the code in MainWindow.cs, but I want the code to be written in a separate class
Code above does not create any instance of the Class1. Therefore, the UserPreferenceChanged event handler does not created.
It is necessary to create instance of Class1. For example:
public partial class MainWindow : Window
{
private Class1 DetectPreferenceChange = new Class1();
public MainWindow()
{
InitializeComponent();
}
...
}
Or, if it is necessary to subscribe to the event without creating of the Class1 instance do the following:
Change signature of the event handler to public static:
public static void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
// ...
}
And now you can subscribe to the event directly:
public MainWindow()
{
InitializeComponent();
SystemEvents.UserPreferenceChanged += (s, e) => { Class1.SystemEvents_UserPreferenceChanged(s, e); };
}
Because of all methods in the class are static now the class constructor might be omitted and class can be declared as static.
For additional information see How to subscribe to and unsubscribe from events (C# Programming Guide)

Using Prism 6 Event Aggregator between view models with an object as the payload

Hi I'm hoping one of you could help me with some syntax.
So I'm following this example https://www.codeproject.com/Tips/591221/Simple-EventAggregator-in-WPF-PRISM on using Prism Event Aggregator to send messages from one ViewModel to another.
The following code works great when publishing a single string as the payload.
internal class OrderSelectedEvent_SS : PubSubEvent<string>
{
private static readonly EventAggregator _eventAggregator;
private static readonly OrderSelectedEvent_SS _event;
static OrderSelectedEvent_SS()
{
_eventAggregator = new EventAggregator();
_event = _eventAggregator.GetEvent<OrderSelectedEvent_SS>();
}
public static OrderSelectedEvent_SS Instance
{
get { return _event; }
}
}
Publisher (In ViewModel1):
OrderSelectedEvent_SS.Instance.Publish(_strCurrentOrder);
Subscriber (In ViewModel2:
OrderSelectedEvent_SS.Instance.Subscribe(OrderSelectedEventHandler_SS);
private void OrderSelectedEventHandler_SS(string strSelectedOrder)
{
CurrentOrder = strSelectedOrder;
}
What I'm trying to do is modify to accept an object (a class with multiple strings) as the payload.
Here's what I have so far:
internal class OrderSelectedEvent : PubSubEvent<object>
{
private static readonly EventAggregator _eventAggregator;
private static readonly OrderSelectedEvent _event;
static OrderSelectedEvent()
{
_eventAggregator = new EventAggregator();
_event = _eventAggregator.GetEvent<OrderSelectedEvent>();
}
public static OrderSelectedEvent Instance
{
get { return _event; }
}
public class OrderSelectedPayload
{
public string SelectedOrder { get; set; }
public string SelectedE32 { get; set; }
}
}
Publisher (In ViewModel1):
OrderSelectedEvent.OrderSelectedPayload ospl = new OrderSelectedEvent.OrderSelectedPayload();
ospl.SelectedOrder = _strCurrentOrder;
ospl.SelectedE32 = _strCurrentE32JobNumber;
OrderSelectedEvent.Instance.Publish(ospl);
Subscriber (In ViewModel2:
OrderSelectedEvent.Instance.Subscribe(OrderSelectedEventHandler);
I'm getting an error here on the subsriber: **ERROR: cannot convert from 'method group' to 'Action'**** and I can't seem to figure out what I need to do.
private void OrderSelectedEventHandler(OrderSelectedEvent.OrderSelectedPayload ospl)
{
CurrentOrder = ospl.SelectedOrder;
CurrentE32JobNumber = ospl.SelectedE32;
}
Thanks in advance for any guidance you can provide.
Your problem is simple: You are defining the event to pass an object as payload, then trying to use a specific class as payload instead. To fix this, change
internal class OrderSelectedEvent : PubSubEvent<object>
To:
internal class OrderSelectedEvent : PubSubEvent<OrderSelectedPayload>
Alternatively, you could change your handler signature to match the expected parameters of the event:
private void OrderSelectedEventHandler(OrderSelectedEvent.OrderSelectedPayload ospl)
To:
private void OrderSelectedEventHandler(object ospl)

Accessing GUI from another class via a static helper class

I just wanted to ask if the following code is a valid method to access the GUI from another class, or if it is bad practice. What I want to do is to write log messages into a RichTextBox in Form1.
If it's bad practice, would it be better to pass a reference of my Form1 to the other class to be able to access the RichTextBox.
I have the following code to access the GUI in my Form1 from another class:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Logger.Init(this.rtbLog);
MyOtherClass myOtherClass = new MyOtherClass();
myOtherClass.DoSomething();
}
}
public class MyOtherClass
{
public void DoSomething()
{
Logger.AppendText("text...");
Logger.AppendText("text...");
Logger.AppendText("text...");
}
}
public static class Logger
{
private static RichTextBox _rtb;
public static void Init(RichTextBox rtb)
{
_rtb = rtb;
}
public static void AppendText(String text)
{
_rtb.AppendText(text);
_rtb.AppendText(Environment.NewLine);
}
}
With Events (thanks to Ondrej):
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Logger.EntryWritten += Logger_EntryWritten;
MyOtherClass myOtherClass = new MyOtherClass();
myOtherClass.DoSomething();
}
void Logger_EntryWritten(object sender, LogEntryEventArgs args)
{
rtbLog.AppendText(args.Message);
rtbLog.AppendText(Environment.NewLine);
}
}
public class MyOtherClass
{
public void DoSomething()
{
Logger.AppendText("text...");
Logger.AppendText("text...");
Logger.AppendText("text...");
}
}
public static class Logger
{
public static event EventHandler<LogEntryEventArgs> EntryWritten;
public static void AppendText(string text)
{
var tmp = EntryWritten;
if (tmp != null)
tmp(null, new LogEntryEventArgs(text));
}
}
public class LogEntryEventArgs : EventArgs
{
private readonly String message;
public LogEntryEventArgs(String pMessage)
{
message = pMessage;
}
public String Message
{
get { return message; }
}
}
It's probably fine for a small throw-away project, but otherwise a logger should not know anything about used platform. Then it would be good to use events for example. Raise an event whenever there's a new log entry written and consumers interested in logged entries will subscribe to a delegate.
Also be careful with threads. If you log a message from a different thread than UI you will end up with an exception because you would access a GUI control from a different thread which is forbidden.
EDIT:
Something along these lines. LogEntryEventArgs is a type you have to create and you can give it properties like Message, TimeWritten, Severity, etc.
public static class Logger
{
public static event EventHandler<LogEntryEventArgs> EntryWritten;
public static void AppendText(string text)
{
var tmp = EntryWritten;
if (tmp != null)
tmp(null, new LogEntryEventArgs(text));
}
}
consumer:
Logger.EntryWritten += Logger_OnEntryWritten;
void Logger_OnEntryWritten(object sender, LogEntryEventArgs args)
{
_rtb.AppendText(args.Message);
_rtb.AppendText(Environment.NewLine);
}
Also, don't forget to invoke on a form/dispatch the body of Logger_OnEntryWritten in order to avoid cross-thread access exception (in case you are considering using threads).

C# Multiple class events

Im making a program what connects to multiple 3th party systems. The connect with different formats so i created multiple classes to deal with them. I have now three 4 classes.
The MainForm is the first class. This is the basic windows form class with the user interface.
SDKCommunication is the second class.
VMS (this class handles the events given of by the 2th party system and activates methods on SDK COmmunication)
Events
Events Class
public class Events
{
public event EventHandler LoginStateChanged;
private bool loginstate;
public bool LogInState
{
get { return this.loginstate; }
set
{
this.loginstate = value;
if (this.LoginStateChanged != null)
this.LoginStateChanged(this, new EventArgs());
}
}
}
part of SDKCommunicatie class
Events events = new Events();
public void onLogon(string username, string directory, string system)
{
events.LogInState = false;
}
MainForm Class
SDKCommunicatie sdkcommunicatie = new SDKCommunicatie();
Events events = new Events();
public MainForm()
{
InitializeComponent();
events.LoginStateChanged += new EventHandler(events_LoginStateChanged);
}
void events_LoginStateChanged(object sender, EventArgs e)
{
log.Info("EventFired loginstateChanged");
}
When the LogInState Changes in the SDKCommunicatie class. There needs to be an event fired in the MainForm class. But sadly that doesn't work.
But when I change the loginstate in the mainform(with a buttonclick)(see code below) the event is fired. But that is not the intention i would like to have.
private void button1_Click(object sender, EventArgs e)
{
events.LogInState = true;
}
If my question isn't clear enough, please let me know.
VMS class Added as reply to #Astef
class VMS {
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(MainForm));
GxUIProxyVB m_UIProxy = new GxUIProxyVB();
public string username2;
public string directory2;
public string Status;
public void initOmni()
{
m_UIProxy.CreateInstance();
m_UIProxy.OnLogon += new _IGxUIProxyVBEvents_OnLogonEventHandler(m_UIProxy_OnLogon);
m_UIProxy.OnLogoff += new _IGxUIProxyVBEvents_OnLogoffEventHandler(m_UIProxy_OnLogoff);
m_UIProxy.OnError += new _IGxUIProxyVBEvents_OnErrorEventHandler(m_UIProxy_OnError);
m_UIProxy.OnAlarmStatusEx2 += new _IGxUIProxyVBEvents_OnAlarmStatusEx2EventHandler(m_UIProxy_OnAlarmStatusEx2);
}
public void login(string username, string password, string directory)
{
username2 = username;
directory2 = directory;
initOmni();
m_UIProxy.LogOn(directory, username, password,false);
}
public void logOff()
{
m_UIProxy.LogOff();
}
void m_UIProxy_OnLogon()
{
SDKCommunicatie sdkcommunicatie = new SDKCommunicatie();
sdkcommunicatie.onLogon(username2, directory2, "Genetec Omnicast");
}
I have fixed this with deleting the following:
SDKCommunicatie sdkcommunicatie = new SDKCommunicatie();
And adding the following in the base of VMS:
SDKCommunicatie sdkcommunicatie;
But now i got a new error in the mainform when i tried to call a class in SDKCommunicatie
connectedStatus = sdkcommunicatie.connectedStatus();
I got the following error:
NullReferenceException was unhandled
You are not using the same instance of the Events class, and that's why on button click you catch LoginStateChanged. You should inject the same instance of Events class to SDKCommunicatie class, then you'll be able to listen to event changes.
Edit:
Jeremy Todd and I were both writing at the same time.
Events in your SDKCommunicatie are not fired because you've created an individual instance of class Events for it. That is not the instance you have placed on the MainForm.
Inject the right instance (pass a reference) to SDKCommunicatie from MainForm through constructor, property or somehow else. For example:
MainForm:
SDKCommunicatie sdkcommunicatie;
Events events = new Events();
public MainForm()
{
InitializeComponent();
events.LoginStateChanged += new EventHandler(events_LoginStateChanged);
sdkcommunicatie = new SDKCommunicatie(events);
}
void events_LoginStateChanged(object sender, EventArgs e)
{
log.Info("EventFired loginstateChanged");
}
SDKCommunicatie:
Events events;
public SDKCommunicatie(Envents eventsInstance)
{
events = eventsInstance;
}
public void onLogon(string username, string directory, string system)
{
events.LogInState = false;
}
Your SDKCommunication class and your MainForm class each have their own separate instance of Events, so any events you trigger from one won't be visible from the other -- they're being raised on an entirely different object.
What you need is a single instance of the Events class that both SDKCommunication and MainForm can share -- that way they'll both be seeing the same thing. There are several different approaches you could take for this. Depending on what it needs to do, one very simple possibility might be to make Events a static class, and then the events would be visible everywhere without needing to create any instances.
I have solved the riddle.
When i need a method is a class i can call the method directly like this:
public class MainForm : Form
{
SDKCommunication sdkcommunication = new SDKCommunication();
public MainForm()
{
}
private void Button1_Click(oject sender, EventArgs e)
{
sdkcommunication.method("Test")
}
}
This is pretty straightforward. Look here the receiverclass:
public class SDKCommunication
{
method(string word)
{
//do something with word
}
}
The biggest problem is calling the class with the form(the original class). I have solved this with a eventhandler.
class CustomEventHandler1 : EventArgs
{
public CustomEventHandler1(string u, string d)
{
msgu = u;
msgd = d;
}
private string msgu;
private string msgd;
public string Username
{
get { return msgu; }
}
public string Directory
{
get { return msgd; }
}
}
Then the SDKCOmmunication class should look like this:
class SDKCommunication
{
public event EventHandler<CustomEventHandler1> RaiseCustomEventHandler1;
protected virtual void OnRaiseCustomEventHandler1(CustomEventHandler1 e)
{
EventHandler<CustomEventHandler1> handler = RaiseCustomEventHandler1;
if (handler != null)
{
handler(this,e);
}
}
//Custom Method that is called somewhere
internal void custommethod()
{
OnRaiseCustomEventHandler1(new CustomEventHandler1("johnsmith", "localhost");
}
}
Then in the mainform class:
public class MainForm : Form
{
public MainForm()
{
sdkcommunication.RaiseCustomEventHandler1 += new EventHandler<CustomEventHandler1>(sdkcommunication_RaiseCustomEventHandler1);
}
void sdkcommunication_RaiseCustomEventHandler1(object sender, CustomEventHandler1 e)
{
//Do something.
}
}
The information sended with the event you can get with e.Username and e.Directory. In this example they are strings where e.Username = johnsmith and e.Directory = localhost.
I hope somebody can use this information for their own code.

Categories

Resources