The following code is a silverlight application but the same happens in WPF, so it seems to be just something I'm missing regarding the delegate, event, etc.
Can anyone tell me why the following code successfully executes this event:
OnLoadingComplete(this, null);
but never executes this event handler?
void initialDataLoader_OnLoadingComplete(object obj, DataLoaderArgs args)
CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Diagnostics;
namespace TestEvent22928
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
DataLoader initialDataLoader = new DataLoader("initial");
initialDataLoader.RegisterText("test1", "http://test:111/testdata/test1.txt");
initialDataLoader.RegisterText("test2", "http://test:111/testdata/test2.txt");
initialDataLoader.BeginLoading();
initialDataLoader.OnLoadingComplete += new DataLoader.LoadingComplete(initialDataLoader_OnLoadingComplete);
}
void initialDataLoader_OnLoadingComplete(object obj, DataLoaderArgs args)
{
Debug.WriteLine("loading complete"); //WHY DOES EXECUTION NEVER GET HERE?
}
}
public class DataManager
{
public DataLoader CreateDataloader(string dataloaderIdCode)
{
DataLoader dataLoader = new DataLoader(dataloaderIdCode);
return dataLoader;
}
}
public class DataLoader
{
public string IdCode { get; set; }
public List<DataItem> DataItems { get; set; }
public delegate void LoadingComplete(object obj, DataLoaderArgs args);
public event LoadingComplete OnLoadingComplete = delegate { };
private int dataItemCurrentlyLoadingIndex;
public DataLoader(string idCode)
{
IdCode = idCode;
DataItems = new List<DataItem>();
dataItemCurrentlyLoadingIndex = -1;
}
public void RegisterText(string idCode, string absoluteSourceUrl)
{
DataItem dataItem = new DataItem
{
IdCode = idCode,
AbsoluteSourceUrl = absoluteSourceUrl,
Kind = DataItemKind.Text
};
DataItems.Add(dataItem);
}
public void BeginLoading()
{
LoadNext();
}
private void LoadNext()
{
dataItemCurrentlyLoadingIndex++;
if (dataItemCurrentlyLoadingIndex < DataItems.Count())
{
DataItem dataItem = DataItems[dataItemCurrentlyLoadingIndex];
Debug.WriteLine("loading " + dataItem.IdCode + "...");
LoadNext();
}
else
{
OnLoadingComplete(this, null); //EXECUTION GETS HERE
}
}
}
public class DataItem
{
public string IdCode { get; set; }
public string AbsoluteSourceUrl { get; set; }
public DataItemKind Kind { get; set; }
public object DataObject { get; set; }
}
public enum DataItemKind
{
Text,
Image
}
public class DataLoaderArgs : EventArgs
{
public string Message { get; set; }
public DataItem DataItem { get; set; }
public DataLoaderArgs(string message, DataItem dataItem)
{
Message = message;
DataItem = dataItem;
}
}
}
You're registering the handler only after you start loading:
initialDataLoader.BeginLoading();
initialDataLoader.OnLoadingComplete += new DataLoader.LoadingComplete(initialDataLoader_OnLoadingComplete);
The way your code is currently written, it looks like BeginLoading() blocks until completion, which means the handler will never be called, as you don't set it until after you've finished loading.
Related
I have currently a problem
I have 1 Interface with two types of argument like this
ITestInterface<ArgumentA>
ITestInterface<ArgumentB>
this interface has only the argument as different
I would like to pass this interface to an constructor of a class. sth like this
public class MyClass
{
public ITestInterface<object> MyInterface {get; set;}
public MyClass(ITestInterface<ArgumentA> testInterfaceA){
this.MyInterface = testInterfaceA as ITestInterface<object>;
this.MyTestInterface.SomeEvent += this.OnSubcribe;
}
public MyClass(ITestInterface<ArgumentB> testInterfaceB){
this.MyInterface = testInterfaceB as ITestInterface<object>;
this.MyTestInterface.SomeEvent += this.OnSubcribe;
}
public void OnSubcribe(){
//Work to do here, dont care about what argument the interface has.
}
}
and to call the MyClass constructor I have sth like this:
public List<MyClass> ClassList = new();
public void testMethod(){
var interA = getInterfaceWithArgumentA();
var myClassA = new MyClass(interA);
var interB = getInterfaceWithArgumentB();
var myClassB = new MyClass(interB);
}
So the problem is i am not able to cast the interface argument to object. I dont need to differenciate the argument either. I just want to avoid to have 2 properties of MyInterface like (MyInterfaceA, MyInterfaceB).
I need also to consider that maybe in the future I will have more type of Argument so maybe to have multiple properties like MyInterfaceA, MyInterfaceB, MyInterfaceC and also multiple constructor for each Interfaceargument type would be a mess.
I just thought about have a Baseclass and the ArgumentA and ArgumentB class derived from it so the cast would work but its not like that.
How would I solve this problem ?
Many Thanks
I think you have not provided what getInterfaceWithArgumentB() and getInterfaceWithArgumentA() method doing. I am making few assumption.
To solve your problem Generic will help.
Following is the example of it.
public class MyClass<T>
{
public ITestInterface<T> MyInterface { get; set; }
public MyClass(ITestInterface<T> testInterfaceA)
{
this.MyInterface = testInterfaceA;
this.MyInterface.SomeEvent += MyInterface_SomeEvent;
}
private void MyInterface_SomeEvent(object sender, EventArgs e)
{
Console.WriteLine(sender);
}
}
public class ArgumentA
{
public string Name { get; set; }
}
public class ArgumentB
{
public int Id { get; set; }
}
public interface ITestInterface<T>
{
T Data { get; set; }
event EventHandler SomeEvent;
void OnSomeEvent();
}
public class TestInterface<T> : ITestInterface<T>
{
public T Data { get ; set; }
public event EventHandler SomeEvent;
public void OnSomeEvent()
{
if(SomeEvent != null)
SomeEvent(Data, EventArgs.Empty);
}
}
You can use it like following.
MyClass<ArgumentA> myClass = new MyClass<ArgumentA>(
new TestInterface<ArgumentA>()
{
Data = new ArgumentA() { Name = "test" }
});
MyClass<ArgumentB> myClas2 = new MyClass<ArgumentB>(
new TestInterface<ArgumentB>()
{
Data = new ArgumentB() { Id = 10 }
});
myClas2.MyInterface.OnSomeEvent();
myClass.MyInterface.OnSomeEvent();
UPDATE
class Program
{
static void Main(string[] args)
{
ObservableCollection<MyClass> items = new ObservableCollection<MyClass>();
MyClass<ArgumentA> myClass = new MyClass<ArgumentA>(
new TestInterface<ArgumentA>()
{
Data = new ArgumentA() { Name = "test" }
});
MyClass<ArgumentB> myClas2 = new MyClass<ArgumentB>(
new TestInterface<ArgumentB>()
{
Data = new ArgumentB() { Id = 10 }
});
items.Add(myClass);
items.Add(myClas2);
myClas2.MyInterface.OnSomeEvent();
myClass.MyInterface.OnSomeEvent();
}
}
public class MyClass
{
public ITestInterface MyInterface { get; set; }
}
public class MyClass<T> : MyClass
{
public MyClass(ITestInterface<T> testInterfaceA)
{
this.MyInterface = testInterfaceA;
this.MyInterface.SomeEvent += MyInterface_SomeEvent;
}
private void MyInterface_SomeEvent(object sender, EventArgs e)
{
Console.WriteLine(sender);
}
}
public class ArgumentA
{
public string Name { get; set; }
}
public class ArgumentB
{
public int Id { get; set; }
}
public interface ITestInterface
{
event EventHandler SomeEvent;
void OnSomeEvent();
}
public interface ITestInterface<T> : ITestInterface
{
T Data { get; }
}
public class TestInterface<T> : ITestInterface<T>
{
public T Data { get; set; }
public event EventHandler SomeEvent;
public void OnSomeEvent()
{
if (SomeEvent != null)
SomeEvent(Data, EventArgs.Empty);
}
}
I have a recycler view with linearlayout and textviews inside cardview. I would like to implement onclick method for my item where in I would like to change the background color of the linear layout and invert the textcolor of the textviews. I have got it working but the main issue I am facing is that if I select 1st item then 1st as well as 7th items color is changed.
Trying to get this to work since two days. Any help would be appreciated.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models.ViewModels;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
namespace A.Droid.Adapters
{
public class DeliveryAdapter : RecyclerView.Adapter//, View.IOnClickListener
{
List<RequestViewModel> list;
public Context v;
public event EventHandler<int> phoneClick;
List<RequestViewModel> selectedList = new List<RequestViewModel>();
public DeliveryAdapter(List<RequestViewModel> records, Context v1)
{
list = records;
v = v1;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
// Inflate the CardView for the photo:
View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.activity_cardview_slinkyRecordList, parent, false);
DeliveryListViewHolder vh = new DeliveryListViewHolder(itemView, OnPhoneClick);
return vh;
}
// Fill in the contents of the photo card (invoked by the layout manager):
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
DeliveryListViewHolder viewHolder = holder as DeliveryListViewHolder;
viewHolder.PICNumber.Text = string.IsNullOrEmpty(list[position].PICNumber) ? "-" : list[position].PICNumber;
viewHolder.PropertyName.Text = string.IsNullOrEmpty(list[position].PropertyName) ? "-" : list[position].PropertyName;
viewHolder.ContactAddress.Text = string.IsNullOrEmpty(list[position].ContactAddress) ? "-" : list[position].ContactAddress;
viewHolder.ContactPerson.Text = string.IsNullOrEmpty(list[position].ContactPerson) ? "-" : list[position].ContactPerson;
viewHolder.ContactNumber.Text = string.IsNullOrEmpty(list[position].ContactNumber) ? "-" : list[position].ContactNumber;
viewHolder.NumberOfAliveSpecies.Text = list[position].NumberOfAliveStock + " Alive " + list[position].NameOfSpecies;
viewHolder.NumberOfDeadSpecies.Text = list[position].NumberOfDeadStock + " Dead " + list[position].NameOfSpecies;
viewHolder.DistanceOfTransporterToPIC.Text = list[position].DistanceOfTransporterFromPIC.ToString() + " KM"; //DeliveryList[position].DistanceOfTransporterFromPIC.ToString();
//viewHolder.MainLinearLayout.SetOnClickListener((new OnClickListener(viewHolder.MainLinearLayout,position)); // cardViewList.Add(viewHolder.cardView); //add all the cards to this list
//viewHolder.MainLinearLayout.SetOnClickListener(this); // cardViewList.Add(viewHolder.cardView); //add all the cards to this list
viewHolder.cardView.Click += delegate (object sender, EventArgs e)
{
if (selectedList.All(i => i.slinkyStockRequestId != list[position].slinkyStockRequestId))
{
selectedList.Add(list[position]);
viewHolder.MainLinearLayout.SetBackgroundColor(v.Resources.GetColor(Resource.Color.white));
viewHolder.PICNumber.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
viewHolder.PropertyName.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
viewHolder.ContactAddress.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
viewHolder.ContactPerson.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
viewHolder.ContactNumber.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
viewHolder.NumberOfAliveSpecies.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
viewHolder.NumberOfDeadSpecies.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
viewHolder.DistanceOfTransporterToPIC.SetTextColor(v.Resources.GetColor(Resource.Color.dark_blue));
viewHolder.mapIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.dark_blue), PorterDuff.Mode.SrcAtop);
viewHolder.contactIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.dark_blue), PorterDuff.Mode.SrcAtop);
viewHolder.phoneIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.dark_blue), PorterDuff.Mode.SrcAtop);
}
else
{
selectedList.Remove(list[position]);
viewHolder.MainLinearLayout.SetBackgroundColor(v.Resources.GetColor(Resource.Color.dark_blue));
viewHolder.PICNumber.SetTextColor(v.Resources.GetColor(Resource.Color.white));
viewHolder.PropertyName.SetTextColor(v.Resources.GetColor(Resource.Color.white));
viewHolder.ContactAddress.SetTextColor(v.Resources.GetColor(Resource.Color.white));
viewHolder.ContactPerson.SetTextColor(v.Resources.GetColor(Resource.Color.white));
viewHolder.ContactNumber.SetTextColor(v.Resources.GetColor(Resource.Color.white));
viewHolder.NumberOfAliveSpecies.SetTextColor(v.Resources.GetColor(Resource.Color.white));
viewHolder.NumberOfDeadSpecies.SetTextColor(v.Resources.GetColor(Resource.Color.white));
viewHolder.DistanceOfTransporterToPIC.SetTextColor(v.Resources.GetColor(Resource.Color.white));
viewHolder.mapIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.white), PorterDuff.Mode.SrcAtop);
viewHolder.contactIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.white), PorterDuff.Mode.SrcAtop);
viewHolder.phoneIcon.SetColorFilter(v.Resources.GetColor(Resource.Color.white), PorterDuff.Mode.SrcAtop);
}
};
animate(holder);
}
public List<RequestViewModel> GetSelectedItems()
{
return selectedList;
}
// Return the number of photos available in the photo album:
public override int ItemCount
{
get { return list.Count; }
}
// Raise an event when the phone-click takes place:
void OnPhoneClick(int position)
{
if (phoneClick != null)
{
phoneClick(this, position);
}
}
public class DeliveryListViewHolder : RecyclerView.ViewHolder
{
public ImageView MapTag { get; private set; }
public TextView PICNumber { get; private set; }
public TextView PropertyName { get; private set; }
public TextView ContactAddress { get; private set; }
public TextView ContactPerson { get; private set; }
public TextView ContactNumber { get; private set; }
public TextView NameOfSpecies { get; private set; }
public TextView NumberOfAliveSpecies { get; private set; }
public TextView NumberOfDeadSpecies { get; private set; }
public TextView DistanceOfTransporterToPIC { get; private set; }
public CardView cardView { get; private set; }
public LinearLayout MainLinearLayout { get; private set; }
public ImageView mapIcon { get; private set; }
public ImageView contactIcon { get; private set; }
public ImageView phoneIcon { get; private set; }
// Get references to the views defined in the CardView layout.
public DeliveryListViewHolder(View itemView, Action<int> phoneClickListener) : base(itemView)
{
MapTag = itemView.FindViewById<ImageView>(Resource.Id.mapIcon);
PICNumber = itemView.FindViewById<TextView>(Resource.Id.PICNumber);
PropertyName = itemView.FindViewById<TextView>(Resource.Id.nameOfProperty);
ContactPerson = itemView.FindViewById<TextView>(Resource.Id.contactPerson);
NumberOfAliveSpecies = itemView.FindViewById<TextView>(Resource.Id.noOfAliveSpecies);
NumberOfDeadSpecies = itemView.FindViewById<TextView>(Resource.Id.noOfDeadSpecies);
DistanceOfTransporterToPIC = itemView.FindViewById<TextView>(Resource.Id.areaInKM);
ContactAddress = itemView.FindViewById<TextView>(Resource.Id.address);
ContactNumber = itemView.FindViewById<TextView>(Resource.Id.mobileNo);
mapIcon = itemView.FindViewById<ImageView>(Resource.Id.mapIcon);
contactIcon = itemView.FindViewById<ImageView>(Resource.Id.contactIcon);
phoneIcon = itemView.FindViewById<ImageView>(Resource.Id.phoneIcon);
cardView = itemView.FindViewById<CardView>(Resource.Id.mainCardviewLayout);
MainLinearLayout = itemView.FindViewById<LinearLayout>(Resource.Id.MainLinearLayout);
ContactNumber.Click += (sender, e) => phoneClickListener(base.Position);
}
}
}
}
Edit
From Deep Patel input, here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models.ViewModels;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
namespace A.Droid.Adapters
{
public class DeliveryAdapter : RecyclerView.Adapter//, View.IOnClickListener
{
List<RequestViewModel> list;
public Context v;
public event EventHandler<int> phoneClick;
List<RequestViewModel> selectedList = new List<RequestViewModel>();
public DeliveryAdapter(List<RequestViewModel> records, Context v1)
{
list = records;
v = v1;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
// Inflate the CardView for the photo:
View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.activity_cardview_slinkyRecordList, parent, false);
DeliveryListViewHolder vh = new DeliveryListViewHolder(itemView, OnPhoneClick);
return vh;
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
DeliveryListViewHolder viewHolder = holder as DeliveryListViewHolder;
viewHolder.PICNumber.Text = string.IsNullOrEmpty(list[position].PICNumber) ? "-" : list[position].PICNumber;
viewHolder.PropertyName.Text = string.IsNullOrEmpty(list[position].PropertyName) ? "-" : list[position].PropertyName;
viewHolder.ContactAddress.Text = string.IsNullOrEmpty(list[position].ContactAddress) ? "-" : list[position].ContactAddress;
viewHolder.ContactPerson.Text = string.IsNullOrEmpty(list[position].ContactPerson) ? "-" : list[position].ContactPerson;
viewHolder.ContactNumber.Text = string.IsNullOrEmpty(list[position].ContactNumber) ? "-" : list[position].ContactNumber;
viewHolder.NumberOfAliveSpecies.Text = list[position].NumberOfAliveStock + " Alive " + list[position].NameOfSpecies;
viewHolder.NumberOfDeadSpecies.Text = list[position].NumberOfDeadStock + " Dead " + list[position].NameOfSpecies;
viewHolder.DistanceOfTransporterToPIC.Text = list[position].DistanceOfTransporterFromPIC.ToString() + " KM"; //DeliveryList[position].DistanceOfTransporterFromPIC.ToString();
//viewHolder.MainLinearLayout.SetOnClickListener((new OnClickListener(viewHolder.MainLinearLayout,position)); // cardViewList.Add(viewHolder.cardView); //add all the cards to this list
//viewHolder.MainLinearLayout.SetOnClickListener(this); // cardViewList.Add(viewHolder.cardView); //add all the cards to this list
viewHolder.cardView.Click += delegate (object sender, EventArgs e)
{
setSelectedPosition(position);
};
viewHolder.MainLinearLayout.SetBackgroundColor(list[position].isSelected() ? v.Resources.GetColor(Resource.Color.white) : v.Resources.GetColor(Resource.Color.dark_blue));
animate(holder);
}
private void setSelectedPosition(int position)
{
for (int i = 0; i < list.Count(); i++)
{
list[position].setSelected(i == position);
}
NotifyDataSetChanged();
}
public List<RequestViewModel> GetSelectedItems()
{
return selectedList;
}
// Return the number of photos available in the photo album:
public override int ItemCount
{
get { return list.Count; }
}
// Raise an event when the phone-click takes place:
void OnPhoneClick(int position)
{
if (phoneClick != null)
{
phoneClick(this, position);
}
}
public class DeliveryListViewHolder : RecyclerView.ViewHolder
{
public ImageView MapTag { get; private set; }
public TextView PICNumber { get; private set; }
public TextView PropertyName { get; private set; }
public TextView ContactAddress { get; private set; }
public TextView ContactPerson { get; private set; }
public TextView ContactNumber { get; private set; }
public TextView NameOfSpecies { get; private set; }
public TextView NumberOfAliveSpecies { get; private set; }
public TextView NumberOfDeadSpecies { get; private set; }
public TextView DistanceOfTransporterToPIC { get; private set; }
public CardView cardView { get; private set; }
public LinearLayout MainLinearLayout { get; private set; }
public ImageView mapIcon { get; private set; }
public ImageView contactIcon { get; private set; }
public ImageView phoneIcon { get; private set; }
// Get references to the views defined in the CardView layout.
public DeliveryListViewHolder(View itemView, Action<int> phoneClickListener) : base(itemView)
{
MapTag = itemView.FindViewById<ImageView>(Resource.Id.mapIcon);
PICNumber = itemView.FindViewById<TextView>(Resource.Id.PICNumber);
PropertyName = itemView.FindViewById<TextView>(Resource.Id.nameOfProperty);
ContactPerson = itemView.FindViewById<TextView>(Resource.Id.contactPerson);
NumberOfAliveSpecies = itemView.FindViewById<TextView>(Resource.Id.noOfAliveSpecies);
NumberOfDeadSpecies = itemView.FindViewById<TextView>(Resource.Id.noOfDeadSpecies);
DistanceOfTransporterToPIC = itemView.FindViewById<TextView>(Resource.Id.areaInKM);
ContactAddress = itemView.FindViewById<TextView>(Resource.Id.address);
ContactNumber = itemView.FindViewById<TextView>(Resource.Id.mobileNo);
mapIcon = itemView.FindViewById<ImageView>(Resource.Id.mapIcon);
contactIcon = itemView.FindViewById<ImageView>(Resource.Id.contactIcon);
phoneIcon = itemView.FindViewById<ImageView>(Resource.Id.phoneIcon);
cardView = itemView.FindViewById<CardView>(Resource.Id.mainCardviewLayout);
MainLinearLayout = itemView.FindViewById<LinearLayout>(Resource.Id.MainLinearLayout);
ContactNumber.Click += (sender, e) => phoneClickListener(base.Position);
}
}
}
}
in your model,
take a boolean variable, and create getter setter methods
private boolean isSelected = false
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
in your OnBindViewHolder
viewHolder.MainLinearLayout.SetBackgroundColor(list[position].isSelected()?v.Resources.GetColor(Resource.Color.white):v.Resources.GetColor(Resource.Color.black));
in your onClick event, // Code below is edited
Note: For Best practice setTag(position) with your position to your view that is getting clicked and use that tag value as a position.
int pos = (int) view.getTag();
list[pos].setSelected(!list[pos].isSelected());
notifyItemChanged(position);
You need to reset the background color of your view at OnBindViewHolder() because views are reused here
Add these lines on your OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
if (selectedList.All(i => i.slinkyStockRequestId != list[position].slinkyStockRequestId))
viewHolder.MainLinearLayout.SetBackgroundColor(v.Resources.GetColor(Resource.C olor.white));
else
viewHolder.MainLinearLayout.SetBackgroundColor(v.Resources.GetColor(Resource.Color.dark_blue));
I have implemented Single Pattern. Here is my code i am getting the an error when i call the Test.BuildData() function. Please help
public class WordDataItem
{
public string Word { get; set; }
public string Definition { get; set; }
public int WordGroupKey { get; set; }
}
public class WordDataGroup
{
public List<WordDataItem> listItem = new List<WordDataItem>();
public int GroupKey { get; set; }
}
public sealed class WordDataSource
{
private static WordDataSource _dataSoruce;
private List<WordDataGroup> listGroup = new List<WordDataGroup>();
public List<WordDataGroup> ListGroup
{
get { return listGroup; }
set { listGroup = value; }
}
private WordDataSource() { }
public static WordDataSource Instance
{
get
{
if (Instance == null)
{
_dataSoruce = new WordDataSource();
}
return _dataSoruce;
}
}
}
public static class Test
{
public static void BuildData()
{
WordDataSource.Instance.ListGroup.Add(new WordDataGroup() { GroupKey = 8, listItem = new List<WordDataItem>() { new WordDataItem() {Word = "Hello", Definition="Greetings", WordGroupKey = 8}} });
}
}
I get an error of stack over flow when i call the Test.BuildData() function.
Your Instance property is recursively calling into itself when you check if it is null.
Try this:
public static WordDataSource Instance
{
get
{
if (_dataSoruce == null)
{
_dataSoruce = new WordDataSource();
}
return _dataSoruce;
}
}
I have a background process that i want to regularly maintain the state of gps location. I am not clear on how to invoke a delegate on the main thread in the ui layer when the threaded method is in another class. Here is sample code. My form launches the thread on load:
public partial class MainScreen : Form
{
.
. // form stuff
.
private void MainScreen_Load(object sender, EventArgs e)
{
var gpsStatusManager = new GpsStatusManager();
Thread t = new Thread(gpsStatusManager.UpdateLocation);
t.IsBackground = true;
t.Start();
}
delegate void GpsDataParameterDelegate(GpsStatus value);
public void UpdateGpsStatus(GpsStatus value)
{
if (InvokeRequired)
{
// We're not in the UI thread, so we need to call BeginInvoke
BeginInvoke(new GpsDataParameterDelegate(UpdateGpsStatus), new object[] { value });
return;
}
// Must be on the UI thread if we've got this far
gpsStatus.SetGpsStatus(value);
}
}
I have a domain object class for the gps information:
public class GpsStatus
{
public void SetGpsStatus(GpsStatus gpsStatus)
{
Latitude = gpsStatus.Latitude;
Longitude = gpsStatus.Longitude;
CurrentDateTime = gpsStatus.CurrentDateTime;
NumberOfSatellites = gpsStatus.NumberOfSatellites;
TotalNumberSatellites = gpsStatus.TotalNumberSatellites;
}
public float Latitude { get; private set; }
public float Longitude { get; private set; }
public DateTime CurrentDateTime { get; private set; }
public int NumberOfSatellites { get; private set; }
public int TotalNumberSatellites { get; private set; }
}
Then, my manager class where i update status in the secondary thread:
public class GpsStatusManager
{
private GpsStatus _gpsStatus;
public void UpdateLocationx()
{
while (UpdateGpsData())
{
Thread.Sleep(2000);
}
}
private bool UpdateGpsData()
{
SError error;
SGpsPosition gpsPosition;
try
{
if (CApplicationAPI.GetActualGpsPosition(out error, out gpsPosition, true, 0) != 1)
return false;
}
catch (Exception)
{
return false;
}
var numberOfSatellites = gpsPosition.Satellites;
var totalSatellites = gpsPosition.satellitesInfo;
var datetime = gpsPosition.Time;
var lat = gpsPosition.Latitude;
var lon = gpsPosition.Longitude;
_gpsStatus.SetGpsStatus(lat, lon, datetime, numberOfSatellites, totalSatellites);
//How do I invoke the delegate to send the _gpsStatus data to my main thread?
return true;
}
}
Thanks for any assistance.
Here's one way to do it, just off the top of my head:
public class GpsStatusEventArgs : EventArgs
{
public GpsStatus Status { get; private set; }
public GpsStatusEventArgs(GpsStatus status)
{
Status = status;
}
}
public class GpsStatusManager
{
...
public event EventHandler<GpsStatusEventArgs> GpsStatusUpdated;
private void OnGpsStatusUpdated(GpsStatus gpsStatus)
{
EventHandler<GpsStatusEventArgs> temp = GpsStatusUpdated;
if (temp != null)
temp.Invoke(this, new GpsStatusEventArgs(gpsStatus));
}
}
public partial class MainScreen : Form
{
...
private void MainScreen_Load(object sender, EventArgs e)
{
var gpsStatusManager = new GpsStatusManager();
gpsStatusManager.GpsStatusUpdated += new EventHandler<GpsStatusEventArgs>(GpsStatusManager_GpsStatusUpdated);
...
}
private void GpsStatusManager_GpsStatusUpdated(object sender, GpsStatusEventArgs e)
{
UpdateGpsStatus(e.Status);
}
...
}
Then add this to the bottom of UpdateGpsData:
OnGpsStatusUpdated(_gpsStatus);
You should use the SynchronizationContext class.
In the UI thread (in any class), set a field (perhaps static) to SynchronizationContext.Current.
You can then call Send or Post on the saved instance to execute code on the UI thread.
Here is another approach using the ISynchronizeInvoke interface. This is the same pattern the System.Timers.Timer class uses to raise the Elapsed event.
public class GpsStatusManager
{
public ISynchronizeInvoke SynchronizingObject { get; set; }
public event EventHandler Update;
public void UpdateGpsData()
{
// Code omitted for brevity.
OnUpdate(_gpsStatus);
return true;
}
private OnUpdate(GpsStatus status)
{
if (SynchronizingObject != null && SynchronizingObject.IsInvokeRequired)
{
ThreadStart ts = () => { OnUpdate(status); };
SynchronizingObject.Invoke(ts, null);
}
else
{
if (Update != null)
{
Update(this, status);
}
}
}
public class UpdateEventArgs : EventArgs
{
public GpsStatus Status { get; set; }
}
}
I want an event notification system that should notify the doctor when the heartbeat of the patient is greater than 120.I do not know, How to design such system. Just I have implemented the wrong one. Help me in implementing the correct one.
static void Main()
{
Patient[] patList = { new Patient
{ PatientID = "1", HeartBeat = 100 },
new Patient { PatientID = "2", HeartBeat = 130 } };
List<Patient> plist = patList.ToList();
Console.ReadKey(true);
}
public class Doctor
{
public event PulseNotifier AbnormalPulseRaised;
public string Name
{
get;
set;
}
}
public class Patient
{
public event PulseNotifier AbnormalPulseRaised;
static Random rnd = new Random();
public Patient()
{
PulseNotifier += new PulseNotifier(OnAbnormalPulseRaised);
}
public string PatientID
{
get;
set;
}
public int HeartBeat
{
get;
set;
}
public void HeartBeatSimulation(List<Patient> patList)
{
foreach(Patient p in patList)
{
if (p.HeartBeat > 120)
{
if (AbnormalPulseRaised != null)
{
AbnormalPulseRaised(p);
}
}
}
}
public void OnAbnormalPulseRaised(Patient p)
{
Console.WriteLine("Patient Id :{0},Heart beat {1}",
p.PatientID, p.HeartBeat);
}
}
Apart from that, I want to have a common clarification.
What is the best way to remember the publisher and observer pattern?. Because I am quite confusing about where to implement publisher and where to implement
Well, for starters, I usually think it's an bad Idea to listen to the events of a class in the same class if you have access to it.
It's also a good idea to derive from EventArgs, which is recommended by MS.
The responsibility of raising the event should indeed be in the patient class itself, but here you raise only the event of the class where you call the HardBeatSimulation function itself instead of on the patient that actually has an abnormal pusle :)
static void Main(string[] args) {
Patient pat1 = new Patient(1, 120);
Patient pat2 = new Patient(3, 150); // this one can have a 150 bpm hartbeat :)
Doctor fancyDoctor = new Doctor();
fancyDoctor.AddPatient(pat1);
fancyDoctor.AddPatient(pat2);
Console.ReadKey(true);
}
public class Doctor {
List<Patient> _patients;
public event EventHandler Working;
public Doctor() {
_patients = new List<Patient>();
}
public void AddPatient(Patient p) {
_patients.Add(p);
p.AbnormalPulses += new EventHandler<AbnormalPulseEventArgs>(p_AbnormalPulses);
}
void p_AbnormalPulses(object sender, AbnormalPulseEventArgs e) {
OnWorking();
Console.WriteLine("Doctor: Oops, a patient has some strange pulse, giving some valium...");
}
protected virtual void OnWorking() {
if (Working != null) {
Working(this, EventArgs.Empty);
}
}
public void RemovePatient(Patient p) {
_patients.Remove(p);
p.AbnormalPulses -= new EventHandler<AbnormalPulseEventArgs>(p_AbnormalPulses);
}
}
public class Patient {
public event EventHandler<AbnormalPulseEventArgs> AbnormalPulses;
static Random rnd = new Random();
System.Threading.Timer _puseTmr;
int _hartBeat;
public int HartBeat {
get { return _hartBeat; }
set {
_hartBeat = value;
if (_hartBeat > MaxHartBeat) {
OnAbnormalPulses(_hartBeat);
}
}
}
protected virtual void OnAbnormalPulses(int _hartBeat) {
Console.WriteLine(string.Format("Abnormal pulsecount ({0}) for patient {1}", _hartBeat, PatientID));
if (AbnormalPulses != null) {
AbnormalPulses(this, new AbnormalPulseEventArgs(_hartBeat));
}
}
public Patient(int patientId, int maxHartBeat) {
PatientID = patientId;
MaxHartBeat = maxHartBeat;
_puseTmr = new System.Threading.Timer(_puseTmr_Tick);
_puseTmr.Change(0, 1000);
}
void _puseTmr_Tick(object state) {
HartBeat = rnd.Next(30, 230);
}
public int PatientID {
get;
set;
}
public int MaxHartBeat {
get;
set;
}
}
public class AbnormalPulseEventArgs : EventArgs {
public int Pulses { get; private set; }
public AbnormalPulseEventArgs(int pulses) {
Pulses = pulses;
}
}
The method OnAbnormalPulseRaised(Patient p) should be placed in Doctor class because doctor is the one being notified about event. The event property should by placed in Patient class because patients are raising the events:
public class Doctor
{
public Doctor()
{
// doctor initialization - iterate through all patients
foreach(patient in patList)
{
// for each patient register local method as event handler
// of the AbnormalPulseRaised event.
patient.AbnormalPulseRaised +=
new PulseNotifier(this.OnAbnormalPulseRaised);
}
}
public void OnAbnormalPulseRaised(Patient p)
{
Console.WriteLine("Patient Id :{0},Heart beat {1}",
p.PatientID, p.HeartBeat);
}
public string Name
{
get;
set;
}
}
public class Patient
{
public event PulseNotifier AbnormalPulseRaised;
static Random rnd = new Random();
public Patient()
{
}
public string PatientID
{
get;
set;
}
public int HeartBeat
{
get;
set;
}
public void HeartBeatSimulation(List<Patient> patList)
{
foreach(Patient p in patList)
{
if (p.HeartBeat > 120)
{
if (AbnormalPulseRaised != null)
{
AbnormalPulseRaised(p);
}
}
}
}
}
Publisher is the object with event property. Subscriber is the object with handler method.