Value not updating in UI - windows-phone-7

I am using this code to map a database to my Windows Phone 7 application.
In particular when I add a new Card item (in a Phone Page different from the home page) and when I get back to the home page the balance value of my Credit Card list does not update (Which is bound to an observable collection of type Card). I think I have added all the necessary NotifyPropertyChanged.
Am I missing something:
Code: Here

This is kind of a shot in the dark, but I don't see PropertyChanged being set anywhere... if PropertyChanged does not get set, then it will equal null and none of the NotifyPropertyChanged calls will actually call PropertyChanged().

I had a problem once that wouldnt update my URI, just try to actualize when you can, not sure how to go about your particular problem but I had a problem that when I changed teams in my app, the main page wouldnt update the statistics when you returned to it, so I just added a method that retrieves the value once more, I use nested classes with setters and getters so its all handled for me
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
method();
base.OnNavigatedTo(e);
}

Related

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)

Handling Tombstoning and Back key properly for performance reasons?

Is there a best practice for handling tombstoning and back key properly?
As it is stated in the MSDN docu you should save transient data in the OnNavigatedFrom method. Ok, so the code for saving states when tombstoning is clear.
But now if you press the back key the OnNavigatedFrom method is also called and if you don't add extra checks, you will first save states into the dictionary and shortly after that the page will be destroyed. And so will the PhoneApplicationPage.State dictionary. So the saving code is completely wasted CPU, disk and battery time.
This is what I've done to prevent it:
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
// when navigating back
if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
{
backKeyPressed = true;
}
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
if (backKeyPressed)
{
// Don't save states on back key!
backKeyPressed = false; // set it anyway
return;
}
// Tombstoning
// save objects
this.SaveState("text", someText);
...
}
As a reminder: OnNavigatingFrom will only be called when navigating away/back from the page but not when app gets tombstoned.
Side note: Shown code covers only pages that can only navigate back. Thats why I added backKeypressed to OnNavigatingFrom. You need extra checks if the page can navigate to another page.
Is there a better way to do this for every page you create?
Now do I really have to add the backKeyPressed variable and check on every page I create?
Shouldn't the framework provide something for us developer so we don't have to worry much about this?
What are your thoughts about this?
EDIT:
Updated question the make it clearer.
your approach of checking the direcetion of navigation in OnNavigatingFrom is indeed the recommended practice in order to avoid the unneccessary performance hit of saving state just before the page gets removed from the backstack.
There is one clarification I want to add to your sample code: You should check the 'NavigationMode' property in the 'NavigationCancelEventArgs' to determine whether it's a forward or backward navigation.
Then only save the state in OnNavigatedFrom if it was a forward navigation, as your sample shows. This will help improve your performance when the user navigates backwards.
Everything you ever needed to know about tombstoning is covered in Jeff Prorise's 4-part Real-World Tombstoning in Silverlight for Windows Phone 7 blog post series. You may want to pay particular attention to part 2 where Jeff talks about clearing up state when the application quits.

Passing data from page to page

