Cancella voce del testo da ViewModel utilizzando RelayCommand

0

Domanda

Vorrei cancellare la voce di testo dal mio ViewModel che è legato lì. Nel codice riportato di seguito ho provato utilizzando un RelayCommand, ma non funziona.

Quello che voglio realizzare: cliccando il pulsante denominato AddQuestionToQuizuna funzione viene eseguita utilizzando il Comando sul pulsante. La funzione OnCreateQuizClick()situato a mio ViewModel, è triggerd e questa funzione ha bisogno di pulire la mia voce testo, che non riesco per il momento.

Ho provato anche ad utilizzare un normale Comando invece di utilizzare un RelayCommand, ma anche qui non ha voglia di lavorare.

EDIT: SOTTO IL CODICE FUNZIONA BENE - È STATO AGGIORNATO IL Il codice è utilizzato per cancellare la voce di testo quando si fa clic su un pulsante da ViewModel, l'implementazione di Interfaccia INotifyPropertyChanged

.xaml - codice

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

ViewModel - codice

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

Migliore risposta

0

EDIT: VIEWMODEL AGGIORNATO

.xaml - codice

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

ViewModel - codice

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

In altre lingue

Questa pagina è in altre lingue

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