Page navigation Xamarin.Forms - c#

I'm new to android apps and xamarin.
I'm trying to build an android app, which exists of 2 views.
1 view with a listview, and 1 view to add a new item to the listview.
My idea is that whenever you press the button, the second view appears.
And when you click on the button add, it switches back to the first view.
I thought this code would work:
await Navigation.PushAsync (new ContactPage());
But as you can see,
The term Navigation is not being recognized.

One mistake I see in your code is using await with Navigation, it's not needed their.Second It seems like you are just setting your first page as MainPage in App.cs.
In order to use the Navigation class in Xamarin.Forms you have to first create NavigationPage instance usually people do that in App.cs like following :
MainPage = New NavigationPage (New ContentPage());
And then in any page they can use navigation by using following code like
Navigation.PushAsync(New ContentPage());

You can only use Navigation with NavigationPage.

So i've found the solution.
First of all it is not possible to have a App.cs file if you don't use cross-platform, which in my case is true.
To solve the problem, you have to use another method, which is called android Multiscreen. Here's a link if you want to check it out.

Related

Xamarin Android App

I'm developing an Android app with Xamarin and I was able to understand how to create a button and go from the main layout1 to layout2 but when I try to go from layout2 to layout3 the button doesn't work and I've been trying to research and get nothing.
I tried putting this code in the MainActivity.cs and in the second Activity so it can go to layout3 but I noticed my code works from MainActivity.cs to go to layout3 but the code doesn't work from layout2 to go to layout3 - help please?
Button BreakFast = FindViewById<Button>(Resource.Id.BTN_MAINMENU_BREAKFAST);
BreakFast.Click += delegate {SetContentView(Resource.Layout.BreakfastMenu);};
I'm not sure, because what you're doing is kind of freakish to me, but since you're setting a new layout you should bind the new id's to the button. I'm guessing you have a button on layout 2, which should take you to layout 3. You should bind the variable to a view's id.
Also... don't do that. Create another activity, so when it start onCreate will be called and that will bind your views.

Xamarin.Forms Prism - Using navigation service to navigate from one detail page to another

In my Xamarin.Forms Prism app, I am using a MasterDetailPage for navigation.
While I am on one detail page, I would like to navigate to another detail page, as if I had selected it from the master page.
Initial navigation in App.xaml.cs:
protected override void OnInitialized()
{
...
NavigationService.NavigateAsync("MainPage/RootNavigation/MyFirstPage");
}
When I click a shortcut button on MyFirstPage, I would like to go to MainPage/RootNavigation/MySecondPage. The closest that I have been able to achieve has been using an absolute Uri.
private async void OnShortcutTapped(MyModel sender)
{
...
await _navigationService.NavigateAsync(new Uri("http://myapp.com/MainPage/RootNavigation/MySecondPage", UriKind.Absolute), navigationParams, null, false);
}
This basically gets me what I want, but after navigating in this manner, if I make the Master visible and select the menu item for MySecondPage, it refreshes the detail page as if it is navigating to the page.
Is there a better way to maintain this navigation, so that the master page knows that MySecondPage is already being displayed and it doesn't try to reload it?
While your navigation pattern doesn't make a lot of sense to me, you can achieve what you want by invoking a navigate command in the MasterDetailPageViewModel. You have a number of ways to do this. You could use the IEventAggregator to send a message to the MasterDetailPageViewModel to navigate, or you can use a CompositeCommand that invokes a DelegateCommand that exists on the MasterDetailPageViewModel.
You can see a sample of using a CompositeCommand here: https://github.com/PrismLibrary/Prism-Samples-Forms/tree/master/UsingCompositeCommands
You can also see how to send messages in this sample that I gave at the Xamarin Evolve conference: https://github.com/brianlagunas/Evolve2016SamplesAndSlides
Another option would be to just call a navigate command off the App.Current.MainPage ViewModel from with your MyFirstPage code behind.

What is the most efficient way to Navigate to a XAML page? wp8

