Tombstoning listbox in WP7 application - windows-phone-7

I have a listbox in my WP7 app, and I would like to keep/save listbox inserted items when I reload the application. I tried this, but it not work:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
this.SaveState(e);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.RestoreState();
}
What should I do??

In tombstoning you should look at storing application data in the PhoneApplicationService.State or IsolatedStorage.ApplicationSettings (depending on the size of the data).
The App.xaml.cs file already contains four methods to help you know when you application is 'Launching' (raised when the user initially launches the application), 'Activated' (raised when the application is dormant or tombstoned and the user navigates back to the application), 'Deactivated' (whenever the user navigates forward away from the application. Although applications are typically made dormant after they are deactivated, there is no way to know at this point whether the application will be tombstoned or terminated after this event) and 'Closing' (when the user uses the Back button to navigate backwards past the first page of your application. After this event, your application is terminated)
For more info check out : How to: Preserve and Restore Application State for Windows Phone

Related

How to hide TimePicker and DatePicker on lifecycle events in Xamarin Forms

I'm running into an issue on android where our TimePicker and DatePicker stay visible when we navigate OnPause(). We need to redirect our users back to the login screen when they background the application, but if the TimePicker or DatePicker is active when they do this it stays on the screen. It appears above the login screen and pressing cancel or ok crashes the app.
We are hooking into the native android lifecycle events (not just using Xamarins built in hooks) and we redirect OnResume(). I've tested this in a barebones app though and it still happens OnPause().
Here is our TimePicker causing us the issue:
<TimePicker x:Name="VitalTimePicker" HorizontalOptions="Fill" VerticalOptions="Fill" IsVisible="false" PropertyChanged="OnTimePickerPropertyChanged"/>
And here is an example of changing screens on a lifecycle event:
protected override void OnSleep()
{
App.Current.MainPage = new NavigationPage(new NotesPage());
}
Any ideas? I was thinking of clearing the Pickers but I can't seem to find how to do that
Edit
Just to add a little more context
The Application class (app.xaml.cs) has lifecycle hooks that we use to catch when our users background the app. In here we call MainPage = new NavigationPage(new LoginPage()); which takes the app back to the login page.
I've added
protected override void OnDisappearing()
{
VitalTimePicker.Unfocus();
VitalDatePicker.Unfocus();
}
to the view i'm working from and it seems to be called when we background the application, but for some reason the TimePicker is staying on the screen when our login page pops up again.
You can programmatically close DatePickers and TimePickers using the method Unfocus(). I'd recommend closing them before you call the next page, as I don't know if they will be able to be referenced and closed after the other screen has been initialized.
Create a method that calls VitalTimePicker.Unfocus() and the same for any other picker you have, and call this method before changing to login screen and you should be good to go.

How to Close/Exit current instance of Xamarin.Forms app while loading a new one

We are facing a strange issue. Not sure if it is design flaw of the existing application. Your help/suggestion is appreciated.
We have a Xamarin.forms app. Targeted both for iOS and Android.
Problem is coming mainly in Android app.
Application flow:
once we logout from the application, app opens an logout activity and delete user info and other data.
opens a new activity for login which contains client SSO implementation
on successful login, app is setting user info and fetch data from web service.
then it calls LoadApplication method so that flow comes back in main application
Now if user perform logout/login several times, its opening a new application instance by calling LoadApplication method and then displaying home screen
So when user is tapping back button in home page, app is not closing and displays previous instance of same application.
User need to press back button several times (depending how many time user perform logout-login).
Is there any goodway to stop this?
Can we close current instance of the application before LoadApplication being called?
Stuck for a long time.
I'd try to avoid to call LoadApplication more then once. You should control the navigation stack.
Given you are on the LogoutPage
remove all views via PopToRootAsync
show the LoginPage
await Navigation.PopToRootAsync(false);
await Navigation.PushAsync(new LoginPage(), true);
This blog post may be worth a reading: https://jfarrell.net/2015/01/22/understanding-xamarin-forms-navigation/
Kindly try this.
https://stackoverflow.com/a/36885388/1941942
[Activity (NoHistory = true)]
public class LoginActivity : Activity { }
The saving instance error has gone a while after I implement it on MainActivity.

