简体   繁体   中英

passing data back to main page using xamarin.forms

I have 2 pages product_page and novo_pedido_page . when I'm in novo_pedido_page I have a button where i call product_Page to select one product from a grid. What I wanna do is, select this product and send back to novo_pedido_page .

What i've done so far is this on novo_pedido_page :

private async void ToolbarItem_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushModalAsync(new product_page());
        }

You need to use Event Handlers and Delegates:

In product_page you should send the event:

public event EventHandler<Product> ProductSelected;

private void Product_Clicked(object sender, EventArgs e)
{
    var productId = ((Button) sender).CommandParameter;
    var product = GetProduct(productId);
    ProductSelected?.Invoke(this, product);
}

In novo_pedido_page you have to subscribe and handle this event :

private Product selectedProduct;

private async void ToolbarItem_Clicked(object sender, EventArgs e)
{
    var page = new product_page();
    page.ProductSelected += OnProductSelected;
    await Navigation.PushModalAsync(page);
}


private void OnProductSelected(object sender, Product product)
{
    this.selectedProduct = product;
}

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