Error while Navigating from Window to page in WPF application - c#

I read around hundreds of Q/A & Blogs for the same, but not able to resolve the error I am getting. In my WPF application I need to navigate from a MainWindow.xaml to a Page Register.xaml. I have below code :
Register register = new Register();
MainWindow.Navigate(register);
Or
this.NavigationService.Navigate(new Uri("Register.xaml ", UriKind.Relative));
It is giving me error
'.MainWindow' does not contain a definition for 'Navigate'
Or
MainWindow' does not contain a definition for 'NavigationService'

To open a page in you need to have it in a frame.
A page can be hosted from Window, NavigationWindow, Frame, or from a browser. To be hosted, a page can be:
The direct child of a Window, NavigationWindow, or Frame element in
XAML.
Instantiated and set as the value of the Content property of Window, NavigationWindow, and Frame.
Set as the uniform resource identifier (URI) source of the Source property of either NavigationWindow or Frame.
If you want to have it in your MainWindow you can do the following.
XAML
<Frame Name="contentFrame" />
C# code-behind
contentFrame.Content = new Page();
To open it in a new window you could do something like this.
Page p = new Page();
p.Show();

Related

Is there a way to replace the root page of a MAUI app?

I have some controls on a page in a Xamarin.Forms app that DO NOT WORK when navigating back to the page. I can only "go back" to the page by navigating to the page outright (I'm using Prism to do this) and it works.
When transitioning to MAUI, I'm writing my own navigation service to replace Prism. Everything works, except navigation to replace the first page and clear the navigation stack. This is NOT App.MainPage.Navigation.PopToRootAsync() The page is the first page in the navigation stack. Attempting to replace App.MainPage with a new page does not work. Nothing happens if I do this.
Example navigation stack:
MainPage
Page1
Page2
Navigation stack after navigating to new root page:
MainPage (but new instance of page, not the original instance). Remainder of navigation stack has been cleared.
I'm not sure what I was doing wrong, but further testing shows that you can replace MainPage for Maui.
You CANNOT just assign a NavigationPage to MainPage. This will work for the navigation and any logic in associated viewmodel will work, but the UI will be blank.
If you just assign a ContentPage to MainPage, everything works great.

Open new page outside masterdetailpage

I just new to Xamarin.Form with Prism. I want to load the page in different behavior. The first one I achieve already is in image below.
But I want to do a behavior like below image. Load a new page outside master detail page. How can I do it in prism?
You have figured out how to display the "Hamburger" icon as well as title by doing the following.
NavigationService.NavigateAsync("MasterPage/NavigationPage/DetailsPage")
If a user makes a selection from the actions listed on the master page. For Example, let's say settings.
You have a couple of options here, you can do navigate relatively
NavigationService.NavigateAsync("Settings")
This will navigate to the settings page. This will also display the back button as the second image. Your current page path will be
MasterPage/NavigationPage/DetailsPage/Settings
Now let's say you want the settings page to be the top details page. You have to navigate to it via an absolute path.
MasterPage/NavigationPage/Settings
NavigationService.NavigateAsync("MasterPage/NavigationPage/Settings")
You need to add a new page to the stack, page over existing one. To do that you should do navigation like this:
navigationService.NavigateAsync("Settings")

UWP Navigate throws AccessViolationException for xaml-less pages

Is it just me, or is it impossible to navigate to a page that does not have a .xaml head?
I am constructing a page entirely in code, and I want to navigate to it. I do not want a xaml page because this is a class library and also it is constructed based on data received. I know all about using .xaml to create the page with templates, binding, etc., but I want to avoid that.
When I call Frame.Navigate(typeof(CodePage)), I get nice AccessViolationException.
My page is simple, and so is the navigation. This is the code with a clean, new project
Navigation (button click):
Frame rootFrame = Window.Current.Content as Frame;
rootFrame.Navigate(typeof(CodePage));
Page:
public class CodePage : Page
{
public CodePage()
{
Content = new TextBlock
{
Text = "It works!",
};
}
I know about this issue: Navigate to a Page of another Class Library but, this is because ALL the pages are in the library, I just have 1 specific page in my library. Also, I have other pages in the "launcher" app.
Navigation only works with pages who have xaml part as well because when the page does InitializeComponent in its constructor it sets up the page for Navigation routes and NavigationCache etc.

Converting Content Page to Navigation Page in Xamarin

I created a Content Page named MainList.xaml. Now my main page isn't a navigation page since it deals with setting the App up but I want MainList.xaml to be a navigation page. The problem is, when I try:
MainList mainListPage = new NavigationPage(new MainList());
I get the error:
Cannot implicitly convert type 'Xamarin.Forms.NavigationPage' to 'SalApp.views.MainList'
How would I go about converting it into a navigation page? There doesn't seem to be an option to even create a navigation page. In the App.xaml file, I can make MainPage.xaml a navigation page but I don't know why MainList doesn't have the same privilige.
you're creating an instance of NavigationPage that wraps MainList, to the type returned will be a NavigationPage
NavigationPage navPage = new NavigationPage(new MainList());

How to Navigate from Tab Page to Normal WPF Page

I have four tab pages in Tab controller. I wish to navigate from a tab page to Normal WPF page. For that I am using this.NavigationService.Navigate(new Page1()); . I am able to navigate to new page that is Page1 but the tab control is still there. Seems Like only stack panel is getting replaced. Is there any way to do it correctly?
You are calling the Navigate method on the ListBox, so only that control is replaced. By setting the MainWindow's Content, only the new Page will be displayed:
Application.Current.MainWindow.Content = new Page1();

Categories

Resources