I have two pages that I navigate to and from. One is called the MainPage which is a Pivot page and the other is an ordinary page/class called the AddNewHistoryPage. There is a function called DisplayHistory in the MainPage that I would like to call on navigation back to MainPage from the AddNewHistoryPage.
I found that there is a protected function called OnNavigatedTo. Could someone help to find a little more information, such as
The OnNavigatedTo function should be written in the MainPage if I want to call the DisplayHistory in the mainPage
what does ' base.OnNavigatedTo(e)' mean?
You should save your phones state when navigating to the DisplayHistory page, so when navigating back to main page your information you need retained will stay retained.
Also when using the function OnNavigatedTo that would be used on MainPage but you would have to write a statement checking which pages it was navigated from....such as DisplayHistory.
If you don't use or that write that statement, every time the page is accessed it will run that Function.
WP7 Navigation in Depth
This Above link helped me a lot when learning tombstoning and having to save specific things to memory ICE(In Case of Emergency).
Hope this will help you out! :)
Keeano Martin's Link should be enough to anwser your first question.
Regarding your second question:
'base.OnNavigatedTo(e)' calls the base classes OnNavigateTo method.
Your page inherits from a base class: 'PhoneApplicationPage'. If you do not override the OnNavigatedTo method then the base classes implementation will be called directly. When you do override a method then you should [usually] call the base class implementation (using the code you posted). If you don't do this then any code in the base classes implementation will never be run and you will probably get some unexpected behaviour.
Related
Is there any reason or problem I cant call InitializeComponent method in OnAppearing function of a page in xamarin forms project?
I understand that I must call InitializeComponent only once to create the actual page. But what if I check that Content is already created and do it as below. Is it a bad implementation or practice? because it is said that no xaml based application does it and always call it in a constructor of the page.
reason I want to do it as below because xamarin.forms start up time is slow running on Android and if you use Masterdetail page(I think same for tabbed page), you must initialize it at the start up, it causes every navigation page defined in masterdetail page to be initialized and it costs you 2-3 secs depending on your UI could be even higher cost. any thoughts or experiences on this?
protected override void OnAppearing()
{
if (Content == null)
{
InitializeComponent();
}
}
I do not recommend this approach. From the xamarin docs.
The constructor of that class calls InitializeComponent, which then calls the LoadFromXaml method that extracts the XAML file (or its compiled binary) from the PCL. LoadFromXaml initializes all the objects defined in the XAML file, connects them all together in parent-child relationships, attaches event handlers defined in code to events set in the XAML file, and sets the resultant tree of objects as the content of the page.
https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/getting_started_with_xaml/
If your forms are too slow on Android I would enable Fast Renders instead
In the OnCreate of your main activity add this line of code just before Xamarin.Forms.Init
Forms.SetFlags("FastRenderers_Experimental");
https://xamarinhelp.com/xamarin-forms-fastrenderers-android/
It would not hurt to try to use compiled Xaml also
using Xamarin.Forms.Xaml;
...
[XamlCompilation (XamlCompilationOptions.Compile)]
public class HomePage : ContentPage
{
https://developer.xamarin.com/guides/xamarin-forms/xaml/xamlc/
I have a small issue I was hoping somebody could help me with. I have to call the NavigationService.RemoveBackEntry() on two of my views due to the way I have my first run wizard set up.
This method needs to be called on the view (in the codebehind) as far as I am aware and cannot be called in my view models.
I was wondering what would be the easiest, cleanest way to call a RemoveLastNavEntry() from the ViewModel if the method lives on the view.
Rob has said it is a feature he will build into the navigation service at some point but until then I need to implement this as a minor hack.
While this truely is a task for the view, you can, if you really want to, call it from the ViewModel, as a static call.
(App.Current.RootVisual as PhoneApplicationFrame).RemoveBackEntry()
See PhoneApplicationFrame.RemoveBackEntry Method for documentation.
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.
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!
My question was little diff.
From MainPage.xaml, I am using 'NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));'. It does not goes to Page1.xaml immediately. It just creates an Now the control does not completely goes to Page1.xaml. It again starts execution what is written on the next line to NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative)).
I have to transfer full control towards Page1.xaml, when I return from Page1 then it must resume the remaining execution of MainPage.xaml.
Plz Help.
Navigating to another page is not a synchronous process where you call the page and wait for a return result.
When you pass control to another page, it is effectively something that you fire off and forget.
Any subsequent processing you want to do when you get control back in the original page should be handled in one of the corresponding events/overrides for that page - Loaded, OnNavigatedTo for example.
So when the user navigates back from Page1, you want to do something on MainPage? Override OnNavigatedTo in MainPage and handle the navigation that way. I don't think there's a really elegant way to determining that the navigation was due to a "backward" navigation - you may need to use PhoneApplicationService.Current.State to work that out.
Note that this due to tombstoning etc, this could be a different instance of MainPage from the original one.
Either way, you won't be able to just continue from where you left off within a method. You need to think in a more event-based way.