RelayCommand kullanarak viewmodel'den giriş metnini temizle

0

Soru

Orada bağlı olan viewmodel'imden giriş metnini temizlemek istiyorum. Aşağıdaki kodda bir RelayCommand kullanarak denedim, ancak çalışmıyor.

Ne yapmak istiyorum: Adlandırılmış düğmeye tıklandığında AddQuestionToQuiz, düğmedeki Komut kullanılarak bir işlev yürütülür. İşlev OnCreateQuizClick() viewmodel'imde bulunan triggerd ve bu işlevin şu an için alamadığım giriş metnimi temizlemesi gerekiyor.

Ayrıca bir RelayCommand kullanmak yerine normal bir Komut kullanmaya çalıştım, ancak burada da çalışmak istemiyor.

DÜZENLEME: KODUN ALTINDA İYİ ÇALIŞIYOR-GÜNCELLENDİ Kod, INotifyPropertyChanged Arabirimini uygulayarak viewmodel'inizden bir düğmeye tıklandığında giriş metnini temizlemek için kullanılır

.xaml kodu

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

ViewModel kodu

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

En iyi cevabı

0

DÜZENLEME: VİEWMODEL GÜNCELLENDİ

.xaml kodu

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

ViewModel kodu

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

Diğer dillerde

Bu sayfa diğer dillerde

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