'Xamarin Toast.MakeText change Color

How can I change the background color of a Toast in a Xamarin Forms Application?

I've tried this code on Android 11:

Context context = Android.App.Application.Context;
string message = "Hello toast!";
ToastLength duration = ToastLength.Short;

Toast t = Toast.MakeText(context, message, duration);
System.Drawing.Color c = Xamarin.Forms.Color.Green;
ColorMatrixColorFilter CM = new ColorMatrixColorFilter(new float[]
{
    0,0,0,0,c.R,
    0,0,0,0,c.G,
    0,0,0,0,c.B,
    0,0,0,1,0            
});
t.View.Background.SetColorFilter(CM);
t.Show();

But I get the following error:

System.NullReferenceException: 'Object reference not set to an instance of an object.'



Solution 1:[1]

I managed to solve the issue by updating my OS to Monterey. Then I run brew doctor to make sure everything was running smoothly and reinstalled theos. After that everything is compiling correctly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jose Pérez Cano