Return and Close

Jassim Al Rahma 1,516 Reputation points
2022-09-16T21:20:39.287+00:00

Hi,

I have the below button in MAUI Class library that should return a Person class and close the current page but when I try it I get

Unreachable code detected

Here is the code:

private List<User> Success_Clicked(System.Object sender, System.EventArgs e)  
{  
    Routing.RegisterRoute("monkeydetails", typeof(Signin));  
  
    List<User> usr = new List<User>();  
  
    usr.Add(new User()  
    {  
        FirstName = "",  
        LastName = ""  
    });  
  
    return usr;  
  
    Shell.Current.GoToAsync("..");  
}  
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,747 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,098 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. MahamatHammat-5360 1 Reputation point
    2022-09-17T11:42:25.16+00:00

    Hi Jassmin

    You can't execute any code after you [return], basically you have moved out of the scope of Success_Clicked and that is what the error is trying to tell you (Unreachable code detected). this code Shell.Current.GoToAsync(".."); will never be reached

    hope this will help


  2. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 33,381 Reputation points Microsoft Vendor
    2022-09-22T07:19:18.103+00:00

    Hello,

    It's no possible to return List<User> and close the page in the Success_Clicked method.

    You could use a static helper class to store the object so that external calls are made.

    You could refer to the following code:

    The helper class:

       public static class UserHelper  
       {  
           public static List<User> users;  
       }  
    

    Success_Clicked method:

       ...  
       UserHelper.users = new List<User>();  
       UserHelper.users.Add(new User()  
       {  
           FirstName = "test",  
           LastName = "test"  
       });  
       Shell.Current.GoToAsync("..");  
    

    Then, you could change MyClass.Current.Signin() to MyClass.UserHelper.users to call it.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. MahamatHammat-5360 1 Reputation point
    2022-09-23T04:11:12.173+00:00

    Hi, I see that you have a page in the class lib projecct. which i dont recomand. use the class lib project for

    1- Separation of concern" which will make it easy to modify or test your application without affecting any other part of your project
    2- Code reuse.

    having said that, i have played with your code and make it so it use the class to simulate the login action in the class library and put both the main page and the login page in the main project.

    the code is farely simple so yo can understand the idea. here is the code: https://github.com/Mhamitay/Maui_Simple_Login

    I hope that answer your question, and if it does, You know what to do ;)