代码之家  ›  专栏  ›  技术社区  ›  SimoNR

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

  •  1
  • SimoNR  · 技术社区  · 6 年前

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

    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);
                }
    
            }
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   Coops    6 年前

    主窗口视图:

    <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>
    

    MainWindowViewModel:

    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];
                }
            }
        }
    }
    

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