Xamarin Toast.MakeText 改变颜色
Xamarin Toast.MakeText change Color
如何在 Xamarin Forms 应用程序中更改 Toast 的背景颜色?
我在 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();
但是我收到以下错误:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
自定义 Toast 视图自 Android 11 起已弃用。因此您的代码中 t.View
的值为 null。
正如官方文档所说:
Custom toast views are deprecated. Apps can create a standard text toast with the makeText(android.content.Context, java.lang.CharSequence, int)
您可以检查以下情况:
如何在 Xamarin Forms 应用程序中更改 Toast 的背景颜色?
我在 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();
但是我收到以下错误:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
自定义 Toast 视图自 Android 11 起已弃用。因此您的代码中 t.View
的值为 null。
正如官方文档所说:
Custom toast views are deprecated. Apps can create a standard text toast with the makeText(android.content.Context, java.lang.CharSequence, int)
您可以检查以下情况: