Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

c# - Xamarin.Forms call method from another controller

I have a MasterDetailPage. In my master page I am calling the OnAppearing method like so:

protected override async void OnAppearing()
        {

            List<MasterPageItem> menu = new List<MasterPageItem>();

            var myValue = await SecureStorage.GetAsync("token");

            if(myValue == null)
            {
                menu.Add(new MasterPageItem
                {
                    Title = "Home",
                    TargetType = typeof(HomePage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Search Patients",
                    TargetType = typeof(PatientsPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Search Professionals",
                    TargetType = typeof(ProfessionalsPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Blog",
                    TargetType = typeof(BlogPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Register",
                    TargetType = typeof(RegistrationPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Login",
                    TargetType = typeof(LoginPage)
                });

            }
            else
            {
                menu.Add(new MasterPageItem
                {
                    Title = "Home",
                    TargetType = typeof(HomePage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Search Patients",
                    TargetType = typeof(PatientsPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Search Professionals",
                    TargetType = typeof(ProfessionalsPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Blog",
                    TargetType = typeof(BlogPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Messages",
                    TargetType = typeof(MailboxPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Account",
                    TargetType = typeof(AccountPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Logout",
                    TargetType = typeof(LoginPage)
                });
            }

            listView.ItemsSource = menu;

        }

How ever it only gets called once when I goto the master page for the first time. I am looking for away to refresh my listView when someone logins or logouts.

I have tried the following on my LoginPage

protected async void OnLogin(System.Object sender, System.EventArgs e)
        {
            LoginClass response = await webService.LoginAsync(Email.Text, Password.Text);

            var myValue = await SecureStorage.GetAsync("token");

            Console.WriteLine(myValue);

            if (response.error != null)
            {
                await DisplayAlert("Error", response.error, "OK");
            }
            else
            {
                await SecureStorage.SetAsync("token", response.token);

                MasterPage masterPage = new MasterPage();

                masterPage.createMenu();
            }

        }

and created a new method in my master page called createMenu and its being called, but my listView does not get updated and it should be the "token" has been created.

public async void createMenu()
        {
            List<MasterPageItem> menu = new List<MasterPageItem>();

            var myValue = await SecureStorage.GetAsync("token");

            if (myValue == null)
            {
                menu.Add(new MasterPageItem
                {
                    Title = "Home",
                    TargetType = typeof(HomePage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Search Patients",
                    TargetType = typeof(PatientsPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Search Professionals",
                    TargetType = typeof(ProfessionalsPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Blog",
                    TargetType = typeof(BlogPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Register",
                    TargetType = typeof(RegistrationPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Login",
                    TargetType = typeof(LoginPage)
                });

            }
            else
            {
                menu.Add(new MasterPageItem
                {
                    Title = "Home",
                    TargetType = typeof(HomePage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Search Patients",
                    TargetType = typeof(PatientsPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Search Professionals",
                    TargetType = typeof(ProfessionalsPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Blog",
                    TargetType = typeof(BlogPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Messages",
                    TargetType = typeof(MailboxPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Account",
                    TargetType = typeof(AccountPage)
                });

                menu.Add(new MasterPageItem
                {
                    Title = "Logout",
                    TargetType = typeof(LoginPage)
                });
            }

            listView.ItemsSource = menu;
        }

What am I doing wrong here? Or is there another way to go about this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...