我正在尝试使用 CotentDialog 获取一些数据并在 UWP XAML 的数据库中更改它,但遇到了一些问题

问题描述 投票:0回答:1
private async void iconu_PointerPressed(object sender, PointerRoutedEventArgs eventArgs)
{
    if (BigGrid.SelectedIndex == -1)
    {
        ContentDialog dialog2 = new ContentDialog
        {
            Title = "Please select a Grid!",
            Content = "When selected it will have a border :)",
            CloseButtonText = "Cancel",
        };
        ContentDialogResult result2 = await dialog2.ShowAsync();
        return;
    }
    ContentDialog dialog = new ContentDialog
    {   

        Title = "Please enter your information",
        CloseButtonText = "Cancel",
        PrimaryButtonText = "OK",
        Content = new StackPanel
        {
            Children =
        {
            new TextBlock { Text = "Title:" },
            new TextBox { Name = "TitleTextBox" },
            new TextBlock { Text = "Description:" },
            new TextBox { Name = "DescriptionTextBox" },
            new TextBlock { Text = "Select Date:" },
            new DatePicker { Name = "DatePicker" },
            new TextBlock { Text = "Select Time:" },
            new TimePicker { Name = "TimePicker" }
        }
        }
    };

    ContentDialogResult result = await dialog.ShowAsync();
    if (result == ContentDialogResult.Primary)
        {
            TextBox titleTextBox = dialog.FindName("TitleTextBox") as TextBox;
            TextBox descriptionTextBox = dialog.FindName("DescriptionTextBox") as TextBox;
            DatePicker datePicker = dialog.FindName("DatePicker") as DatePicker;
            TimePicker timePicker = dialog.FindName("TimePicker") as TimePicker;

            if (titleTextBox != null && descriptionTextBox != null && datePicker != null && timePicker != null)
            {
                Debug.WriteLine("All controls are not null.");
                string title = titleTextBox.Text;
                string description = descriptionTextBox.Text;
                DateTime selectedDateTime = datePicker.Date.DateTime.Date + timePicker.Time;
                Debug.WriteLine($"1.TitleTextBox: {titleTextBox.Text}, DescriptionTextBox: {descriptionTextBox.Text}, DatePicker: {datePicker.Date}, TimePicker: {timePicker.Time}");

            int Id = ((Note)BigGrid.SelectedItem).Id;
                Notedatabase.UpdateData(Id, title, description, selectedDateTime);
                BigGrid.ItemsSource = Notedatabase.GetData();
            }
            else
            {
                Debug.WriteLine("Some controls are null.");
                Debug.WriteLine($"2.TitleTextBox: {titleTextBox}, DescriptionTextBox: {descriptionTextBox}, DatePicker: {datePicker}, TimePicker: {timePicker}");
                ContentDialog dialog1 = new ContentDialog
                {
                    Title = "Please input All values!",
                    Content = "Thank you :)", 
                    CloseButtonText = "Cancel",
                };
                await dialog1.ShowAsync();
            }
    }

我从本地保存的数据库获取数据,BigGrid 是 GridView.ItemTemplate

我期望获得要保存的值,但它不断抛出另一个对话框“请输入所有值”

Debug.WriteLine($"2.TitleTextBox: {titleTextBox}, DescriptionTextBox: {descriptionTextBox}, DatePicker: {datePicker}, TimePicker: {timePicker}");

这行说明里面什么都没有

我想我做错了?但我不太确定

TextBox titleTextBox = dialog.FindName("TitleTextBox") as TextBox;
            TextBox descriptionTextBox = dialog.FindName("DescriptionTextBox") as TextBox;
            DatePicker datePicker = dialog.FindName("DatePicker") as DatePicker;
            TimePicker timePicker = dialog.FindName("TimePicker") as TimePicker;

任何帮助将不胜感激:D

c# uwp uwp-xaml
1个回答
0
投票
            StackPanel stackPanel = (StackPanel)dialog.Content;
            string title = ((TextBox)stackPanel.Children[1]).Text;
            string description = ((TextBox)stackPanel.Children[3]).Text;
            DateTime selectedDate = ((DatePicker)stackPanel.Children[5]).Date.DateTime;
            TimeSpan selectedTime = ((TimePicker)stackPanel.Children[7]).SelectedTime.Value;

我用这个代替,它起作用了:)

© www.soinside.com 2019 - 2024. All rights reserved.