How can i use NavigationServices in Application_Activated windows phone 7?

im developin an app for wp7 that it holds pictures and notes with password login. But when app running if user press windows button app is running at background and if user press back button it resumes without asking password again.
i tried to Navigate when app activated but i couldnt manage it in Application_Activated method. is there a way to do that? Or could you advice me sth else that solve my problem.
ty.
here is my code im using to navigate,
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
I got around this issue by using UserControls on the MainPage, showing one if the user had not yet logged in and the other if they had, I set these controls up to show/hide based on certains states in the MainPage and then bind that to the MainViewModel:
private void Application_Activated(object sender, ActivatedEventArgs e)
{
// Ensure that application state is restored appropriately
....your code here to load stuff...
App.ViewModel.MainPageState = "ShowThemTheLogin";
}
}

The equivalent to onResume() in Windows Phone 7

I'm looking for some app life cycle help from the wp7 experts. My app has a refresh step in a specific page but I only want to launch this when the user brings the app to life from the background.
Note- The life cycle step I'm looking for isn't called when the page is init() only when I'm navigated (back) to or the user has taken a phone call and then re-opens the app (keeping the same page open)
Thank you in advance
what you are looking for is called Tombstoning and you can find a great article at http://wildermuth.com/2010/10/17/Architecting_WP7_-_Part_5_of_10_Tombstoning
The events are:
Launching (opened from tile)
Deactivated (user takes a call or something)
Activated (back from the call)
Closing (Leaves you app via the "Back" button)
You are looking for the Activated event. These are in your App.xaml.cs/vb file. Hook into the event, and update your data model. When your page is bound to that model it will get the data.
If you are not using MVVM, and can't really refresh from that event, you can do it using the PhoneApplicationService.Current.StartupMode property. It has two options Activate (what you are looking for) and Launch (loaded fresh from the tile). It would look something like
Init()
{
if (PhoneApplicationService.Current.StartupMode == StartupMode.Activate)
{
Refresh()
}
}

"Don't Allow" and "Allow" buttons always off-screen on WP7 Facebook OAUTH browser control

I've developed a WP7 client that uses the Facebook C# SDK, using OAUTH and the web browser control.
Everything works fine except that on the page where the user is requested to accept/reject the access permissions I am asking for, the "Don't Allow" and "Allow" buttons are off the bottom of the browser's screen, and it isn't obvious that the user must scroll down to click on them.
I've tried using all the different display modes (touch, wap, page, popup) and "page" is the only one that shows the buttons on the same page, but then the fonts are tiny. I've also tried different sizes for the browser control.
The example in the SDK has the same behavior.
Has anyone found a work-around for this?
The solution I have found is to use Javascript to change the CSS properties for the element:
private void FacebookLoginBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
// check for when this approve page has been navigated to
if (FacebookLoginBrowser.Source.AbsolutePath == "/connect/uiserver.php")
{
showBrowser();
// do the script injection on the LoadCompleted event - doing it here will appear to work when you have a fast connection, but almost certainly fails over 3G because the elements aren't ready in time to be modified
FacebookLoginBrowser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(FacebookLoginBrowser_LoadCompleted);
}
// etc ...
}
void FacebookLoginBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
FacebookLoginBrowser.LoadCompleted -= FacebookLoginBrowser_LoadCompleted;
// Facebook will likely change this and break our code soon, so make sure you anticipates this
try
{
FacebookLoginBrowser.InvokeScript("eval", "document.getElementById('platform_dialog_bottom_bar').style.position = 'relative';document.getElementById('platform_dialog_bottom_bar').style.top = '-60px';");
}
catch
{
// TODO: display instruction to scroll down if we ever end up here
}
}
I hope that helps. Feel free to contact me if you run into problems
I haven't tried this, but could you use the WebBrowser control's Scale(SizeF) method to change the zoom level of the page?

Resources