Suppose, I have two XAML page: MainPage.xaml & Page1.xaml.
For navigating to Page1.xaml, I always use this code:
Page1 mynewPage = new Page1();
this.Content = mynewPage;
But I see people using other codes for navigating purpose. Am I doing this in an inefficient way? What's the most efficient way to do this?
use this.NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
In that case you have not control about, for instance going back,in your case have to create code to manage the navigation by yourself, and you might to need to store the information of each page at a time, that is automatic when you do it.
Apart there are transitions between pages that are great to show the user that the page is changing. Of course you can create your own transitions but at the end you are creating the navigation by yourself.
With navigation you can control the events of Navigating and navigated and use them to initialize the page, etc.
In windows phone 8, you should use "NavigationService". NavigationService contains methods, properties, and events to support navigation and implemented by Microsoft.
Your code just updates content. It doesnt keep navigation history. So, you cant use back button unless override. You cant pass parameter to another view or you cant know navigation sucessfully ended.
To sum up, navigation service offers all of these features and mores. For detail information , you can checkout in app navigation model from here: In-app navigation for Windows Phone 8

MvvmCross Navigate From ViewModel Event

I read about "ViewModel to ViewModel navigation" and "View Model Lifecycle" from here:
https://github.com/MvvmCross/MvvmCross/wiki/ViewModel--to-ViewModel-navigation
https://github.com/MvvmCross/MvvmCross/wiki/View-Model-Lifecycle
I can use Init() or Start() methods to initialise current ViewModel.
Also I can pass parameters from one ViewModel to another and receive it in the Init() method.
So my question:
When I created windows phone apps I used "NavigateTo" and "NavigateFrom" methods.
Init() is similar to "NavigateTo".
But I didn't find alternative for "NavigateFrom" method in mvvmcross and I don't know how to reload data when I move 'back' by "Close(this)" or using back button on windows phone.
Could you hint me?
Thanks in advance!
updated
I found out that Messenger (MvvmCross plugin) can help me with informing first ViewModel, when an other second ViewModel has changed data (for example add an item to a collection).
So when the second ViewModel add a new item, first ViewModel reloads the data in the OnCollectionChanged(CollectionChangedMessage obj) method.
Stuart showed how to work with this plugin in the NPlus1DaysOfMvvmCross/N-13-CollectABull-Part2.
Link here: https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/tree/master/N-13-CollectABull-Part2
But I didn't find alternative for "NavigateFrom" method in mvvmcross and I don't know how to reload data when I move 'back' by "Close(this)" or using back button on windows phone.
In general, you don't need to reload data in this event - because the ViewModel is already created and initialised from the previous navigation in the forwards direction.
If you do want to do some refresh of the ViewModel when navigating back, then the IVisible pattern in the N=42 video may help but you'll need to add this to your View and ViewModel yourself - see http://slodge.blogspot.co.uk/2013/11/n42-is-my-viewmodel-visible-can-i-kill.html

How do you navigate to a new page in a metro app?

trying to write a metro app in C# right now. Ran into a problem while trying to navigate to a new page.
<HyperLinkButton NavigateUri="foo.xaml"/>
doesn't work, as the NavigateUri field doesn't exist.
The Windows.Navigate namespace isn't available either, so no luck there. What's the proper way to go to a new page in my app in metro?
You can handle the Button control's Click event (in fact, you can use all events with the following code) because Metro's HyperLink button only inherits the ButtonBase class without any special properties or events, such as NavigateUri.
If you want to navigate to another page in your metro app, add a frame in the .xaml page and put this code in the button's event handler:
this.Frame.Navigate("VideoStoryCreator.ComposePage");
There are two ways of Navigating to another page -
Client app way ->
You'll implement this in click event -
var page = new PageName();
Window.Current.Content = page;
Similar to Silverlight Navigation
If you are using Frame/Page Navigation then you can do it like this -
Create a shell page (master page) with the element declared. Then instead of creating new pages with create .
The easiest way to do this is to replace UserControl with Page
This is nicely explained in this tutorial -
http://www.markermetro.com/2011/11/technical/windows-8-metro-style-apps-implementing-navigation-with-xaml/
Hope it helps!
Regards,
Bhavik
Handle the Click event on the button and then call the Navigate method on your Frame
just type the following code in your click event
this->Frame->Navigate(TargetPageName::typeid,this);

Categories

Resources