我正在构建一个客户服务风格的应用程序,我有一个问题列表,我们想用一个简单的“好”或“坏”按钮来回答。在回答后,我希望“文本”块移至下一个问题。我尝试过
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);
}
}
我不知道如何从按钮响应中将问题更改为列表中的下一个。
答案 0 :(得分:1)
我想我对您想要的问题有所了解,就是先提交答案,然后提示问题移至列表中的下一个。
MainWindowView:
<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];
}
}
}
}
如何获取问题列表完全取决于您自己,如何存储答案将取决于您自己的实现,但是通过这种简单的解决方案,您可以了解如何遍历问题列表。