简体   繁体   English

Xamarin.Forms变量导航

[英]Xamarin.Forms variable navigation

I'm currently making a stock control app for Android with Xamarin.Forms. 我目前正在使用Xamarin.Forms为Android开发一个股票控制应用程序。 I have an extra page just for entering the Pruduct ID. 我还有一个额外的页面,用于输入产品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 情况1的“ DetailStockmovementPage”
  2. The "DetailOrderPage" in case 2 情况2中的“ DetailOrderPage”

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): 在我的ViewModel中,我有以下代码可以导航(针对场景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 我可以在ViewModel中定义变量“ NextPage”,从“ vm.NextPage = DetailStockmovementPage”这样的视图中进行设置,然后像

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. 我的意思是我可以为每个页面推送一个字符串,并在导航之前进行一次if查询,但是我认为这不是一个好方法。 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: 在您的App.cs文件中添加一个枚举:

public enum NavTarget
{
    Order,
    Stockmovement
}

Define a property in your VM for the target selected in your menu page: 在VM中为在菜单页面中选择的目标定义属性:

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: 然后在构造函数中为您的ProductSelectionPage设置该属性:

public ProductSelectionPage(NavTarget target)
{
    InitializeComponent();

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

    vm.Target = target;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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