繁体   English   中英

WPF 单击按钮并将 Textblock 更新为新问题

[英]WPF Click Button and Update Textblock to a new question

我正在构建一个客户服务风格的应用程序,我有一个问题列表,我们希望通过简单的“好”或“坏”按钮响应来回答这些问题,响应后我希望文本块移动到下一个问题。 我试过了

private void GetQuestions()
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("https://***-***/api/v1/");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var response = client.GetAsync("EPOS/GetQuestions").Result;

            if (response.IsSuccessStatusCode)
            {
                var questionList = response.Content.ReadAsAsync<IEnumerable<Questions>>().Result;

                foreach (var question in questionList)
                {
                    QuestionsBox.FontFamily = new FontFamily("Consolas");
                    QuestionsBox.SetValue(TextElement.FontSizeProperty, 20.0);
                    QuestionsBox.Text = question.QuestionText;
                }
            }
            else
            {
                MessageBox.Show("Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase);
            }

        }

我无法弄清楚如何通过按钮响应将问题更改为列表中的下一个问题。

我想我知道您想从问题中得到什么,并且提交的答案会提示问题移至列表中的下一个。

主窗口视图:

<Window x:Class="TestWpfApplication.MainWindowView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" 
        Height="450" 
        Width="800">

    <Window.Resources>
        <FontFamily x:Key="DefaultFont">Corbel</FontFamily>

        <Style TargetType="Button">
            <Setter Property="FontFamily" Value="{StaticResource DefaultFont}" />
            <Setter Property="FontSize" Value="50" />
        </Style>

        <Style TargetType="Label">
            <Setter Property="FontFamily" Value="{StaticResource DefaultFont}" />
            <Setter Property="FontSize" Value="20" />
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
        </Style>
    </Window.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Label Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center" Content="{Binding Path=QuestionText}" />

        <Grid Grid.Column="1" Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <Button Grid.Column="0" Command="{Binding Path=YesCommand}" Content="Yes" />
            <Button Grid.Column="1" Command="{Binding Path=NoCommand}" Content="No" />
        </Grid>
    </Grid>
</Window>

主窗口视图模型:

using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using TestWpfApplication.Annotations;

namespace TestWpfApplication
{
    internal class MainWindowViewModel : INotifyPropertyChanged
    {
        private RelayCommand _yesCommand;
        private RelayCommand _noCommand;
        private int _questionIndex;
        private string _questionText;
        private List<string> _questions;

        public MainWindowViewModel()
        {
            _questions = new List<string>
            {
                "Name your favourite football team",
                "Do you like F1",
                "Mothers Maiden Name",
                "Random Question"
            };

            QuestionText = _questions[_questionIndex];

            _yesCommand = new RelayCommand(o => Yes());
            _noCommand = new RelayCommand(o => No());
        }

        public string QuestionText
        {
            get => _questionText;
            set
            {
                _questionText = value;
                OnPropertyChanged(nameof(QuestionText));
            }
        }

        public ICommand YesCommand => _yesCommand;

        public ICommand NoCommand => _noCommand;

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private void Yes()
        {
            // Logic for storing the answer to the question
            MoveToNextQuestion();
        }

        private void No()
        {
            // Logic for storing the answer to the question
            MoveToNextQuestion();
        }

        private void MoveToNextQuestion()
        {
            if (_questionIndex < _questions.Count - 1)
            {
                QuestionText = _questions[++_questionIndex];
            }
        }
    }
}

您如何获得问题列表完全取决于您,如何存储答案将是您自己的实现,但从这个简单的解决方案中,您可以看到如何循环浏览问题列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM