2016-09-17 62 views
2

我在andrdoid Xamarin中实现了Resources.GetDrawable。该程序的工作原理,但如果我点击实施Resources.GetDrawable程序的力量关闭。这里是我的代码:C# - Resources.GetDrawable已在Android Xamarin中废弃

SetContentView(Resource.Layout.Main); 
drawView = FindViewById<DrawingView>(Resource.Id.drawing); 
LinearLayout paintLayout = FindViewById<LinearLayout>(Resource.Id.paint_colors); 
currPaint = (ImageButton)paintLayout.GetChildAt(0); 
currPaint.SetImageDrawable(Resources.GetDrawable(Resource.Drawable.paint_pressed)); 

Resources.GetDrawable(Resource.Drawable.paint_pressed了绿色(在Visual Studio 2015 Xamarin)强调。日志消息返回例外:

Android.Content.Res.Resources + NotFoundException

'Resources.GetDrawable(INT)' 已过时: '不赞成使用'

对于Java版的解决方案是利用这3个中的一个:

ResourcesCompat.getDrawable(getResources(), R.drawable.name, null); 
//or 
ContextCompat.getDrawable(getActivity(), R.drawable.name); 
//or 
ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme); 

是什么版本的C#的Android Xamarin?谢谢。

回答

1

更改为SetImageResource这样

currPaint.SetImageResource(Resource.Drawable.paint_pressed); 
+0

看来工作,绿色下划线消失了。但该按钮仍然强制关闭应用程序。谢谢。 –

5
var currPaint= ContextCompat.GetDrawable(this,Resource.Drawable.paint_pressed); 
     currPaint.SetBounds(0, 0, 60, 60); 

这是我在我的项目已经implimented即使我有同样的problem.Hope这有助于! @Jake