Connect Multiple event to same method in c# - c#

I have three event like this:
public static event EventHandler BelowLowLimit;
public static event EventHandler AboveHighLimit;
public static event EventHandler PercentBelowLowLimit;
protected virtual void OnThresholdReached()
{
EventHandler handler = null;
if (rs.Value < rs.LowLimit)
{
handler = BelowLowLimit;
}
else if (rs.Value > rs.UpperLimit)
{
handler = AboveHighLimit;
}
if (handler != null)
{
handler(this, new EventArgs());
}
}
I have to raise event based on following condition:
public long Rs
{
get { return rs.Value; }
set
{
if (rs.Value < rs.LowLimit)
{
OnThresholdReached();
}
else if (rs.Value > rs.UpperLimit)
{
OnThresholdReached();
}
else
{
this.rs.Value = value;
}
}
}
What I want to connect this event to same method to raise the event. Is this correct way to do it?
please suggest me how to do it?

Are you looking at something like this?
protected virtual bool CheckForThresholds(long rsValue, long rsLowLimit, long rsUpperLimit)
{
EventHandler handler = null;
if (rsValue < rsLowLimit)
{
handler = BelowLowLimit;
}
else if (rsValue > rsUpperLimit)
{
handler = AboveHighLimit;
}
if (handler != null)
{
handler(this, new EventArgs());
return true;
}
return false;
}
public long Rs
{
get { return rs.Value; }
set
{
if (!CheckForThresholds(value, rs.LowLimit, rs.UpperLimit))
{
this.rs.Value = value;
}
}
}

Related

How do I correctly subscribe to a PropertyChange event of another class?

