Xamarin Forms Conditional statements












0















Hello guys: Am stuck with my code here. There is a point in my app where I need to notify users to register and sign in order to save their preferences. I have been trying to add a display alert inside a ViewModel but it's not working. Please help I am stuck.



ApproveViewModel



    namespace MyApp.ViewModels
{
public class ApproveViewModel
{
private DataService dataService = new DataService();
public Oppotunity SelectedOppotunity { get; set; }


public ICommand SaveCommand => new Command(async () =>
{

await dataService.PostSaveOppotunity(SelectedOppotunity, Settings.AccessToken);

});

public ApproveViewModel()
{
SelectedOppotunity = new Oppotunity();
}

}
}


ApprovePage.xaml



<ScrollView>
<StackLayout Padding ="15">

<Label Text ="{Binding SelectedOppotunity.Title}"/>
<Label Text ="{Binding SelectedOppotunity.Description }"/>
<Label Text ="{Binding SelectedOppotunity.Organisation}"/>
<Label Text ="{Binding SelectedOppotunity.Venue }"/>
<Label Text ="{Binding SelectedOppotunity.Eligibility}"/>
<Label Text ="{Binding SelectedOppotunity.Benefits}"/>
<Label Text ="{Binding SelectedOppotunity.Province}"/>
<Label Text ="{Binding SelectedOppotunity.Country}"/>
<Label Text ="{Binding SelectedOppotunity.OppotunityLink}"/>
<Label Text ="{Binding SelectedOppotunity.Category}"/>
<Label Text ="{Binding SelectedOppotunity.Deadline}"/>
<!--
<Switch IsToggled ="{Binding SelectedOppotunity.IsApproved}"></Switch>
-->

<Button Text ="Apply" BackgroundColor ="#A91717" TextColor ="White"
Command ="{Binding SaveCommand }"/>


</StackLayout>





The code i wish to invoke on saving:



 if (!string.IsNullOrEmpty(Settings.AccessToken))
{
// Implement the SaveCommand from the ViewModel;
}
// Go to Login form to get an access token
else if (!string.IsNullOrEmpty(Settings.Username) &&
!string.IsNullOrEmpty(Settings.Password))
{
MainPage = new NavigationPage(new Login());
}

else
{
//Register first
MainPage = new NavigationPage(new NewRegisterPage());
}









