Converting Content Page to Navigation Page in Xamarin - c#

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());

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.

Show new Page from a button in a Detail Page

I have a MasterDetailPage. I can add a page from my "Menu" and it is content inside the master page. When I want to do the same but with a button from my "FirstMasterPage" it show the Page but over the MasterDetailPage
I tried to create a new MasterPage but not showing nothing
This is the action of the button of one of my pages
Navigation.PushModalAsync(new Pagina_Dos());
I also tried this, but showing nothing. Pagina_Master is my MasterDetailPage
MasterDetailPage mdp = new Pagina_Master();
mdp.Detail.Navigation.PushModalAsync(new Pagina_Dos());
mdp.IsPresented = false;
I want to show my content Page (Pagina_Dos) inside my Master Detail Page, when is clicked from the button. Because from the menu of course its working
first you need a reference to the current MasterDetailPage
var mdp = (MasterDetailPage)App.Current.MainPage;
then you can use the Detail's Navigation property to push a new page
mdp.Detail.Navigation.PushAsync(new Pagina_Dos());

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.

Deep linking/Navigating to tabbed page in prism not working

I have a master detail page as the root page for my application. As a detail page for that I have a tabbed page. In the tabbed page I have a content page and a navigation page containing one content page. In the master detail's OnNavigatedTo I'm retrieving some organisation data from the cloud and then I want to navigate to a manage organisation page which is the plain content page in the tabbed page.
Using NavigateAsync and a relative uri to the manage organisation page I find that the Tabbed Page OnNavigatedTo is hit twice and then I get an exception such as (simplified) below. I can also see Binding errors in my output showing that elements on my manage organisation page xaml tried to bind to the tabbed page view model.
I'm not sure if this is an issue with the view model auto wire change in 6.2 or if it's a deep linking issue or if I've done something wrong.
"Sequence contains no elements".
at System.Linq.Enumerable.Last[TSource] (IEnumerable`1 source) [0x00079]
at Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer.OnAttachedToWindow () [0x00011]
in C:\BuildAgent\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\AppCompat\NavigationPageRenderer.cs:189
Using XF 2.3.1.114, Prism.Forms 6.2, I'm not sure if other packages are relevant. If you think so I can include them.
Also interestingly, it works if I try navigate to the content page in the navigation page from the master detail i.e. "TabbedPage/NavigationPage/ContentPage".
Any ideas what the problem might be?
So first thing first, you don't have the proper XAML syntax for your TabbedPage. When defining the default page for a NavigationPage in XAML you must supply the page via the Arguments element:
<views:SimpleNavPage Title="Foo">
<x:Arguments>
<views:NestedContentPage />
</x:Arguments>
</views:SimpleNavPage>
You must also have the proper ctor in your navigation page:
public SimpleNavPage(Page root) : base (root)
{
InitializeComponent();
}
Now, I personally don't recommend navigating within the OnNavigatedTo unless you can absolutely guarantee that you will not be adding that page in another deep link, or navigation scenario. Imagine you start out with NavigateAsync("MainPage") and that has an OnNavigatedTo that does a deep link navigation operation. Now you decide to NavigateAsync("SomeOtherPage") in which it NavigateAsyc("AnotherPage/MainPage/SomeOtherPage/LastPage") from within it's OnNavigatedTo. Now, you will have created an issue because the MainPage.OnNavigatedTO will have kicked off another navigation operation while you are still navigating to the next "SomeOtherPage". You are asking for trouble.

Error while Navigating from Window to page in WPF application

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();

Categories

Resources