I am attempting to bind a CheckBox in my View to a property in my ViewModel. I am trying to subscribe to changes of the individual ViewModel property.
I have implemented INotifyPropertyChanged on my custom class, however my handler method is never called.
I have included basic examples of the View (XAML), ViewModel, and the custom user class.
ViewModel
public class HomeViewModel: ViewModelBase
{
public HomeViewModel()
{
this.selectedUser = new usersVM();
this.selectedUser.PropertyChanged += propChangedHandler;
}
private void propChangedHandler(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "superuser") // <-- breakpoint here
{
}
}
private usersVM _selectedUser;
public usersVM selectedUser
{
get { return this._selectedUser; }
set
{
if (this._selectedUser != value)
{
this._selectedUser = value;
this.RaisePropertyChanged("selectedUser");
}
}
}
}
Custom User Class
public class usersVM : INotifyPropertyChanged
{
public usersVM()
{
this.hasChanges = false;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
private int _superuser;
public int superuser
{
get
{
return this._superuser;
}
set
{
if (value != this._superuser)
{
this._superuser = value;
NotifyPropertyChanged("username");
}
}
}
XAML #
<CheckBox Margin="0,0,8,0" Content="SuperUser" IsChecked="{Binding superuser}" DataContext="{Binding selectedUser}" />
I have a breakpoint in my Hadler Method to try to verify when the event is handled, but this is never called.
How can I properly implement INotifyPropertyChanged and subscribe to these events in my ViewModel?
Detach the PropertyChanged event handler from the current selectedUser value and attach it to the new one like this:
private usersVM _selectedUser;
public usersVM selectedUser
{
get { return _selectedUser; }
set
{
if (_selectedUser != value)
{
if (_selectedUser != null)
{
_selectedUser.PropertyChanged -= propChangedHandler;
}
_selectedUser = value;
if (_selectedUser != null)
{
_selectedUser.PropertyChanged += propChangedHandler;
}
RaisePropertyChanged("selectedUser");
}
}
}

How to stop INotify from updating twice?

I am updating a Datagrid and when a user inputs a number that already exists I want notify the user they the number already exists and then clear the value from the datagrid.
I know why this is happening, but I can't figure out how to stop this or how to make a work around.
This is very simplified code: Using EF code first with MVVM model.
public partial class StaffMasterData
{
public System.Guid Id { get; set; } // ID (Primary key)
public int? StaffNo { get; set; } // StaffNo
public StaffMasterData()
{
InitializePartial();
}
partial void InitializePartial();
}
Entity extension class for StaffMasterData :
public partial class StaffMasterData : INotifyPropertyChanged
{
partial void InitializePartial()
{
Id = Guid.NewGuid();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
And the method to save the data:
public void SaveMasterData(StaffMasterData nwRowData)
{
using (var db = CreateDbContext())
{
//MasterDataBinding is the observableCollection
//the datagrid is being bound to.
var staffNoExists = MasterDataBinding.Any(p => p.StaffNo == nwRowData.StaffNo);
if (!staffNoExists)
{
db.StaffMasterDatas.AddOrUpdate(nwRowData);
db.SaveChanges();
}
else
{
Alerts.Error("Staff Number exists");
nwRowData.StaffNo = null;
}
}
}
And the assinging of the collection changed event:
public class ShiftManagerViewModel : INotifyPropertyChanged
{
private ObservableCollection<StaffMasterData> _mMasterDataBinding = new ObservableCollection<StaffMasterData>();
public ObservableCollection<StaffMasterData> MasterDataBinding
{
get { return _mMasterDataBinding; }
set
{
if (value != _mMasterDataBinding)
{
_mMasterDataBinding = value;
OnPropertyChanged();
}
}
}
public ShiftManagerViewModel()
{
_mMasterDataBinding.CollectionChanged += collectionChanged_Event;
}
private void collectionChanged_Event(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null && e.NewItems.Count > 0)
{
foreach (INotifyPropertyChanged item in e.NewItems.OfType<INotifyPropertyChanged>())
{
item.PropertyChanged += propertyChanged_Event;
}
}
if (e.OldItems != null && e.OldItems.Count > 0)
{
foreach (INotifyPropertyChanged item in e.OldItems.OfType<INotifyPropertyChanged>())
{
item.PropertyChanged -= propertyChanged_Event;
}
}
}
public void propertyChanged_Event(object sender, PropertyChangedEventArgs e)
{
if (sender is StaffMasterData)
{
SaveMasterData((StaffMasterData)sender);
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
As it is probably very clear, when running through this line of code nwRowData.StaffNo = null; , it fires the event again as the collection has been modified which then in turn runs through the messageBox code and it pops up twice.
Honestly I have hit a brick wall with this and any point in the right direction would be appreciated.
You could use a flag that determines whether to actually call the SaveMasterData method. Set this flag to false just before you set the StaffNo property to null and then set it back to true immediately afterwards:
private bool _handle = true;
public void SaveMasterData(StaffMasterData nwRowData)
{
using (var db = CreateDbContext())
{
//MasterDataBinding is the observableCollection
//the datagrid is being bound to.
var staffNoExists = MasterDataBinding.Any(p => p.StaffNo == nwRowData.StaffNo);
if (!staffNoExists)
{
db.StaffMasterDatas.AddOrUpdate(nwRowData);
db.SaveChanges();
}
else
{
Alerts.Error("Staff Number exists");
_handle = false;
nwRowData.StaffNo = null;
_handle = true;
}
}
}
public void propertyChanged_Event(object sender, PropertyChangedEventArgs e)
{
if (!_handle && sender is StaffMasterData)
{
SaveMasterData((StaffMasterData)sender);
}
}

Xamarin forms, working with a OnPropertyChanged with a INT value

I have one button on my MasterDetailPage changing the value on an INT (named App.value1) depending on what you click looking like this:
void click1 (object s, EventArgs a)
{
if (App.value1 == 0) {
App.value1 = App.value1 + 1;
} else {
App.value1 = 0;
}
}
And I want this click function to immediately change the value on my StartPage (another ContentPage). So I have created a viewmodel looking like this, where I try to work with the current value:
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged (string propertyName)
{
var changed = PropertyChanged;
if (changed != null) {
PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
}
}
public int currentValue {
get {
return App.value1;
}
set {
if (App.value1 == 0) {
App.value1 = 0;
} else {
App.value1 = 1;
}
}
}
And this is the StartPage where I want the value of the INT to update immediately depending on what you clicked on at the MasterDetailView.
public StartPage ()
{
var ourView = new StartPageViewModel ();
ourCurrentValue = ourView.currentValue;
}
protected async override void OnAppearing()
{
LoadData();
}
private async Task<List<Pin>> LoadData() //I work with pins here (not showing that code though as it is irrelavant.
{
if (ourCurrentValue == 0) {
System.Diagnostics.Debug.WriteLine ("Value is 0");
}
else {
System.Diagnostics.Debug.WriteLine ("Value is 1");
}
}
Right now I only see "Value is 0" in my log. Nothing updates when I click on my button on the MasterDetailPage.
UPDATED CODE:
public class StartPageViewModel : INotifyPropertyChanged
{
private ICommand clickCommand;
private int currentValue;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged (string propertyName)
{
var changed = PropertyChanged;
if (changed != null)
{
PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
}
}
public StartPageViewModel()
{
ClickCommand = new Command(() => CurrentValue = CurrentValue + 1);
}
public ICommand ClickCommand
{
get { return clickCommand; }
set
{
clickCommand = value;
OnPropertyChanged("ClickCommand");
}
}
public int CurrentValue
{
get { return currentValue; }
set
{
currentValue = value;
OnPropertyChanged("CurrentValue");
}
}
}
And StartPage:
public StartPage ()
{
App.PropertyChanged += (sender, args) => OnPropertyChanged("currentValue"); // ERROR: `An object reference is requiered to access non-static member 'Xamarin.Forms.BindableObject.PropertyChanged`
}
You can proceed with something like that:
Make following changes to your App class and value1 property inside that class:
public static event PropertyChangedEventHandler PropertyChanged;
private static void OnPropertyChanged (string propertyName)
{
var changed = PropertyChanged;
if (changed != null)
{
PropertyChanged (null, new PropertyChangedEventArgs (propertyName));
}
}
private static int _value1;
public static int value1
{
get { return _value1; }
set
{
_value1 = value;
OnPropertyChanged("value1");
}
}
Then add this line to your StartPageViewModel constructor:
App.PropertyChanged += (sender, args) => OnPropertyChanged("currentValue");
In that code you are just leveraging PropertyChanged for your own purposes (you can even create your own event for that).
I mean StartPageViewModel subscribes to PropertyChanged event in Appclass, so it will be notified when value1 change. And when it actually occurs, then it is invoking his own PropertyChanged to notify View about currentValue change.
However, I would say better solution is to share View Model between MasterDetailPage and StartPage, because using global state makes your solution hard to understand :
public class SharedViewModel : INotifyPropertyChanged
{
private ICommand clickCommand;
private int currentValue;
/* INotifyPropertyChanged implementation */
public SharedViewModel()
{
ClickCommand = new Command(() => CurrentValue = CurrentValue + 1);
}
public ICommand ClickCommand
{
get { return clickCommand; }
set
{
clickCommand = value;
OnPropertyChanged("ClickCommand");
}
}
public int CurrentValue
{
get { return currentValue; }
set
{
currentValue = value;
OnPropertyChanged("CurrentValue");
}
}
}
And you need to use the same instance of SharedViewModel in MasterDetailPage as well as StartPage

Compass in Windows Phone 8.1 COMException OnPropertyChanged MVVM

I got a problem with my MVVM pattern.
class MagnetometrViewModel : INotifyPropertyChanged
{
private Compass compass;
double temp;
public MagnetometrViewModel()
{
compass = Compass.GetDefault();
CompassControl_Loaded();
}
private double heading;
public double Heading
{
get
{
return heading;
}
set
{
heading = value;
OnPropertyChanged("Heading");
}
}
private void CompassControl_Loaded()
{
compass = Windows.Devices.Sensors.Compass.GetDefault();
if (compass != null)
{
compass.ReadingChanged += CompassReadingChanged;
}
}
private void CompassReadingChanged(Windows.Devices.Sensors.Compass sender, Windows.Devices.Sensors.CompassReadingChangedEventArgs args)
{
temp = Convert.ToDouble(args.Reading.HeadingMagneticNorth);
Heading = temp;
//Heading = Convert.ToDouble(args.Reading.HeadingMagneticNorth);
}
#region PropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
When I debuging on that line
temp = Convert.ToDouble(args.Reading.HeadingMagneticNorth);
it get the result, but my other method OnPropertyChanged throws exception:
System.Runtime.InteropServices.COMException
You need to fire the PropertyChanged events on the UI thread, if you're doing a Silverlight Windows Phone app, then you can do something a little like:
protected delegate void OnUIThreadDelegate();
/// <summary>
/// Allows the specified delegate to be performed on the UI thread.
/// </summary>
/// <param name="onUIThreadDelegate">The delegate to be executed on the UI thread.</param>
protected static void OnUIThread(OnUIThreadDelegate onUIThreadDelegate)
{
if (onUIThreadDelegate != null)
{
if (Deployment.Current.Dispatcher.CheckAccess())
{
onUIThreadDelegate();
}
else
{
Deployment.Current.Dispatcher.BeginInvoke(onUIThreadDelegate);
}
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
OnUIThread(() =>
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
});
}
If you're using the more modern Universal style apps, then simply change the OnUIThread implementation to be something more like:
protected async void OnUIThread(DispatchedHandler onUIThreadDelegate)
{
if (onUIThreadDelegate != null)
{
var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
if (dispatcher.HasThreadAccess)
{
onUIThreadDelegate();
}
else
{
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, onUIThreadDelegate);
}
}
}

Check if event has any listeners?

Is it possible to detect if event has any listeners? (I need to dispose my event provider object, if nobody needs it)
Assume the class is in a 3rd party library and it can't be modified:
public class Data
{
public event EventHandler OnSave;
//other members
}
In your program:
Data d = new Data();
d.OnSave += delegate { Console.WriteLine("event"); };
var handler = typeof(Data).GetField("OnSave", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(d) as Delegate;
if (handler == null)
{
//no subscribers
}
else
{
var subscribers = handler.GetInvocationList();
//now you have the subscribers
}
You can check if event is != null.
By the way, in C# you need this check each time you raise an event:
if (TheEvent != null) {
TheEvent(this, e);
}
and the reason is exactly to check if the event has any listener.
EDIT
Since you can't access TheEvent from outside the class, you could implement a method that does the check:
public class TheClass {
public bool HasEventListeners() {
return TheEvent != null;
}
}
void Main()
{
Console.WriteLine(ContainsOnSomethingEvent()); // false
OnSomething += (o,e) => {};
Console.WriteLine(ContainsOnSomethingEvent()); // true
}
EventHandler mOnSomething;
event EventHandler OnSomething {
add { mOnSomething = (EventHandler)EventHandler.Combine(mOnSomething, value); }
remove { mOnSomething = (EventHandler)EventHandler.Remove(mOnSomething, value); }
}
public bool ContainsOnSomethingEvent() {
return mOnSomething != null && mOnSomething.GetInvocationList().Length > 0;
}

Categories

Resources