Search code examples
androidxamarinandroid-preferences

Adding a preferences fragment in my xamarin/visualstudio project


Very new in android programming... I've created a simple priject just to learn. Trying to add a preferences screen following this guide : When I declare my Preferences class inside the namespace as follows:

 public class MySettingsFragment extends PreferenceFragment
    {

        @Override
        public void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Load the preferences from an XML resource
        AddPreferencesFromResource(Resource.Xml.preferences);
    }

I get errors such as "extends" cannot be found and many other saying it is not the right context where to write it what I am missing?


Solution

  • It seems that you are mixing Java and C# syntax, in C# you use a colon instead of the word 'extends' to inherit a class, and the 'override' modifier is used instead of '@Override'

    Your code would end up looking like this

     public class MySettingsFragment : PreferenceFragment
     {
    
    
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
            // Load the preferences from an XML resource
            AddPreferencesFromResource(Resource.Xml.preferences);
        }
    }