gpt4 book ai didi

c# - 将列表框的选定项目传递到 xaml

转载 作者:可可西里 更新时间:2023-11-01 11:24:24 27 4
gpt4 key购买 nike

我是使用 C# 进行编码的新手,我有使用 Laravel(php) 进行编码的背景。

我需要使用 CRUD 构建应用程序 (Windows 8.1)。但是在编辑中我遇到了问题,我需要知道如何将选定的项目传递到其他 xaml 文件中。

我需要将 MainPage 的选定项目传递给 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));
}
}
}

部分截图:

主页 enter image description here

编辑页面 enter image description here

最佳答案

你向右传递参数,向左传递参数只是为了在导航后获取它。

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

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

关于c# - 将列表框的选定项目传递到 xaml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44805302/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com