Android/Java 开发中的一个典型模式是有一个嵌套类从父类访问方法:
public class MainActivity extends FragmentActivity {
// implementation
public class SectionsPagerAdapter extends FragmentPagerAdapter {
String someString = getString(R.string.thestring);
}
}
所以在这种情况下,Context.getString()
将访问 MainActivity
上下文。
这将如何转换为 Xamarin.Android
(Android 的 Mono)?
当尝试完全相同的模式时,我得到:
Error CS0038: Cannot access a nonstatic member of outer type
Test.MainActivity' via nested type
Test.MainActivity.SectionsPagerAdapter' (CS0038)
我当然可以传递一个 Context 对象,但这似乎很乏味。
最佳答案
根据 API design Xamarin.Android 文档:
Non-static nested classes, also called inner classes, are significantly different. They contain an implicit reference to an instance of their enclosing type and cannot contain static members (among other differences outside the scope of this overview).
因此,您应该将 MainActivity
的引用传递给 SectionsPagerAdapter
。然后,您可以访问 MainActivity
的成员。
关于java - 将 Java 嵌套类转换为 Xamarin.Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15304904/