John Livermore
John Livermore

Reputation: 31313

Xamarin Forms open a view without Navigation

Xamarin Forms navigation examples I see leverage a NavigationPage...

public App()
{
    InitializeComponent();

    MainPage = new NavigationPage(new HyperTrack2.Login());
}

and then when an event occurs to move to the next page...

async void OnLogin(object sender, EventArgs e)
{
    await Navigation.PushAsync(new MapPage());
}

How can one show a view without using the NavigationPage?

Upvotes: 2

Views: 2052

Answers (1)

irreal
irreal

Reputation: 2296

Depends on what you consider a view to be.

The process you outlined in your question is the prefered way to navigate using Xamarin pages. Xamarin pages contain one or more controls, or views. Usually some layout views at the root and then they branch out as needed.

The built in navigation mechanism actually utilises the optimal native techniques for navigating inside your application and will allow you to use standard gestures and buttons for navigations, display standard toolbars and title views, etc.

I fully recommend that unless you have very specific needs, you keep using the built in navigation mechanism.

Otherwise, your other option for moving through xamarin.forms pages is by just settting the MainPage property of your Application class (called App by default) You can either access it through Application.Current.MainPage = new MyPage(); or if writing code within the app class, directly by calling MainPage = new MyPage();

Lastly, you specifically mention showing a view and not a page. If you are talking about a single control within a page, you can either bind it's IsVisible property to some value in your ViewModel, or you can give it an x:Name="myControl" (if using XAML) or store a reference to it (if creating UI in code behind) and then do myControl.IsVisible = true/false somewhere in your code behind. This toggles the visibility of a single control, which will then become a part of your page hiearchy.

If you instead wish to display the view as a "popup" over the existing page, without it actually becoming a part of the page, there are some built in simple UI elements you can use, such as action sheet and display alert. These provide simple popup windows that offer a couple of actions or display a message. For more complex UI, look into Nuget plugin packages, or if you prefer, you can write your own by implementing the functionality on all the target platforms you require.

I suggest looking into Acr User dialogs as it's a wonderful plugin that has served me quite well. It has a much broader set of UI elements you can use, but it still won't let you define any element that you want and display it in a popup, to do that, search for popup plugins for Xamarin.Forms. There are quite a few, but none work perfectly imho.

Upvotes: 3

Related Questions