2016-09-17 61 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