Here's what I tried. Changing in the constructor works but I cannot seem to change dynamically.
public PhrasesFrame()
{
InitializeComponent();
correctButton.Clicked += correctButtonClicked;
resetButton.Clicked += resetButtonClicked;
faveLabel.BackgroundColor = Color.Red;
faveLabel.GestureRecognizers.Add(new TapGestureRecognizer
{
Command = new Command(() => FaveLabelTapped())
});
// this works
faveLabel.TextColor = Color.Red;
}
void FaveLabelTapped()
{
AS.phrase.Favorite = !AS.phrase.Favorite;
if (AS.phrase.Favorite) {
// this gives an error
faveLabel.TextColor = Color.Red;
} else {
faveLabel.TextColor = Color.Yello;
}
App.DB.UpdateFavorite(AS.phrase.Favorite, AS.phrase.PhraseId);
}
Gives me a message
Color doesn't exist in the current context
Can someone give me some ideas as to how I can change from inside the FaveLabelTapped method?
If you're really stuggling, why can't you just do something like this:
public PhrasesFrame()
{
InitializeComponent();
correctButton.Clicked += correctButtonClicked;
resetButton.Clicked += resetButtonClicked;
faveLabel.BackgroundColor = Color.Red;
faveLabel.GestureRecognizers.Add(new TapGestureRecognizer
{
Command = new Command(() =>
{
AS.phrase.Favorite = !AS.phrase.Favorite;
if (AS.phrase.Favorite)
{
faveLabel.TextColor = Color.Red;
}
App.DB.UpdateFavorite(AS.phrase.Favorite, AS.phrase.PhraseId);
})
});
}
Creating your command at the same point that you assign it allows you to correctly associate the pointer to the label. Also unless you care about using the method 'FaveLabelTapped()' elsewhere then there's no major need to have it refactored into its own method.
EDIT:
It's possible it's not resolving the reference to the 'Color' class correctly at runtime, so you could also try:
void FaveLabelTapped()
{
AS.phrase.Favorite = !AS.phrase.Favorite;
if (AS.phrase.Favorite) {
// this gives an error
faveLabel.TextColor = Xamarin.Forms.Color.Red; //Change this to include the assembly reference.
}
App.DB.UpdateFavorite(AS.phrase.Favorite, AS.phrase.PhraseId);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.