share|improve this question





























    0















    Hello guys: Am stuck with my code here. There is a point in my app where I need to notify users to register and sign in order to save their preferences. I have been trying to add a display alert inside a ViewModel but it's not working. Please help I am stuck.



    ApproveViewModel



        namespace MyApp.ViewModels
    {
    public class ApproveViewModel
    {
    private DataService dataService = new DataService();
    public Oppotunity SelectedOppotunity { get; set; }


    public ICommand SaveCommand => new Command(async () =>
    {

    await dataService.PostSaveOppotunity(SelectedOppotunity, Settings.AccessToken);

    });

    public ApproveViewModel()
    {
    SelectedOppotunity = new Oppotunity();
    }

    }
    }


    ApprovePage.xaml



    <ScrollView>
    <StackLayout Padding ="15">

    <Label Text ="{Binding SelectedOppotunity.Title}"/>
    <Label Text ="{Binding SelectedOppotunity.Description }"/>
    <Label Text ="{Binding SelectedOppotunity.Organisation}"/>
    <Label Text ="{Binding SelectedOppotunity.Venue }"/>
    <Label Text ="{Binding SelectedOppotunity.Eligibility}"/>
    <Label Text ="{Binding SelectedOppotunity.Benefits}"/>
    <Label Text ="{Binding SelectedOppotunity.Province}"/>
    <Label Text ="{Binding SelectedOppotunity.Country}"/>
    <Label Text ="{Binding SelectedOppotunity.OppotunityLink}"/>
    <Label Text ="{Binding SelectedOppotunity.Category}"/>
    <Label Text ="{Binding SelectedOppotunity.Deadline}"/>
    <!--
    <Switch IsToggled ="{Binding SelectedOppotunity.IsApproved}"></Switch>
    -->

    <Button Text ="Apply" BackgroundColor ="#A91717" TextColor ="White"
    Command ="{Binding SaveCommand }"/>


    </StackLayout>





    The code i wish to invoke on saving:



     if (!string.IsNullOrEmpty(Settings.AccessToken))
    {
    // Implement the SaveCommand from the ViewModel;
    }
    // Go to Login form to get an access token
    else if (!string.IsNullOrEmpty(Settings.Username) &&
    !string.IsNullOrEmpty(Settings.Password))
    {
    MainPage = new NavigationPage(new Login());
    }

    else
    {
    //Register first
    MainPage = new NavigationPage(new NewRegisterPage());
    }









    share|improve this question



























      0












      0








      0








      Hello guys: Am stuck with my code here. There is a point in my app where I need to notify users to register and sign in order to save their preferences. I have been trying to add a display alert inside a ViewModel but it's not working. Please help I am stuck.



      ApproveViewModel



          namespace MyApp.ViewModels
      {
      public class ApproveViewModel
      {
      private DataService dataService = new DataService();
      public Oppotunity SelectedOppotunity { get; set; }


      public ICommand SaveCommand => new Command(async () =>
      {

      await dataService.PostSaveOppotunity(SelectedOppotunity, Settings.AccessToken);

      });

      public ApproveViewModel()
      {
      SelectedOppotunity = new Oppotunity();
      }

      }
      }


      ApprovePage.xaml



      <ScrollView>
      <StackLayout Padding ="15">

      <Label Text ="{Binding SelectedOppotunity.Title}"/>
      <Label Text ="{Binding SelectedOppotunity.Description }"/>
      <Label Text ="{Binding SelectedOppotunity.Organisation}"/>
      <Label Text ="{Binding SelectedOppotunity.Venue }"/>
      <Label Text ="{Binding SelectedOppotunity.Eligibility}"/>
      <Label Text ="{Binding SelectedOppotunity.Benefits}"/>
      <Label Text ="{Binding SelectedOppotunity.Province}"/>
      <Label Text ="{Binding SelectedOppotunity.Country}"/>
      <Label Text ="{Binding SelectedOppotunity.OppotunityLink}"/>
      <Label Text ="{Binding SelectedOppotunity.Category}"/>
      <Label Text ="{Binding SelectedOppotunity.Deadline}"/>
      <!--
      <Switch IsToggled ="{Binding SelectedOppotunity.IsApproved}"></Switch>
      -->

      <Button Text ="Apply" BackgroundColor ="#A91717" TextColor ="White"
      Command ="{Binding SaveCommand }"/>


      </StackLayout>





      The code i wish to invoke on saving:



       if (!string.IsNullOrEmpty(Settings.AccessToken))
      {
      // Implement the SaveCommand from the ViewModel;
      }
      // Go to Login form to get an access token
      else if (!string.IsNullOrEmpty(Settings.Username) &&
      !string.IsNullOrEmpty(Settings.Password))
      {
      MainPage = new NavigationPage(new Login());
      }

      else
      {
      //Register first
      MainPage = new NavigationPage(new NewRegisterPage());
      }









      share|improve this question
















      Hello guys: Am stuck with my code here. There is a point in my app where I need to notify users to register and sign in order to save their preferences. I have been trying to add a display alert inside a ViewModel but it's not working. Please help I am stuck.



      ApproveViewModel



          namespace MyApp.ViewModels
      {
      public class ApproveViewModel
      {
      private DataService dataService = new DataService();
      public Oppotunity SelectedOppotunity { get; set; }


      public ICommand SaveCommand => new Command(async () =>
      {

      await dataService.PostSaveOppotunity(SelectedOppotunity, Settings.AccessToken);

      });

      public ApproveViewModel()
      {
      SelectedOppotunity = new Oppotunity();
      }

      }
      }


      ApprovePage.xaml



      <ScrollView>
      <StackLayout Padding ="15">

      <Label Text ="{Binding SelectedOppotunity.Title}"/>
      <Label Text ="{Binding SelectedOppotunity.Description }"/>
      <Label Text ="{Binding SelectedOppotunity.Organisation}"/>
      <Label Text ="{Binding SelectedOppotunity.Venue }"/>
      <Label Text ="{Binding SelectedOppotunity.Eligibility}"/>
      <Label Text ="{Binding SelectedOppotunity.Benefits}"/>
      <Label Text ="{Binding SelectedOppotunity.Province}"/>
      <Label Text ="{Binding SelectedOppotunity.Country}"/>
      <Label Text ="{Binding SelectedOppotunity.OppotunityLink}"/>
      <Label Text ="{Binding SelectedOppotunity.Category}"/>
      <Label Text ="{Binding SelectedOppotunity.Deadline}"/>
      <!--
      <Switch IsToggled ="{Binding SelectedOppotunity.IsApproved}"></Switch>
      -->

      <Button Text ="Apply" BackgroundColor ="#A91717" TextColor ="White"
      Command ="{Binding SaveCommand }"/>


      </StackLayout>





      The code i wish to invoke on saving:



       if (!string.IsNullOrEmpty(Settings.AccessToken))
      {
      // Implement the SaveCommand from the ViewModel;
      }
      // Go to Login form to get an access token
      else if (!string.IsNullOrEmpty(Settings.Username) &&
      !string.IsNullOrEmpty(Settings.Password))
      {
      MainPage = new NavigationPage(new Login());
      }

      else
      {
      //Register first
      MainPage = new NavigationPage(new NewRegisterPage());
      }






      asp.net-web-api xamarin.forms






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 16:42









      Adam

      65321122




      65321122










      asked Nov 21 '18 at 13:57









      Absolom KanyeAbsolom Kanye

      31




      31
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Solution:



          You can use MessageCenter subscriptions to work through this problem.




          Xamarin.Forms MessagingCenter enables view models and other components
          to communicate with without having to know anything about each other
          besides a simple Message contract.



          Subscribe - Listen for messages with a certain signature and perform
          some action when they are received. Multiple subscribers can be
          listening for the same message.



          Send - Publish a message for listeners to act upon. If no
          listeners have subscribed then the message is ignored.




          Code in the View Model:



          if (!string.IsNullOrEmpty(Settings.AccessToken))
          {
          // Implement the SaveCommand from the ViewModel;
          }
          // Go to Login form to get an access token
          else if (!string.IsNullOrEmpty(Settings.Username) &&
          !string.IsNullOrEmpty(Settings.Password))
          {
          MainPage = new NavigationPage(new Login());
          }
          else
          {
          //Register first

          //if you want to notify users to register here, use MessageCenter.Send
          MessageCenter.Send(this, "displayAlert")

          MainPage = new NavigationPage(new NewRegisterPage());
          }


          Code in the View:



          MessagingCenter.Subscribe<ViewModelName>(this, "displayAlert", (sender) => {
          // do something whenever the "displayAlert" message is sent

          DisplayAlert("notification", "you should register first", "ok");
          });


          Send the message where you want to display an alert in the viewModel.



          For more information about MessageCenter, you can refer to https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center






          share|improve this answer


























          • I still dont understand please can you clarify.

            – Absolom Kanye
            Nov 22 '18 at 8:51











          • @AbsolomKanye See my edit, reply me if you have any question.

            – Jack Hua - MSFT
            Nov 22 '18 at 9:07



















          0














          If you wan't to display an alert from the ViewModel you can:



          Application.Current.MainPage.DisplayAlert();





          share|improve this answer
























          • I hope i would become that much of help to beginners like me oneday thank you.

            – Absolom Kanye
            Nov 22 '18 at 10:24











          • @AbsolomKanye don't forget to mark the answer as correct ;:) Glad I could help

            – Bruno Caceiro
            Nov 22 '18 at 11:53











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413687%2fxamarin-forms-conditional-statements%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Solution:



          You can use MessageCenter subscriptions to work through this problem.




          Xamarin.Forms MessagingCenter enables view models and other components
          to communicate with without having to know anything about each other
          besides a simple Message contract.



          Subscribe - Listen for messages with a certain signature and perform
          some action when they are received. Multiple subscribers can be
          listening for the same message.



          Send - Publish a message for listeners to act upon. If no
          listeners have subscribed then the message is ignored.




          Code in the View Model:



          if (!string.IsNullOrEmpty(Settings.AccessToken))
          {
          // Implement the SaveCommand from the ViewModel;
          }
          // Go to Login form to get an access token
          else if (!string.IsNullOrEmpty(Settings.Username) &&
          !string.IsNullOrEmpty(Settings.Password))
          {
          MainPage = new NavigationPage(new Login());
          }
          else
          {
          //Register first

          //if you want to notify users to register here, use MessageCenter.Send
          MessageCenter.Send(this, "displayAlert")

          MainPage = new NavigationPage(new NewRegisterPage());
          }


          Code in the View:



          MessagingCenter.Subscribe<ViewModelName>(this, "displayAlert", (sender) => {
          // do something whenever the "displayAlert" message is sent

          DisplayAlert("notification", "you should register first", "ok");
          });


          Send the message where you want to display an alert in the viewModel.



          For more information about MessageCenter, you can refer to https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center






          share|improve this answer


























          • I still dont understand please can you clarify.

            – Absolom Kanye
            Nov 22 '18 at 8:51











          • @AbsolomKanye See my edit, reply me if you have any question.

            – Jack Hua - MSFT
            Nov 22 '18 at 9:07
















          0














          Solution:



          You can use MessageCenter subscriptions to work through this problem.




          Xamarin.Forms MessagingCenter enables view models and other components
          to communicate with without having to know anything about each other
          besides a simple Message contract.



          Subscribe - Listen for messages with a certain signature and perform
          some action when they are received. Multiple subscribers can be
          listening for the same message.



          Send - Publish a message for listeners to act upon. If no
          listeners have subscribed then the message is ignored.




          Code in the View Model:



          if (!string.IsNullOrEmpty(Settings.AccessToken))
          {
          // Implement the SaveCommand from the ViewModel;
          }
          // Go to Login form to get an access token
          else if (!string.IsNullOrEmpty(Settings.Username) &&
          !string.IsNullOrEmpty(Settings.Password))
          {
          MainPage = new NavigationPage(new Login());
          }
          else
          {
          //Register first

          //if you want to notify users to register here, use MessageCenter.Send
          MessageCenter.Send(this, "displayAlert")

          MainPage = new NavigationPage(new NewRegisterPage());
          }


          Code in the View:



          MessagingCenter.Subscribe<ViewModelName>(this, "displayAlert", (sender) => {
          // do something whenever the "displayAlert" message is sent

          DisplayAlert("notification", "you should register first", "ok");
          });


          Send the message where you want to display an alert in the viewModel.



          For more information about MessageCenter, you can refer to https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center






          share|improve this answer


























          • I still dont understand please can you clarify.

            – Absolom Kanye
            Nov 22 '18 at 8:51











          • @AbsolomKanye See my edit, reply me if you have any question.

            – Jack Hua - MSFT
            Nov 22 '18 at 9:07














          0












          0








          0







          Solution:



          You can use MessageCenter subscriptions to work through this problem.




          Xamarin.Forms MessagingCenter enables view models and other components
          to communicate with without having to know anything about each other
          besides a simple Message contract.



          Subscribe - Listen for messages with a certain signature and perform
          some action when they are received. Multiple subscribers can be
          listening for the same message.



          Send - Publish a message for listeners to act upon. If no
          listeners have subscribed then the message is ignored.




          Code in the View Model:



          if (!string.IsNullOrEmpty(Settings.AccessToken))
          {
          // Implement the SaveCommand from the ViewModel;
          }
          // Go to Login form to get an access token
          else if (!string.IsNullOrEmpty(Settings.Username) &&
          !string.IsNullOrEmpty(Settings.Password))
          {
          MainPage = new NavigationPage(new Login());
          }
          else
          {
          //Register first

          //if you want to notify users to register here, use MessageCenter.Send
          MessageCenter.Send(this, "displayAlert")

          MainPage = new NavigationPage(new NewRegisterPage());
          }


          Code in the View:



          MessagingCenter.Subscribe<ViewModelName>(this, "displayAlert", (sender) => {
          // do something whenever the "displayAlert" message is sent

          DisplayAlert("notification", "you should register first", "ok");
          });


          Send the message where you want to display an alert in the viewModel.



          For more information about MessageCenter, you can refer to https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center






          share|improve this answer















          Solution:



          You can use MessageCenter subscriptions to work through this problem.




          Xamarin.Forms MessagingCenter enables view models and other components
          to communicate with without having to know anything about each other
          besides a simple Message contract.



          Subscribe - Listen for messages with a certain signature and perform
          some action when they are received. Multiple subscribers can be
          listening for the same message.



          Send - Publish a message for listeners to act upon. If no
          listeners have subscribed then the message is ignored.




          Code in the View Model:



          if (!string.IsNullOrEmpty(Settings.AccessToken))
          {
          // Implement the SaveCommand from the ViewModel;
          }
          // Go to Login form to get an access token
          else if (!string.IsNullOrEmpty(Settings.Username) &&
          !string.IsNullOrEmpty(Settings.Password))
          {
          MainPage = new NavigationPage(new Login());
          }
          else
          {
          //Register first

          //if you want to notify users to register here, use MessageCenter.Send
          MessageCenter.Send(this, "displayAlert")

          MainPage = new NavigationPage(new NewRegisterPage());
          }


          Code in the View:



          MessagingCenter.Subscribe<ViewModelName>(this, "displayAlert", (sender) => {
          // do something whenever the "displayAlert" message is sent

          DisplayAlert("notification", "you should register first", "ok");
          });


          Send the message where you want to display an alert in the viewModel.



          For more information about MessageCenter, you can refer to https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 9:07

























          answered Nov 22 '18 at 7:59









          Jack Hua - MSFTJack Hua - MSFT

          78717




          78717













          • I still dont understand please can you clarify.

            – Absolom Kanye
            Nov 22 '18 at 8:51











          • @AbsolomKanye See my edit, reply me if you have any question.

            – Jack Hua - MSFT
            Nov 22 '18 at 9:07



















          • I still dont understand please can you clarify.

            – Absolom Kanye
            Nov 22 '18 at 8:51











          • @AbsolomKanye See my edit, reply me if you have any question.

            – Jack Hua - MSFT
            Nov 22 '18 at 9:07

















          I still dont understand please can you clarify.

          – Absolom Kanye
          Nov 22 '18 at 8:51





          I still dont understand please can you clarify.

          – Absolom Kanye
          Nov 22 '18 at 8:51













          @AbsolomKanye See my edit, reply me if you have any question.

          – Jack Hua - MSFT
          Nov 22 '18 at 9:07





          @AbsolomKanye See my edit, reply me if you have any question.

          – Jack Hua - MSFT
          Nov 22 '18 at 9:07













          0














          If you wan't to display an alert from the ViewModel you can:



          Application.Current.MainPage.DisplayAlert();





          share|improve this answer
























          • I hope i would become that much of help to beginners like me oneday thank you.

            – Absolom Kanye
            Nov 22 '18 at 10:24











          • @AbsolomKanye don't forget to mark the answer as correct ;:) Glad I could help

            – Bruno Caceiro
            Nov 22 '18 at 11:53
















          0














          If you wan't to display an alert from the ViewModel you can:



          Application.Current.MainPage.DisplayAlert();





          share|improve this answer
























          • I hope i would become that much of help to beginners like me oneday thank you.

            – Absolom Kanye
            Nov 22 '18 at 10:24











          • @AbsolomKanye don't forget to mark the answer as correct ;:) Glad I could help

            – Bruno Caceiro
            Nov 22 '18 at 11:53














          0












          0








          0







          If you wan't to display an alert from the ViewModel you can:



          Application.Current.MainPage.DisplayAlert();





          share|improve this answer













          If you wan't to display an alert from the ViewModel you can:



          Application.Current.MainPage.DisplayAlert();






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 14:01









          Bruno CaceiroBruno Caceiro

          3,12811329




          3,12811329













          • I hope i would become that much of help to beginners like me oneday thank you.

            – Absolom Kanye
            Nov 22 '18 at 10:24











          • @AbsolomKanye don't forget to mark the answer as correct ;:) Glad I could help

            – Bruno Caceiro
            Nov 22 '18 at 11:53



















          • I hope i would become that much of help to beginners like me oneday thank you.

            – Absolom Kanye
            Nov 22 '18 at 10:24











          • @AbsolomKanye don't forget to mark the answer as correct ;:) Glad I could help

            – Bruno Caceiro
            Nov 22 '18 at 11:53

















          I hope i would become that much of help to beginners like me oneday thank you.

          – Absolom Kanye
          Nov 22 '18 at 10:24





          I hope i would become that much of help to beginners like me oneday thank you.

          – Absolom Kanye
          Nov 22 '18 at 10:24













          @AbsolomKanye don't forget to mark the answer as correct ;:) Glad I could help

          – Bruno Caceiro
          Nov 22 '18 at 11:53





          @AbsolomKanye don't forget to mark the answer as correct ;:) Glad I could help

          – Bruno Caceiro
          Nov 22 '18 at 11:53


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413687%2fxamarin-forms-conditional-statements%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ottavio Pratesi

          Tricia Helfer

          15 giugno