question

JassimAlRahma-9056 avatar image
0 Votes"
JassimAlRahma-9056 asked MahamatHammat-5360 commented

Return and Close

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("..");
 }



dotnet-csharpdotnet-maui
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MahamatHammat-5360 avatar image
0 Votes"
MahamatHammat-5360 answered MahamatHammat-5360 commented

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

· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

then what is the solution here if I want to return data and close?

0 Votes 0 ·

I think it depends on the application type you are building, but in most cases you don’t need to close you just redirect to another page or to Home page if its a web application for example.

0 Votes 0 ·

it's a MAUI mobile app.

What I am thinking from my app is something like this:


 if (MyClass.Current.Signin())
 {
       get the return data.....
 }
0 Votes 0 ·

// Create the object
Person person= new Person
{
Name = "Hammad",
Age = 30,
Occupation = "Developer",
Country = "USA"
};

//navigate to another page, pass the person object

await Navigation.PushModalAsync(new AnotherPage(person));

0 Votes 0 ·

but the problem is my other page is not inside the app itself, it's a Class Library added as a reference in the pp

0 Votes 0 ·

Sooo. What is it you trying to close? I am confused.
Do you have it on github? Let me take a look at it.

0 Votes 0 ·

No it's not on github but here is a sample project:

https://www.bluefile.app/temp/AppwithClass.zip


0 Votes 0 ·
YonglunLiu-MSFT avatar image
0 Votes"
YonglunLiu-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MahamatHammat-5360 avatar image
0 Votes"
MahamatHammat-5360 answered MahamatHammat-5360 commented

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 ;)

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The sample is great but the problem is I don't want the user to do that from his app, I mean they will do the login and then they will do the sign it.. What I want from the app os single command signing() and I will check the status in the class then return the User or null.

0 Votes 0 ·
MahamatHammat-5360 avatar image MahamatHammat-5360 JassimAlRahma-9056 ·

Hi..

I realy want to help you but, sorry it is not clear what you trying to acheive.
Maybe i could help you if you can better explain it and put it in a simple usecase

good luck

0 Votes 0 ·