Currently in Windows Phone 8.1 if I want to navigate to a new Page, first I must create a new Page item - MyNewPage - that produces a XAML and CS file. The in order to navigate to it I do the following:
Frame.Navigate(typeof(MyNewPage));
Now, I want to know if it is possible to create a new Page in code behind and navigate to it, something like:
Page myNewPage = new Page();
Frame.Navigate(typeof(myNewPage));
Since the Navigate method only accepts a typeof(), how can I accomplish this?
This link says that you can pass any object as a 2nd parameter for the another version of Frame.Navigate method. I think you can use it (i suggest that you want to fill some properties from code behind from your new page or smth...). Also you can access your page from Frame after navigation. Think about it, it may helps.
var root = Window.Current.Content as Frame;
var mainPage = root.Content as Page;
Thanks to #gunr2171's wild guess, I was able to make it work. So the final code looks like this:
Page myNewPage = new Page();
Type pageType = myNewPage.GetType();
Frame.Navigate(pageType);
or to make it simpler:
Page myNewPage = new Page();
Frame.Navigate(myNewPage.GetType());
Related
I am very new to Xamarin.
I have created a login page for my app but I need a new page to use as the main menu. The new page should be editable on Xcode to design the UI. It also needs a ViewController.cs to run some code inside it.
Are there any possible ways that I can create a page which needs my requirements that I have just mentioned?
I solved the issue by creating a new ViewControl in Xcode then I specified it's class at identity inspector and I also gave it a storyboard ID from the same menu.
Then I wrote the following code to display the new ViewControl.
MainViewController mainViewController = this.Storyboard.InstantiateViewController("MainViewController") as MainViewController;
mainViewController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen;
PresentViewController(mainViewController, true, null);
MainViewController is the class that I mentioned in identity inspector and second MainViewController which is inside the quotation mark is my storyboard ID.
I am working on a Xamarin.Forms project, and in my PCL i created startup page called "Login.xaml". On this page a user has to fill in their credentials and sends it to an WebAPI. When the API returns "1"/true it opens a new page called "Home.xaml".
How can i prevent the user from returning to the login page when pressing on the back button on the phone? ie: The user logs in on the first page, webapi validates it and returns a "1" so the new page ("Home") gets opened, but when the user presses the Back button it returns to the login screen. This should not be possible until the app gets closed down.
You can remove the login page from the navigation stack when you're pushing your Home.xaml:
await Navigation.PushAsync(new Home()); // or whatever your page is called
Navigation.RemovePage(this);
This way there's nothing to go back to after your user gets to the homepage.
For this to work, your login page needs to be a NavigationPage. For this, you'll have to wrap it with the NavigationPage ctor:
// this goes into your App.cs where you enter your app:
public App()
{
MainPage = new NavigationPage(new Login()); // or whatever your login page is called
}
I suggest you have a look at the documentation: Introduction to Xamarin Forms - Navigation
As for me, calling RemovePage() was giving me all sorts of problems.
What I had to do is shift my thinking. If you do Pop instead of Push, you're actually removing the page for good. So what do you need to do in order to be able to do Pop instead of Push? Insert the next page before the current page first, and then Pop:
var newRootPage = new NewRootPage();
this.Navigation.InsertPageBefore(newRootPage, this);
await this.Navigation.PopAsync();
Note: for this to work you will also need to do wrap your initial root page in a NavigationPage like #germi says in the last part of his answer:
// this goes into your App.cs where you enter your app:
public App()
{
// or whatever your login page is called
MainPage = new NavigationPage(new Login());
}
PS: FWIW this was my fix in my F# open source project.
I am building a Xamarin app, where in my portable logic, I have my app.cs file, launching a HomePage. At the moment this is done ith the following code:
public App ()
{
MainPage = new NavigationPage (new HomePage (Adapter, IsLoggedIn));
}
However, I don't want the "title" bar to show at the top of this page, so would like to launch this page with a PushModalAsync command, so that HomePage is a Modal Page. However, the following line doesn't work, as in app.cs, Navigation is not recognised.
Navigation.PushModalAsync(new HomePage());
Is this possible and if so, how do I get around this?
Thanks.
I don't believe you can set the root page of your App to be a modal. However, if you just want to hide the navigation bar, you can do this in the page you want it hidden on:
NavigationPage.SetHasNavigationBar(this, false);
Since it's your main page you can just set the page itself. Say "HomePage" inherits from "ContentPage":
MainPage = new HomePage (Adapter, IsLoggedIn);
My question is if there's a more elegant solution for navigating between pages that are contained in a Window than searching that Window and changing it's content.
This's how I did it:
Page1 a = new Page1(param);
Window parent = Window.GetWindow(this);
parent.Content = a;
My thought is that if there are Navigation tools already implemented I should be using them, instead of doing that.
Thanks.
I think you can use Navigation Service of the page as follows:
GoTo Page2 From Page1.
Page2 p2 = new Page2();
this.NavigationService.Navigate(p2);
Also the better way is to use Page Function instead of Page.
See My answer for the Page Function here
or refer this msdn page
I'm having an issue with a Windows 8 App Store application that I'm trying to write.
I'm trying to navigate to a new page. I'm using this code in my MainViewModel:
var page = (Window.Current.Content as Frame);
page.Navigate(typeof(Home));
Then in my HomeViewModel I'm trying to access the Home view so that I could get some stuff to work, I'm using this code:
var page = (LayoutAwarePage)(Window.Current.Content);
When I run my application it tells me:
Object reference not set to an instance of an object.
and when I place my mouse over
Window.Current
, I see it is set to
NULL
... So how is this possible? Am I missing something?
If I understand you correctly, I think what you might need to do is to move the code that references the 'Home' page in Home.xaml.cs instead of in HomeViewModel.cs. Call a method in HomeViewModel.cs from Home.xaml.cs - hope this helps. (I am assuming your HomeViewModel.cs is a class, which you'll probably need to instantiate in Home.xaml.cs)