Handling Asynchronous operations in Windows Phone7 - windows-phone-7

I am developing a Windows Phone7 application in which I have two App bar buttons both when clicked makes Asynchronous calls to Web and Callbacks will be performed upon the Web response.
Now my problem is, if I click on one button and as the Async operation is going on in the background ans meanwhile if I click on another button both callbacks are executing one after the other, which is not good for obvious reasons. Could any one help me on how to handle this???
First I thought to disable other buttons when 1 Async operation is going. But it doesnt give good feel for user. So what will be the best way to handle this problem??

You can use a Flag variable and check its value within the async call complete method. Based on your requirement you can choose to update or not update the view.

I was looking for the same answer.
I have found the solution, If you initialize an object inside a constructor like this, you will get multiple loops when you call a service function:
public partial class MainPage : PhoneApplicationPage
{
MovieServiceClient mov;
public MainPage()
{
mov = new MovieServiceClient(); //Don't do this.
InitializeComponent();
}
}
Avoid that.

Related

Writing event handlers in extensions in AX7

I am working in Dynamics AX7 form development. I have to write code in 'Clicked' method of a button, but there is already some 'Sys Layer' code in 'Clicked' method. I have to apply some conditions on it. But I don't want to do 'over-layering', i have to do it with Extensions, but if I write code in onClicked event, the problem is, my code runs before or after the sys-layer code, but i need to apply some conditions on that sys-layer code.
my question is, can we achieve this logic with extension event handlers ? I have already done it with over-layering, but I need to do it with extensions. So is it possible to do it with extensions ?
Code is added below.
void clicked()
{
super();
// My logic will be written here
if(result == true) //This is my code, based on above logic I applied this check
{
// start of sys layer code
remainSalesPhysical.realValue(0);
remainInventPhysical.realValue(0);
if (formCtrl)
{
formCtrl.cancelLine();
}
element.closeOk();
// end of sys layer code
} //this is my code
else //this is my code
{ //this is my code
error("Some error message"); //this is my code
} //this is my code
}
Yes and no. If it's just a button then the super() doesn't really do anything, so you can do a pre event handler.
If it's a menu item button, where the super() calls a class, then you would do a post event handler to the class and not the button, so that way your logic runs immediately after the super() call.
And in your class, you can do something like formRun = _xppPrePostArgs.getThis() and then if (formRun.name() == formStr(SalesTable)) or any number of things if the class has multiple entry points.
I have searched about it and what i concluded so far is that, we can't do it 100% without overlayering. We have Pre and Post events but these are unable to cater the above mentioned problem, May be in future we will have some more specific way of doing this, but for now we have three options.
Do overlayering as we did in AX 2012 (which is not recommended)
Do it With Delegates (even with delegates we're restricted to do some overlayering, but it is recommended way)
You can also hide that button and replace it with your own button, but it will work only for Form Controls, we can't do it for methods, as you can't avoid calling them.
I solved my problem using delegates.
Here is a Helpful link I found about it and it helped.
https://ievgensaxblog.wordpress.com/2016/05/01/ax-7-how-to-override-form-data-source-field-methods-without-overlaying/

Lazy loading list box, in a panorama page

I wanted to add a lazy loading list box(load content when swipe) in a panorama page in one of my windows phone 7 applications. I could however do it using a pivot page. I referred this link
But this is not working with panorama page. Can anyone please help me?
Okay, you're going to need to do one of two things: use the BCL Async package (basically adds async Tasks and such to WP7) or use a background worker. I highly suggest the BCL Async package, it's easy to get on Nuget.
Now, in your ViewModel (you are using MVVM, yes?) the property that it's bound to, let's call it Items should return an ObservableCollection of the item type you need. Now, here's where the magic happens. In the Getter of that property, return a new collection and use a task to fill it. Something like this:
public ObservableCollection<object> Items
{
get
{
ObservableCollection<object> retCollection = new ObservableCollection<object>();
FillCollection(retCollection);
return retCollection;
}
}
public async void FillCollection(ObservableCollection<object> collectionToFill)
{
Task.Factory.StartNew(() =>
{
foreach(object objectToAdd in collectionImGettingThisDataFrom)
{
// We do this using the Dispatcher to
// be sure to pop back into the UI thread.
Deployment.Current.Dispatcher.BeginInvoke(
() => collectionToFill.Add(objectToAdd));
}
}
}
Because FillCollection is async, the Get method will continue and return the current collection. On another thread, the Task that's created will find the data to add, then push it to the UI thread to add it into the collection. This way, you'll be able to lazy load the data only when you ask for it, without completely blocking your UI thread. If it turns out that it's still making your UI slow, you can add the line:
await TaskEx.Delay(25); // Some time in milliseconds. Too much and it will
// take a long time to load the list,
// too little and it will still bog down your UI.
At the end of the foreach block, but not in the Dispatcher invokation.
Happy coding!
Have you looked at the Telerik Rad Controls yet? They have all types of pull to refresh controls. I used them in a recent app I released called "Rad Libs". You can see the controls here http://www.telerik.com/products/windows-phone.aspx and you can also download an app that demos all of their controls. (Disclaimer: I have no affiliation with telerik. I stand to gain nothing from promoting them here)

What controls "loading" feedback in MVP?

I see two basic ways to implement "loading" feedback (e.g. swirly loading icon):
In the presenter:
void displayData()
{
display.startShowingLoadingIcon();
startLoadingData();
}
void onDataLoaded()
{
display.stopShowingLoadingIconAndDisplayData(data);
}
In the display:
void showData()
{
startShowingLoadingIcon();
presenter.getData(callback);
}
Callback callback(data)
{
stopShowingLoadingIconAndDisplayData(data);
}
I like the second solution because it seems like loading feedback is clearly a user interface decision. I don't want my presenter to have to know about any kind of user interaction. Also, the first solution allows the display some flexibility for the case that the user cancels a request, something else comes up, etc. On the other hand, the first solution is simpler to implement, and doesn't require the display to know about the presenter.
What do you do?
I think it belongs in the view because:
1: The reason you'd take things out of the view is so you can more easily test it. A simple start/ stop showing loading doesn't seem like a lot of logic worth writing tests for.
2: This feature is so specific to the view, that it feels the presenter shouldn't even have to know about it.
3: Since the view is responsible for knowing how to render itself, it makes sense that it should know how to display a loading icon when needed.

MVVM Light: Where do I instantiate a class from a model and how do I update/access it?

Right now, I'm working on my first WP7 app and have run into some questions, which I haven't been able to answer despite reading what I could find online. Please consider an app that has a main page, a parameters page and a results page. In the parameters page, the user can enter or update numbers in various textboxes. Hitting the back button takes the user back to the main page, where there is a button called "Calculate". Hitting that button should take the data, perform a calculation with it and take the user to the results page presenting a grid with the results.
In a file called Calculator.cs I have a class called Calculator inside a folder called Models. I also have my MainViewModel.cs, ParametersViewModel.cs, and ResultsViewModel.cs files inside the ViewModels folder and the corresponding MainPage.xaml, along with Parameters.xaml and Results.xaml inside a folder called Views. I'm assuming that all the data will be manipulated within the instance of the Calculator class and then a results set will be returned and directed to Results.xaml. I'm just at a loss as to where to instantiate the Calculator class, pass it data, then retrieve the results. I'm also somewhat puzzled how I will trigger the automatic navigation to the Results page when the calculation is done.
Any help with this would be greatly appreciated.
UPDATE: Passing a complex object to a page while navigating in a WP7 Silverlight application has some more info on the same subject. I can go into App.xaml.cs and add something like this:
public class Foobar
{
public string barfoo = "hah!";
}
public static Foobar myfoob = new Foobar();
Then access it from a ViewModel page, e.g. AboutViewModel.cs, like this:
public AboutViewModel()
{
string goo = App.myfoob.barfoo;
}
But at this point I'm still uncertain what unforseen effects that might have. I'm going to tackle serialization/tombstoning at this point to see what happens with either this approach or by using the same DataContext across pages. Otherwise, one of the posters in the link above mentioned serializing the params and passing them between pages. My concern there would be whether or not there is a character limit as with HTTP GET. Seems there is: URI Limits in Silverlight
There are of course lots of possible designs - and lots of them are correct in different ways!
Here's one I might use:
The Calculate button press should trigger the Navigate to the Results page
On navigate to, the Results page should show some animation (maybe just a progress bar)
On navigate to, the Results page should create a new ResultsViewModel, passing in the MainViewModel as parameters
the constructor (or some init method) of the ResultsViewModel should spark up a thread to do the calculation
when this calculation is complete, then the relevant properties of the ResultsViewModel will get set
at which point the databinding on the Results page will clear the animation and show the results
Other solutions are definitely available - will be interested to read what other people suggest and prefer.
As an aside, one thing to watch out for on your Results page is tombstoning - could be an interesting challenge!

Observe event created through an instance of a Winforms UserControl

In my winforms app, I have a UserControl that contains a DataGridView. I instantiate and load this UserControl when needed into a panel in my Main Form (frmMain). My problem is figuring out how to resond to or listen for events raised in my UC's DataGridView. For example, I want to handle the CellDoubleClick event of the DataGridView in my Main Form rather than through the UC.
Is this possible? I had thought of updating a property when the cell in the grid is double-clicked, and then let my Main form do whatever when that property changes - therefore I thought of using INotifyPropertyChanged. Im not heavily clued up on how to use it in m scenario however, and would deeply appreciate some help in this regard, or if anyone can suggest an alternate solution.
Much thanx!
Your user control must encapsulate some logic, so if you want to handle event of the DataGridView that is in your control the way you've described, you probably missing something in idea of user controls and encapsulation. Technically here two ways to do this:
Make a public property in your user control of type DataGridView.
Make an event wrapper. You will need to create an event in your user control that is raised when DataGridView CellDoubleClick (or any) is rased and in your calling code you will handle this event wrapper.
The second approach is more logical, cos internal logic of your control is incapsulated and you can provide end-user of you component with more logical and meaningful event then CellDoubleClidk or else.
thank u 4 your reply. Sorry for not responding earlier. I did manage to sort this issue out by creating a public event in my UC:
public event DataGridViewCellEventHandler GridRowDoubleClick {
add { dgvTasks.CellDoubleClick += value; }
remove { dgvTasks.CellDoubleClick -= value; }
}
and in my main form, after I instantiate and load the UC
_ucTask.GridRowDoubleClick += new DataGridViewCellEventHandler(TasksGrid_CellDoubleClick);
with the following attached event:
private void TasksGrid_CellDoubleClick( object sender, DataGridViewCellEventArgs e ) {
// do work here!
}
This does work, although I don't know if any of u experts out there foresee a problem with this approach.

Resources