C# - Resources.GetDrawable 在 Android Xamarin 中已过时

标签 c# android android-layout xamarin xamarin.android

我在 andrdoid Xamarin 中实现了 Resources.GetDrawable。该程序有效,但如果我单击实现 Resources.GetDrawable 的按钮,程序将强制关闭。这是我的代码:

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)' is obsolete: 'deprecated'

对于 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 的版本是多少?谢谢。

最佳答案

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

This is what i have implimented in my project even i had same problem.Hope this helps! @Jake

关于C# - Resources.GetDrawable 在 Android Xamarin 中已过时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39546964/

相关文章:

java - 从 Android 中的 FragmentActivity 类访问 Fragment 类方法

c# - MVC3 使用 C# 可选参数做什么?

c# - 如何制作采用泛型类型的泛型方法

java - UTF-8 的字符串构造函数是否已损坏?

Android:我无法制作collapseActionView

java - 连接到 Android 中的数据库时表中缺少字段

android - 如何从 EditText Android 获取值

c# - 如何在 C# 中提取字节的第 5 位

c# - 使用 ffmpeg、node.js 和 socket.io 将 Flv 流式传输到套接字

javascript - AVG(COLUMN_SCORE) 未生成平均值