I'm looking for the best practice on how to pass data from page to page.
In Page A I have a button that fires off Page B.
On Page B I have 6 textboxes that allow the user to enter information.
When the user is done, the click on a button that brings them back to Page A.
I want to pass that data back to Page A.
I've seen suggestions to:
build XML documents and save to Isolated Storage
use the App class to store information in properties
pass it like a query string
I'm looking for the Best practice. Is there one that Microsoft recommends or one that is generally accepted as the best way?
Thanks
PhoneApplicationService.Current.State["yourparam"] = param
NavigationService.Navigate(new Uri("/view/Page.xaml", UriKind.Relative));
then in other page simply
var k = PhoneApplicationService.Current.State["yourparam"];
Personally I'd store the values entered on Page B in a model(object) that is also accessible to Page A.
Depending on how you're navigating to Page A the second time, one or more of the following may be usful to help understand passing values between pages:
How to pass the image value in one xaml page to another xaml page in windows phone 7?
Passing a complex object to a page while navigating in a WP7 Silverlight application
How to pass an object from a xaml page to another?
How to pass a value between Silverlight pages for WP7?
How do I navigate from one xaml page to another, and pass values?
One thing you can consider is to use MVC: let your App be the controller, store all data in the model, and the pages are just views that contains pure UI logic. In this case your pages are painters and you pass your model object around. This gives nice isolation of business logic and the UI so that you can rev them easily.
BTW, Silverlight and XAML are great tools for MVC so it's a natural match.
There's a couple of things at play here. First of all, if/when the user uses the Back button to return to page A instead of your button, is the information in the text boxes exchanged or not (is Back = Cancel, or is Back = OK?)
That said, if you're using NavigationService.GoBack (which you should be instead of NavigationService.Navigate, because if you use the Navigate call, repeated hits of the back key will cause all kinds of bad UX for your users), then QueryStrings are not an option. Because pages really have no way to reference each other in the WP7 Silverlight nav system, you need to use a 3rd party to hold your data. For that, you can turn to (a) Isolated Storage (slow & heavy, but fail-safe), (b) Use the PhoneApplicationService.State dictionary, or (c) use Global properties of some kind, either hung off of the application object, or using Statics/Singletons...
Remember to watch for Tombstoning behavior when you do this - your page will process the OnNavigatedTo method when (a) you navigate into it in your application (b) you navigate back to it when you complete your work on Page B, or (c) you tombstone your app from that page and return to your application using the Back key.
Sorry I didn't give a more direct answer there - a lot depends on your specific circumstances. In the most general case, I'd strongly consider using the App State Dictionary on the PhoneApplicationService...it is lightweight, easy to use, and survives tombstoning. Just be sure that your keys are as unique as they need to be.
If you create a new Windows Phone project and use the Windows Phone Databound Template you will have most of the work done for you.
What you will want to do is set up the ViewModel to contain all the data for your app. You can serialize and deserialize this data using IsolatedStorage so that it's saved across application sessions and when Tombstoning.
In the template you will notice MailViewModel and ItemViewModel. MainViewModel stores all the data your application needs including an ObservableCollection of ItemViewModel, and ItemViewModel represents the individual data type for your application.
On the DetailsPage.xaml page you'll want to DataBind each textbox to the App.MainViewModel Items. Set the binding to TwoWay if you want the ViewModel to get updated as soon as the user manipulates the data on DetailsPage.xaml. You can optionally set the Binding to OneWay and then have an OK button that writes the changes back to the ViewModel and saves to IsolatedStorage.
Here is an example of what a Binding looks like:
<TextBlock x:Name="ListTitle" Text="{Binding LineOne}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
In this case LineOne is a property in ItemViewModel and the page gets this data from the query string when the user selects an item from the MainPage.xaml. The DataContext for the page determs where the databound information comes from.
Here is the snippet where the MainPage passes the selected item from the ViewModel to the DetailsPage.
// Handle selection changed on ListBox
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// If selected index is -1 (no selection) do nothing
if (MainListBox.SelectedIndex == -1)
return;
// Navigate to the new page
NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));
// Reset selected index to -1 (no selection)
MainListBox.SelectedIndex = -1;
}
Here is how the DetailsPage gets the selected item.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string selectedIndex = "";
if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
{
int index = int.Parse(selectedIndex);
DataContext = App.ViewModel.Items[index];
}
}
Play around with the default template above and ask any additional questions.
The beauty of databinding and the ObservableCollection is that you can just update the data and the UX will reflect those changes immediatley. This is because any changes to the data fires off an event:
public string LineOne
{
get
{
return _lineOne;
}
set
{
if (value != _lineOne)
{
_lineOne = value;
NotifyPropertyChanged("LineOne");
}
}
}
NotifyPropertyChanged() that broadcasts this information to the View.
You can also keep it simple and use PhoneApplicationService.Current.State which is basically a hashtable. You will need to implement your own marshalling to and from isolated storage if you want anything to outlive the app.
Omar's suggestion to use the Windows Phone Databound Template is probably the best idea on this page. It amounts to the same as my suggestion but you will get a better result (more maintainable code) at the cost of a longer steeper learning curve.
I suggest you do it my way and then do it again Omar's way.
as i implemented like this.. Whether its correct or not i dont know..
When u click news list page it should open the news detail page.
I want to pass the selected news item contents from news List-Page to news-details Page.
the News list page contains following method.
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
NewsDetailsPage newsDetailPage = (e.Content as NewsDetailsPage);
if (newsDetailPage != null)
newsDetailPage.SelectedNewsItem = SelectedNewsItem; //Contains the news details
base.OnNavigatedFrom(e);
}
In the News details Page. U can access that(SelectedNewsItem) object.
This may or may not be correct.
One option is to use Application.Resources:
Store data:
Application.Current.Resources.Add("NavigationParam", customers);
NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));
Retrieve data:
var customers = (List<Customer>) Application.Current.Resources["NavigationParam"];
Here's a blog post with describes this in more detail: http://mikaelkoskinen.net/windows-phone-pass-data-between-pages-application-resources/ (author: me)

Display a success message to user in a SharePoint Document Library after validation in ItemAdding Event

When validating a document in the ItemAdding event there are many ways to display errors to the user, but there doesn't seem to be a way to display successes to user, like a validation was successful. An info message to the user at the top of document library would be great.
public class MyItemEventReceiver : SPItemEventReceiver {
public MyItemEventReceiver() {}
public override void ItemAdding(SPItemEventProperties properties) {
// Do some validation
// If successful display message to user - can't seem to do
// If unsuccessful cancel and go to the error page - easy
}
}
}
In the event handler you have a Property called Cancel when set to true it will redirect you to the Error Page. Whereas when you dont distrub it, it will redirect you to the Metadata page of the document, i.e it will as you for the Name and Title of the document. Being said that out of the Box it is not possible to archive what you want to do. One alternate approach I would suggest is that once the validation is successful, set a Flag in the Session / Property Bag of the List with the Document GUID as the Key and in the Final Landing page deploy a Custom Web Part that will check for the this GUID and if there is a Value then It will display the Message.
Umm... edit List's AllItems.aspx or edit your masterpage, add Literal control to it. At ItemAdded event just refer to that control and set it's value.
Maybe it turns out you even need code-behind for that masterpage. Refer here.

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