App does not contain a definition for ...

jddjdd USMember ✭✭✭

I am very surprised.
I have added a local database as described in https://developer.xamarin.com/samples/xamarin-forms/Todo/

I added a public static property in my partial class App:

public partial class App : Application
    {
        static SettingsDatabase database;

        public App()...
        public static SettingsDatabase SettingsDB
        {
            get
            {
                if (database == null)
                {
                    database = new SettingsDatabase(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SettingsSQLite.db3"));
                }
                return database;
            }
        }

and I try to bind it to a list view like in the Todo sample:

    public partial class ConstantCorrectionsPage : ContentPage
    {
        public ConstantCorrectionsPage ()
        {
            InitializeComponent ();
        }
        protected override async void OnAppearing()
        {
            base.OnAppearing();

            listView.ItemsSource = await App.SettingsDB();
        }
    }

but I get the compile error:

'App' does not contain a definition for 'SettingDB'

??

Best Answer

  • jddjdd US ✭✭✭
    Accepted Answer

    Well, I moved the code to a public static class and it compiles...

        public static class InitializeSettingsDB
        {
            static SettingsDatabase db;
    
            public static SettingsDatabase SettingsDB
            {
                get
                {
                    if (db == null)
                    {
                        db = new SettingsDatabase(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SettingsSQLite.db3"));
                    }
                    return db;
                }
            }
    

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    to ItemSource you can set a List, or an ObservableCollection, not another object

  • jddjdd USMember ✭✭✭

    You are right, I tried to replace with method, back to property and forgot the method...
    This is the right code. I still meet the compilation error:

        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class ConstantCorrectionsPage : ContentPage
        {
            public ConstantCorrectionsPage ()
            {
                InitializeComponent ();
            }
            protected override async void OnAppearing()
            {
                base.OnAppearing();
                listView.ItemsSource = await App.SettingsDB.GetItemsAsync();
            }
        }
    

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    1- try to delete Bin and Obj directories, close and reopen the project, rebuild it
    2- Which is the GetItemsAsync() return type? It should return "List" or "ObservableCollection"

  • jddjdd USMember ✭✭✭
    Accepted Answer

    Well, I moved the code to a public static class and it compiles...

        public static class InitializeSettingsDB
        {
            static SettingsDatabase db;
    
            public static SettingsDatabase SettingsDB
            {
                get
                {
                    if (db == null)
                    {
                        db = new SettingsDatabase(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SettingsSQLite.db3"));
                    }
                    return db;
                }
            }
    
Sign In or Register to comment.