Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Java嵌套类转换为Xamarin.Android_Java_Android_Xamarin.android - Fatal编程技术网

将Java嵌套类转换为Xamarin.Android

将Java嵌套类转换为Xamarin.Android,java,android,xamarin.android,Java,Android,Xamarin.android,Android/Java开发中的一种典型模式是使用嵌套类从父类访问方法: public class MainActivity extends FragmentActivity { // implementation public class SectionsPagerAdapter extends FragmentPagerAdapter { String someString = getString(R.string.thestring); } } 因此,在本例中,C

Android/Java开发中的一种典型模式是使用嵌套类从父类访问方法:

public class MainActivity extends FragmentActivity {

  // implementation

  public class SectionsPagerAdapter extends FragmentPagerAdapter {
    String someString = getString(R.string.thestring);
  }
}
因此,在本例中,Context.getString将访问MainActivity上下文。 这将如何转换为Xamarin.Android Mono for Android? 尝试完全相同的图案时,我得到:

错误CS0038:无法访问外部类型的非静态成员 Test.MainActivity“通过嵌套类型 测试。主要活动。章节SPAGERADAPTER'CS0038

我当然可以传递一个上下文对象,但这样做似乎很乏味。

根据Xamarin.Android的文档:

非静态嵌套类(也称为内部类)有很大的不同。它们包含对其封闭类型实例的隐式引用,并且不能包含静态成员以及本概述范围之外的其他差异

因此,您应该将MainActivity的引用传递给SectionsPagerAdapter。然后,您可以访问MainActivity的成员。

根据Xamarin.Android的文档:

非静态嵌套类(也称为内部类)有很大的不同。它们包含对其封闭类型实例的隐式引用,并且不能包含静态成员以及本概述范围之外的其他差异


因此,您应该将MainActivity的引用传递给SectionsPagerAdapter。然后,您可以访问MainActivity的成员。

这里是一个示例。我希望有帮助

public class YourParentClassActivity : Activity
{
//your stuff 
//...


//The Nested Class (that can implement any interface or base class)
    class YourNextedExampleClass : WebViewClient
    {
        //Parent Class Reference
        private YourParentClassActivity _pc;
        public YourNextedExampleClass(YourParentClassActivity pc)
        {
            _pc = pc;
        }

        public override void OnReceivedSslError(WebView view, SslErrorHandler handler, SslError SSLError)
        {
            Intent i = new Intent(Intent.ActionView, Android.Net.Uri.Parse("xxx");
            _pc.StartActivity(i);
            _pc.Finish();
            handler.Proceed(); // Ignore SSL certificate errors
        }
    }

}

这里有一个例子。我希望有帮助

public class YourParentClassActivity : Activity
{
//your stuff 
//...


//The Nested Class (that can implement any interface or base class)
    class YourNextedExampleClass : WebViewClient
    {
        //Parent Class Reference
        private YourParentClassActivity _pc;
        public YourNextedExampleClass(YourParentClassActivity pc)
        {
            _pc = pc;
        }

        public override void OnReceivedSslError(WebView view, SslErrorHandler handler, SslError SSLError)
        {
            Intent i = new Intent(Intent.ActionView, Android.Net.Uri.Parse("xxx");
            _pc.StartActivity(i);
            _pc.Finish();
            handler.Proceed(); // Ignore SSL certificate errors
        }
    }

}

我总是提取嵌套类并在构造函数中传递活动。。。这就解释了。把这个写下来作为答案,我会接受的,汉克斯。你能帮我个忙吗?你能建议标记xamarin.android是monodroid的同义词吗?我不能这样做,因为我的声誉低于2.5公里我总是提取嵌套类并在构造函数中传递活动。。。这就解释了。把这个写下来作为答案,我会接受的,汉克斯。你能帮我个忙吗?你能建议标记xamarin.android是monodroid的同义词吗?我不能这样做,因为我的声誉低于2.5公里请你写一个例子好吗?