Clar de intrare text din ViewModel folosind RelayCommand

0

Problema

Aș dori să clarific intrare text din ViewModel care se ajunge acolo. În codul de mai jos am încercat prin utilizarea unui RelayCommand, dar nu merge.

Ceea ce vreau să realizeze: atunci Când faceți clic pe butonul numit AddQuestionToQuizo funcție este executată utilizând Comanda pe buton. Funcția OnCreateQuizClick()situat în ViewModel, este declanșat și această funcție are nevoie pentru a-mi limpezi de text de intrare, care nu ajunge pentru moment.

Am încercat de asemenea să utilizeze în mod regulat de Comandă în loc de a folosi un RelayCommand, dar, de asemenea, aici nu vreau să lucrez.

EDIT: SUB COD FUNCȚIONEAZĂ BINE - AM ACTUALIZAT Codul este utilizat pentru a șterge intrare text atunci când faceți clic pe un buton de pe ViewModel, de punere în aplicare INotifyPropertyChanged Interfață

.xaml - cod

<Button x:Name="AddQuestionToQuiz" WidthRequest="200" Command="{Binding CreateQuizCommand}" Style="{StaticResource ButtonStyle}" Text="Add question to quiz"></Button>

ViewModel - cod

internal class CreateQuizPageViewModel : INotifyPropertyChanged
{
    // Quiz Name Input
    public String QuizNameInput { get; set; }

    private String quizQuestionInput = "";
    public String QuizQuestionInput 
    {
        get { return quizQuestionInput; }   
        set { quizQuestionInput = value; OnPropertyChanged(); }
    } 

    public RelayCommand CreateQuizCommand { get; set; }

    public CreateQuizPageViewModel()
    {
        CreateQuizCommand = new RelayCommand(OnCreateQuizClick);
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public void OnCreateQuizClick()
    {
        QuizQuestionInput = "";
    }
}
1

Cel mai bun răspuns

0

EDIT: VIEWMODEL ACTUALIZAT

.xaml - cod

<Button x:Name="AddQuestionToQuiz" WidthRequest="200" Command="{Binding CreateQuizCommand}" Style="{StaticResource ButtonStyle}" Text="Add question to quiz"></Button>

ViewModel - cod

internal class CreateQuizPageViewModel : INotifyPropertyChanged
{
    // Quiz Name Input
    public String QuizNameInput { get; set; }

    private String quizQuestionInput = "";
    public String QuizQuestionInput 
    {
        get { return quizQuestionInput; }   
        set { quizQuestionInput = value; OnPropertyChanged(); }
    } 

    public RelayCommand CreateQuizCommand { get; set; }

    public CreateQuizPageViewModel()
    {
        CreateQuizCommand = new RelayCommand(OnCreateQuizClick);
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public void OnCreateQuizClick()
    {
        QuizQuestionInput = "";
    }
}
2021-11-24 08:58:05

În alte limbi

Această pagină este în alte limbi

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................