Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Why i can't use this on switch case? Please help

ricardoventuraricardoventura Member ✭✭
edited May 2019 in Xamarin.Forms

Basically, i have a MasterDetailPage where i have a few items with a listview (programmaly). And... on every entrys i have a TextChanged where use a variable which is type bool called "certeza". The purpose of all of that is for asking to the user if he/she wants to leave without saving it. So i did this:

private async void View_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
Item itens = e.SelectedItem as Item;
switch (itens.Text)
{
case "Abastecimento":
if (App.certeza == true)
{
var response1 = await DisplayAlert("Leaving", "Do you want to leave without saving?", "Yes", "No");
if (response1)
{
Detail = new NavigationPage(new Abastecimento());
App.certeza = false;
}
}
else
Detail = new NavigationPage(new Abastecimento());
brake;
}

With these code, if i press "No" on DisplayAlert, i cannot open that page anymore if i don't open another one. The problem is that "break". What i tried to do was:

      switch (itens.Text)
      {
          case "Abastecimento":
              if (App.certeza == true)
              {
                  var response1 = await DisplayAlert("Leaving", "Do you want to leave without saving?", "Yes", "No");
                  if (response1)
                  {
                      Detail = new NavigationPage(new Abastecimento());
                      App.certeza = false;
                      break;
                  }
              }
              else
              {
                  Detail = new NavigationPage(new Abastecimento());
                  brake;
              }

(What i want to do is: ) If i press "No", the switch does not break and when i want to press the page i previously selected, the page will be available and ready to open.

Posts

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Just deselect the item in the ListView at the end of the method.

    private async void View_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        Item itens = e.SelectedItem as Item;
    
        if (itens == null)
        {
            return;
        }
    
        switch (itens.Text)
        {
            // blah blah blah
        }
    
        ((ListView)sender).SelectedItem = null;
    }
    
  • ricardoventuraricardoventura Member ✭✭

    @JoeManke said:
    Just deselect the item in the ListView at the end of the method.

    private async void View_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        Item itens = e.SelectedItem as Item;
        
        if (itens == null)
        {
            return;
        }
    
        switch (itens.Text)
        {
            // blah blah blah
        }
    
        ((ListView)sender).SelectedItem = null;
    }
    

    "System.NullReferenceException: Object reference not set to an instance of an object."
    I tried aswell using in a middle of the switch, like:

    private async void View_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
       Item itens = e.SelectedItem as Item;
      switch (itens.Text)
              {
                  case "Abastecimento":
                      if (App.certeza == 1)
                      {
                          var response1 = await DisplayAlert("Sair sem guardar", "Deseja sair sem guardar?", "Sim", "Não");
                          if (response1)
                          {
                              Detail = new NavigationPage(new Abastecimento());
                              App.certeza = 0;
                          }
                          else
                              ((ListView)sender).SelectedItem = null;
                      }
                      else
                          Detail = new NavigationPage(new Abastecimento());
                      break;
              }
    }
    

    BUT, didn't work aswell. I got the same error... But thanks for the replying! Any else solution?

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    This part is very important, and you left it out:

    if (itens == null)
    {
        return;
    }
    

    When you reset the ListView.SelectedItem to null, the event will fire again with e.SelectedItem being null.

  • ricardoventuraricardoventura Member ✭✭

    @JoeManke said:
    This part is very important, and you left it out:

    if (itens == null)
    {
        return;
    }
    

    When you reset the ListView.SelectedItem to null, the event will fire again with e.SelectedItem being null.

    Ohhh my f*king bad, didnt' see it. That was the solution, thank you!! Much love!

Sign In or Register to comment.