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
Related
I have a main page A with a click button. Once the button is clicked, it will call a method on another c# class and trigger a new page B from this new c# class. However, the page A does not refresh with page B.
If I call this new page B from code inside page A, then it will refresh with page B.
May I know why and how do i solve this?
Thanks.
from within a Page
Navigation.PushAsync(new PageB());
from a non-page class
App.Current.MainPage.Navigation.PushAsync(new PageB());
both approaches assume your current page is enclosed in a NavigationPage
When trying to navigate from a Content Page to a MasterDetail Page the menu icon does not appear. Although, if i first load my MasterDetail Page the menu icon is there as tis supposed to be. The menu functionality is not affected. Working on Xamarin Forms but the problem occurs only when debugging in Android device.
Any idea about the icon?
Load instantly MasterDetail Page using MainPage = new NavigationPage(new MainPage())
Load from Content Page (LoginPage) using MainPage = new NavigationPage(new Login()) then when a button is pressed navigate to MasterDetail Page using await Navigation.PushAsync(new MainPage(), true);
Menus works on both cases
Changing from a content page to a master detail page is achieved by changing your navigation stack. You need to replace the MainPage to do this.
It's two different types of navigation.
You usually see this type of stack swap when navigating from a login page which is usually a simple content page to the main app landing page which can be a master detail page.
So rather than this
await Navigation.PushAsync(new MainPage(), true);
Do this
Application.MainPage = new MainPage();
If you are using an MVVM framework, this can best be achieved with a StackService injected into the View Model.
The MVVM framework I use, FreshMVVM, has the ability to swap out navigation stacks built in.
Follow this link then jump to the "Switching out NavigationStacks on the Xamarin.Forms MainPage" section for more details.
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);
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());
I have a Search.aspx page which calls UCSearch control. UCSearch control does everything like getting what is being searched and what should be displayed, etc. I am trying to give the title to the page. As i dont have any info to write the code in the aspx page, i am thinking to write it in the control. But it is not displaying me when i tried using Page.Title in control. What am i doing wrong?? This is in Asp.net and C#.
Page.Title = "Search Results for Newark, NY";
Thanks in advance!!
Does not:
this.Page.Title = "My beautiful title";
work?
You should be able to get to the ASPX using the Parent property of the control. Cast that property to a Page (it's a WebControl or something similarly generic), then set its Title property. If you have a hierarchy of master pages or are nesting this control in other controls, you may need to traverse the Parent hierarchy for a few more levels.
You could also fire an event from your user control, passing the Title that you would like to display. You could then handle this event on the page, and set the title.
This does require a small amount of code in your aspx page, however, at least now the user control does not care where the title goes, the parent of the control can worry about it. If ever you want to change where this title goes, or even put it in multiple places, you don't have to change the user control. Let me know if you want a sample, I'll add it.
If you don't like that idea, then cederlof's answer will work, I just tested it.
protected void helloBtn_Click(object sender, EventArgs e)
{
this.Page.Title = "hello from control";
}
** You can use naming container to find the parent control of the current control. Through this way you can move through the page hierarchy.
Quickwatch will help you a lot in figuring out the things and building the statement for quick casting. Do some more research on naming container.
var container = userControl.NamingContainer;
if(container is Page)
{
Page p = container as Page;
p.Title = "Your Title";
}
**
Above is not the exact solution, but can help you it you can usercontrol directly on the page. Unless you need to iterate through the page controls. This was just for an quick help.