1
votes

I'm very new coding with C#, I have a background coding with Laravel(php).

I need to build app (Windows 8.1) with CRUD. But in the Edit I'm having a problem, I need to know how to pass a selected item into other xaml file.

I need to pass a selected item of MainPage to Editar

MainPage.xaml.cs


    namespace SQLiteDemo
    {
        /// 
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// 
        public sealed partial class MainPage : Page
        {
            SQLiteAsyncConnection conn = new SQLiteAsyncConnection("dados.sqlite");

            public MainPage()
            {
                this.InitializeComponent();
                conn.CreateTableAsync();
            }

            private async void Listar_Click(object sender, RoutedEventArgs e)
            {
                await Atualiza();
            }

            private async Task Atualiza()
            {
                var query = conn.Table();
                listBox.ItemsSource = await query.ToListAsync();
            }

            private void Novo_Click(object sender, RoutedEventArgs e)
            {
                Frame.Navigate(typeof(Novo));
            }

            private void Editar_Click(object sender, RoutedEventArgs e)
            {
                /*
                var u = listBox.SelectedItem as User;
                u.nome = "nome alterado";
                await conn.UpdateAsync(u);
                await Atualiza();
                */
                listBox.SelectedItems.Add(listBox.SelectedItem as User);
                var u = listBox.SelectedItem as User;
                Frame.Navigate(typeof(SQLiteDemo.Editar), u);
            }
        }
    }

Editar.xaml

<Grid HorizontalAlignment="Left" Height="520" Margin="55,115,0,0" VerticalAlignment="Top" Width="1155">
    <TextBox x:Name="Nome"  HorizontalAlignment="Left" Margin="70,60,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="40" Width="990" PlaceholderText="Nome"/>
    <TextBox x:Name="Email" HorizontalAlignment="Left" Margin="70,140,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="40" Width="990" PlaceholderText="Email"/>
</Grid>

Editar.xaml.cs

namespace SQLiteDemo
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    /// 
    public sealed partial class Editar : Page
    {
        SQLiteAsyncConnection conn = new SQLiteAsyncConnection("dados.sqlite");

        public Editar()
        {
            this.InitializeComponent();
            conn.CreateTableAsync<User>();
        }

        private void SalvarEdit_Click(object sender, RoutedEventArgs e)
        {
            /*
            var u = listBox.SelectedItem as User;
            u.nome = Nome.Text;
            u.email = Email.Text;
            conn.UpdateAsync(u);
            */

        }

        private void Voltar_Click(object sender, RoutedEventArgs e)
        {
            Frame.Navigate(typeof(MainPage));
        }
    }
}

Some screenshots:

MainPage enter image description here

Edit page enter image description here

1

1 Answers

3
votes

You passing the parameter right what left is just to get it after the navigation.

Add this function to the Editar.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e)
{
   var user = e.Parameter as User;
   Nome.Text = user.nome;
   Email.Text = user.email;
}