简体   繁体   中英

Xamarin.Forms variable navigation

I'm currently making a stock control app for Android with Xamarin.Forms. I have an extra page just for entering the Pruduct ID. I want to use that page in two scenarions:

  1. You pick an product to add/remove them from stock
  2. You pick an product to tell the office that new ones have to be ordered

After picking the product you will be redirectet to:

  1. The "DetailStockmovementPage" in case 1
  2. The "DetailOrderPage" in case 2

The Problem is, that the next Page depends on what button was clicked before . In my ViewModel I have the following Code to navigate (for scenario 1):

public async Task GoToNextPage(Product product)
        {
            await Navigation.PushAsync(new DetailStockmovementPage(product), true);
        }

How can I make the next Page variable? Can I define a variable "NextPage" in my ViewModel, set it from the View like "vm.NextPage = DetailStockmovementPage" and Navigate like

public async Task GoToNextPage(Product product)
            {
                await Navigation.PushAsync(new NextPage(product), true);
            }

I know that does not work, but I think that poor try made clear what I want to achieve. I mean I could push a string for each page and make an if-query before navigating, but I don't think thats an good way to do it. How can I vary the page to be navigated to?

You could pass the user's choice from the first page into the page where they select the product, and then use that information to decide which page to navigate to. For example:

In your App.cs file add an enum:

public enum NavTarget
{
    Order,
    Stockmovement
}

Define a property in your VM for the target selected in your menu page:

public NavTarget Target { get; set; }

...and use it in your navigation method:

public async Task GoToNextPage(Product product)
{
    if (Target == NavTarget.Stockmovement)
        await Navigation.PushAsync(new DetailStockmovementPage(product), true);
    else
        await Navigation.PushAsync(new WhateverItsCalledPage(product), true);
}

Then set that property in the constructor for your ProductSelectionPage:

public ProductSelectionPage(NavTarget target)
{
    InitializeComponent();

    // some code that sets the VM for this page
    // ...

    vm.Target = target;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM