Passing some Data between pages

I am in this situation:
The user is in the Page1
Click on the button and navigate to Page2
In the doing of things and comes back

In Page1 in the light of the actions performed in modifying the text of a button (that is, from the I have to bring back data)

The same thing from Page to Page
This is what I did but each call changes the page losing the info

<ContentPage.Content>
    <StackLayout Orientation="Vertical">
        <StackLayout Orientation="Horizontal">
            <Label Text="Cliente: "></Label>
            <Button Text="Seleziona&#10;Cliente" x:Name="ClienteSelect"></Button>
        </StackLayout>
        <StackLayout Orientation="Horizontal">
            <Label Text="Magazzino: "></Label>
            <Button Text="Seleziona&#10;Magazzino" x:Name="MagazzinoSelect"></Button>
        </StackLayout>
        <StackLayout Orientation="Horizontal">
            <Label Text="Codice: "></Label>
            <Entry x:Name="BarcodeFilterEntry" Placeholder="Digitare il Barcode" />
        </StackLayout>
        <StackLayout Orientation="Horizontal">
            <Label Text="Descrizione: " x:Name="DescArticolo"></Label>
            <Entry x:Name="Barcode" ></Entry>
        </StackLayout>
        <StackLayout Orientation="Horizontal">
            <Label Text="Quantità: "></Label>
            <Entry x:Name="QuantitaEntry" ></Entry>
        </StackLayout>
        <StackLayout Orientation="Horizontal">
            <Button Text="Aggiungi" x:Name="AggiungiButton" Clicked="OnAddClicked"></Button>
        </StackLayout>
    </StackLayout>
</ContentPage.Content>

public partial class ListaLiberaInsertPage : ContentPage
{

    public ListaLiberaInsertPage (Session session)
    {
        InitializeComponent ();
        OnPageLoad(session);
    }

    public ListaLiberaInsertPage(Session session, BpId cliente)
    {
        InitializeComponent();
        OnPageLoad(session);
        ClienteSelect.Text = string.Format("{0} ({1})",cliente.CardName, cliente.CardCode);
    }

    public ListaLiberaInsertPage(Session session, Magazzino magazzino)
    {
        InitializeComponent();
        OnPageLoad(session);
        MagazzinoSelect.Text = magazzino.WarehouseName;
    }

    protected override void OnAppearing()
    {
        BarcodeFilterEntry.Focus();
    }

    void OnPageLoad(Session session)
    {

        BarcodeFilterEntry.Completed += (sender, e) =>
        {
            OnSubmitClickedAsync(sender, e, session);
        };

        ClienteSelect.Clicked += (sender, e) =>
        {
            OnOpenClienteSelectClicked(sender, e, session);
        };

        MagazzinoSelect.Clicked += (sender, e) =>
        {
            OnOpenMagazzinoSelectClicked(sender, e, session);
        };

    }

    private async void OnOpenClienteSelectClicked(object sender, EventArgs e, Session session)
    {
        await Navigation.PushAsync(new ClientiListPage(session));
    }

    private async void OnOpenMagazzinoSelectClicked(object sender, EventArgs e, Session session)
    {
        await Navigation.PushAsync(new MagaListPage(session));
    }

}

public partial class ClientiListPage : ContentPage
{
public ObservableCollection bpIdList;

    public ClientiListPage(Session session)
    {

        InitializeComponent();

        OnPageLoad(session);

    }

    protected override void OnAppearing()
    {
        BpFilterEntry.Focus();
    }

    private void OnPageLoad(Session session)
    {

        list.ItemTapped += (sender, ItemTappedEventArgs) =>
        {
            OnItemTappedAsync(sender, ItemTappedEventArgs, session);
        }; 
    }


    private async void OnItemTappedAsync(object sender, ItemTappedEventArgs e, Session session)
    {
        var item = e.Item;
        Navigation.InsertPageBefore(new ListaLiberaInsertPage(session, item as BpId),this);
        await Navigation.PopAsync();
        ((ListView)sender).ItemsSource = null;
    }
}

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Take a look back for the last day or two. At least 4 other threads were started with nearly identical titles. The help in those applies to you as well.

    In brief: If you're passing data between pages instead of between ViewModels you're already far off in the wrong direction. Read up on MVVM - that is the expected pattern you should be use. Your pages (and all UI) is just a reflection of data and should never be the actual repository of data.

    Observation: None of your markup shows any databinding. So yeah, you are already heading in the wrong direction. Read up on MVVM.

Sign In or Register to comment.