我创建了Android窗口小部件,所以我想动态更改图像源。我尝试过的小部件代码无法加载,但是当我删除更改ImageView源的代码时,它加载正常。
这是我的代码。
int test = (int)typeof(Resource.Drawable).GetField("Cloud").GetValue(null);
public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
var x = new ComponentName(context, Java.Lang.Class.FromType(typeof(AppWidget)).Name);
appWidgetManager.UpdateAppWidget(x, BuildRemoteViews(context, appWidgetIds));
}
private RemoteViews BuildRemoteViews(Context context, int[] appWidgetIds)
{
var widgetView = new RemoteViews(context.PackageName, Resource.Layout.widget);
LoadData(widgetView);
return widgetView;
}
private void LoadData(RemoteViews widgetView)
{
widgetView.SetTextViewText(Resource.Id.widgetCity,city);
widgetView.SetTextViewText(Resource.Id.widgetWeatherDescription, description);
widgetView.SetImageViewResource(Resource.Id.widgetIcon, test); //This line fails the widget from loading
}
我可能做错了什么,或者我可以采取什么措施解决此问题。我正在使用Xamarin Android
由于您的代码不完整,所以我写了一个简单的示例,您可以检查它。
我将一个点击事件添加到ImageView,当您单击imageview时,它将更改图像资源。
[BroadcastReceiver(Label = "HellApp Widget")]
[IntentFilter(new string[] { "android.appwidget.action.APPWIDGET_UPDATE" })]
// The "Resource" file has to be all in lower caps
[MetaData("android.appwidget.provider", Resource = "@xml/appwidgetprovider")]
public class AppWidget : AppWidgetProvider
{
private static string AnnouncementClick = "AnnouncementClickTag";
private int test = (int)typeof(Resource.Drawable).GetField("fivePlus").GetValue(null);
/// <summary>
/// This method is called when the 'updatePeriodMillis' from the AppwidgetProvider passes,
/// or the user manually refreshes/resizes.
/// </summary>
public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
var me = new ComponentName(context, Java.Lang.Class.FromType(typeof(AppWidget)).Name);
appWidgetManager.UpdateAppWidget(me, BuildRemoteViews(context, appWidgetIds));
}
private RemoteViews BuildRemoteViews(Context context, int[] appWidgetIds)
{
// Retrieve the widget layout. This is a RemoteViews, so we can't use 'FindViewById'
var widgetView = new RemoteViews(context.PackageName, Resource.Layout.Widget);
LoadData(widgetView);
RegisterClicks(context, appWidgetIds, widgetView);
return widgetView;
}
private void LoadData(RemoteViews widgetView)
{
widgetView.SetTextViewText(Resource.Id.widgetMedium, "HelloAppWidget");
widgetView.SetTextViewText(Resource.Id.widgetSmall, string.Format("Last update: {0:H:mm:ss}", DateTime.Now));
}
private void RegisterClicks(Context context, int[] appWidgetIds, RemoteViews widgetView)
{
var intent = new Intent(context, typeof(AppWidget));
intent.SetAction(AppWidgetManager.ActionAppwidgetUpdate);
intent.PutExtra(AppWidgetManager.ExtraAppwidgetIds, appWidgetIds);
// Register click event for the Background
var piBackground = PendingIntent.GetBroadcast(context, 0, intent, PendingIntentFlags.UpdateCurrent);
widgetView.SetOnClickPendingIntent(Resource.Id.widgetBackground, piBackground);
// Register click event for the Announcement-icon
widgetView.SetOnClickPendingIntent(Resource.Id.widgetAnnouncementIcon, GetPendingSelfIntent(context, AnnouncementClick));
}
private PendingIntent GetPendingSelfIntent(Context context, string action)
{
var intent = new Intent(context, typeof(AppWidget));
intent.SetAction(action);
return PendingIntent.GetBroadcast(context, 0, intent, 0);
}
/// <summary>
/// This method is called when clicks are registered.
/// </summary>
public override void OnReceive(Context context, Intent intent)
{
base.OnReceive(context, intent);
// Check if the click is from the "Announcement" button
if (AnnouncementClick.Equals(intent.Action))
{
try
{
RemoteViews remoteViews = new RemoteViews(context.PackageName, Resource.Layout.Widget);
remoteViews.SetImageViewResource(Resource.Id.widgetAnnouncementIcon, Resource.Drawable.fivePlus);
AppWidgetManager appWidgetManager = AppWidgetManager.GetInstance(context);
ComponentName componentName = new ComponentName(context, Java.Lang.Class.FromType(typeof(AppWidget)).Name);
appWidgetManager.UpdateAppWidget(componentName, remoteViews);
}
catch
{
// Something went wrong :)
}
}
}
}