In my application, I will need to check whether an ObservableCollection is now in CollectionChanged Event, and if yes, I will refrain from modifying the collection.
The reason I need to do this is because sometimes my application will fire add or remove event in rapid succession, that even before the previous CollectionChanged Event is finished, the next modifying event is already coming. If this happens, then I will get a
Cannot change ObservableCollection during a CollectionChanged event
System: System.InvalidOperationException
Cannot change ObservableCollection during a CollectionChanged event.
at System.Collections.ObjectModel.ObservableCollection`1.CheckReentrancy()
at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)
at System.Collections.ObjectModel.Collection`1.System.Collections.IList.Add(Object value)
at Syncfusion.Data.CollectionViewAdv.CommitNew()
at Syncfusion.UI.Xaml.Grid.GridAddNewRowController.CommitAddNew(Boolean changeState)
at Syncfusion.UI.Xaml.Grid.GridSelectionController.ProcessSourceCollectionChanged(NotifyCollectionChangedEventArgs e, CollectionChangedReason reason)
at Syncfusion.UI.Xaml.Grid.GridModel.OnSourceCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args)
at Syncfusion.Data.CollectionViewAdv.RaiseSourceCollectionChangedEvent(NotifyCollectionChangedEventArgs args)
at Syncfusion.Data.CollectionViewAdv.SourceNotifyCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)
In such a situation,it's alright for my application to reject the subsequent modifying event to avoid the crash.
How to actually tell that the previous CollectionChanged event is still executing?
I've finally managed to reproduce the problem in a small sample-- it turns out that this is a bug in the third party component ( Syncfusion).
I've contacted the support for help
Related
i want to update the text field value when i was changed.for this i use action listener because every updation time one trigger was fired in my code so,that's the reason i used action listener instead of data changed listener.for data changed listener on key pressed the listener is called updation done but my requirement is after completion of entering the data on text field only the listener is called.but it is not calling properly.
please find the below code,
agencyWorker.addActionListener(createAgencyActionListener(agencyWorker,eventPostchedules.getSerialId()));
private ActionListener createAgencyActionListener(final TextField searchField, String serialId){
return new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
String agencyText = searchField.getText();
searchField.setPreferredW(110);
roasterDao = RoasterDao.getInstance();
roasterDao.updateEventPostScheduleAgency(agencyText, serialId, "supervisor");
}
};
}
thanks in advance.
I just tested this on my iPad and action listener was invoked as expected. Check that you don't have a different error that is causing this e.g. networking error related to https change in iOS.
FYI on a side note, it's really bad to write this:
searchField.setPreferredW(110);
You are limiting the size of the field based in pixels which is rarely the right thing to do. The method is deprecated for a reason...
In WPF, there is a Closing event on <Window... tag, where we can hook up some dispose code in MainWindow_OnClosing.
But there is no such event in UWP app. The closing I guess is Unloaded, not even Unloading is there.
I just placed my disposing code there but still feel concerned. Is Unloaded event supposed for this type of work? Is there something I need to take note?
According to the MSDN, the Window class has a Closed event.
I'm mentioning this as you posted the Closing event of a window component, but keep in mind the remark of the event:
The Closed event occurs when a Window closes. However, Windows Store
apps typically use a single Window instance, and do not open and close
additional Window instances.
Now, when using the Frame navigation system of the main window with Pages, I advise you to use the OnNavigatedTo and OnNavigatedFrom events to manipulate all initialisation and dispose functionality of the class.
You may want to pay attention to the OnNavigationFrom as it is invoked immediately before the Page is unloaded and is no longer the current source of a parent Frame.
A really simple example:
Windows.ApplicationModel.Resources.ResourceLoader loader;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
loader = new Windows.ApplicationModel.Resources.ResourceLoader();
var navigationPageContentFormat = loader.GetString("NavigationPageContent");
var navigationPageContentWhenEmpty = loader.GetString("NavigationPageContentWhenEmpty");
this.ParameterTextBlock.Text = String.Format(navigationPageContentFormat, e.Parameter?.ToString() ?? navigationPageContentWhenEmpty);
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
loader = null;
}
I tried to override an exisiting method but after i published I get this error.
Attempt by method 'Wrapper.PX.Objects.AR.Cst_ARPaymentEntry.ARPayment_RowSelectedGeneratedWrapper(PX.Objects.AR.ARPaymentEntry, PX.Data.PXCache, PX.Data.PXRowSelectedEventArgs)' to access method 'PX.Objects.AR.ARPaymentEntry_Extension.ARPayment_RowSelected(PX.Data.PXCache, PX.Data.PXRowSelectedEventArgs)' failed.
when I tried to remove the PXOverride attribute no error occured. I'm using 5.10.072 version.
[PXOverride]
protected void ARPayment_RowSelected(PXCache cache, PXRowSelectedEventArgs e)
{
}
The problem is that you try to override an event handler - not a common virtual method of the BLC. To do this one has to use a different approach. Namely, you need to declare the event handler without the PXOverride attribute, but with an additional argument of type PXRowSelected and then either call it or not based on your internal logic. Here is an example of such a declaration:
protected void ARPayment_RowSelected(PXCache cache, PXRowSelectedEventArgs e, PXRowSelected invokeBaseHandler)
{
/* your custom event handling logic here */
if(/* your custom condition may go here */)
invokeBaseHandler(cache, e);
/* some more of your logic here if needed */
}
Note that if you simply want your handler be executed along with the base one, you don't need the additional argument - simply declare the handler with your code and it will be called after the original handlers.
You may find much more information and explanatiions on this topic in the help article located under Help > Customization > Examples of Functional Customization > Adding or Altering BLC Event Handler in any instance of Acumatica.
How can you make a background web request and then update the UI, but have all the code that does the web requesting/parsing in a separate class so you can use it in multiple places? I thought I could use the classes methods as event handlers for a BackgroundWorker class, like
APIHelper mHelper = new APIHelper("http://example.com?foo=bar");
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork +=new DoWorkEventHandler(mHelper.GetResponse);
bw.RunWorkerCompleted +=new RunWorkerCompletedEventHandler(mHelper.HandleResponse);
bw.RunWorkerAsync();
where APIHelper has the method
public void GetResponse(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = (BackgroundWorker) sender;
WebRequest request = HttpWebRequest.Create(this.URL);
IAsyncResult result = (IAsyncResult)
request.BeginGetResponse(ResponseCallback, request);
}
but then I don't know how to access the worker thread from ResponseCallback and, anyway, HandleResponse gets called first (obviously). (I tried putting in result.AsyncWaitHandle.WaitOne(); but I get a NotSupportedException error.) Yet I can't work out how to make the web request call synchronously. I'm clearly trying to go about this the wrong way, but I have no idea what the right way is.
ETA:
My aim is to be able to go:
user clicks (a) button(s) (on various pages)
a "working" message is displayed on the UI thread (and then input is blocked)
in a background thread my APIHelper class makes the relevant API call, gets the response, and passes it back to the UI thread; I only seem to be able to do this by starting another thread and waiting for that to return, because there's no synchronous web requests
the UI thread updates with the returned message (and input continues as before)
I can do the first two bits, and if I have the response, I can do the last bits, but I can't work out how to do the middle bit. Hopefully that made it clearer!
It took me several tried before I found there is a Dispatcher.
During the BackgroundWorker's dowork and complete methods you can call:
this.Dispatcher.BeginInvoke(() =>
{
// UPDATE UI BITS
});
I think the Dispatcher is only available in the view. So I'm not sure if the methods can exist outside of the xaml.cs
Put whatever you want to update in your UI; when updating an ObservableCollection you must do the update of you items in the Dispatcher.BeginInvoke too
This link might be a good read too:
http://www.windowsphonegeek.com/articles/All-about-Splash-Screens-in-WP7-ndash-Creating-animated-Splash-Screen
Update to assist notes
This is just a rough idea mind you...
bw.DoWork +=new DoWorkEventHandler(DoWork);
bw.RunWorkerCompleted +=new RunWorkerCompletedEventHandler(Complete)
// At least I think the EA is DoWork....
public void DoWork(object sender, DoWorkEventArgs e)
{
mHelper.GetResponse();
this.Dispatcher.BeginInvoke(() =>
{
UIObject.Visibility Collapse.
});
// Wait and do work with response.
});
}
public void Complete(object sender, RunWorkerCompleteEventArgs e)
{
this.Dispatcher.BeginInvoke(() =>
{
UIObject.Visible ....
});
}
I'd put all this logic in a viewmodel that the viewmodel of each page inherits from.
Have the pages bind to properties on the viewmodel (such as ShowLoading, etc.) which the model updates appropriately. i.e. before making the webrequest and in the callback.
As you won't be running the viewmodel code in the UI thread you also wouldn't need to run in a separate BackgroundWorker and you'll be able to access the properties of the viewmodel without issue.
It might be useful if you use a helper class that I have developed for WebDownload purposes during WP7 development.
I'm using it in 2-3 WP7 apps and no problem so far. Give it a go to see if it helps. You can get the class from the my blog linked bellow:
http://www.manorey.net/mohblog/?p=17#content
[NOTE] When working with this class you don't need to run anything in a background worker or new thread; it handles it all asynchronously.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Difference between events and delegates and its respective applications
Hi,
I am kind of new here,
I wanted to know what is the difference between Delegates & Event usages?
How can I choose which one to use?
Thanks!
If you are writing a class that exposes events, there is a subtle difference between using the event keyword or not.
For example the following code is valid and will allow clients to subscribe to either ExposedAsEvent or ExposedAsDelegate:
public delegate void ExposedEventHandler(object sender, EventArgs e);
public MyClass{
public event ExposedEventHandler ExposedAsEvent;
public ExposedEventHandler ExposedAsDelegate;
}
The only difference is that using the event modifier restricts what clients can do with the delegate. In this case clients cannot invoke the delegate directly or set it to null.
Remove the event prefix and the delegate can still be used similar to an event, however it can also be possibly 'misued' by clients.
The event modifier is really just a way of further clarifying the intent to clients of your class and limiting access (encapsulation).
Delegates are used for events in C#. A delegate is a signature for a method that can be called by an Event. An example would be:
public delegate void MessageHandler(string message);
an event that uses that delegate would be:
public event MessageHandler NewMessage;
to call the event:
NewMessage("Hello events");
which would call a method using the delegate above such as:
public void Client_NewMessage(string message)
{
MyTextBox.Text += message;
}
To subscribe to the event (using a local method implementing the delegate):
Client cl = new Client();
cl.NewMessage += new MessageHandler(Client_NewMessage);
From MSDN:
Event:
An event is a message sent by an
object to signal the occurrence of an
action
Delegate:
A delegate is a class that can hold a
reference to a method
In respect to event handling, the question is not really whether to use the one or the other. The class defines an event which is executed when some action takes place, and the consumer assigns a method which matches the delegate definition of the event.