关于c#:将列表框的选定项目传递给xaml

Passing a selected item of listbox into a xaml

我是用C编写的新代码,我用Laravel(PHP)编写了一个后台代码。

我需要用CRUD构建应用程序(Windows8.1)。但在我遇到问题的编辑过程中,我需要知道如何将选定的项传递到其他XAML文件中。

我需要将选定的主页项传递给Editar

主页.xaml.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    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);
            }
        }
    }

编辑编辑

1
2
3
4
<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>

社论

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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));
        }
    }
}

一些屏幕截图:

主页enter image description here

编辑页面enter image description here


您将参数右移左移,就是为了在导航后得到它。

将此函数添加到editar.xaml.cs

1
2
3
4
5
6
protected override void OnNavigatedTo(NavigationEventArgs e)
{
   var user = e.Parameter as User;
   Nome.Text = user.nome;
   Email.Text = user.email;
}