0
votes

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

2 Answers

0
votes

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

0
votes

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

Application.Current.MainPage.DisplayAlert();