Navigation using MVVM without using any MVVM frameworks

whitewabbitwhitewabbit Member ✭✭
edited March 15 in Xamarin.Forms

I tried binding a button using command to navigate. it worked on Android but not in iOS. Please Help.
this is in my ViewModel

public LoginViewModel(INavigation navigation)
{
Navigation = navigation;
SignUpBtnClicked = new Command(async () => await SignUp());
}

public INavigation Navigation { get; set; }
public Command SignUpBtnClicked { get; }

public async Task SignUp()
{
    await Navigation.PushAsync(new UserOptionPage());
}

#region INotifyPropertyChanged 
public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion

Best Answer

  • whitewabbitwhitewabbit ✭✭
    Accepted Answer

    NVM, I found my problem. I enabled the Linker in iOS that's why it didn't hit the method.

    Solution
    It's either Disable the Linker in iOS Properties or put [Preserve] on the method.

Answers

  • AsurAsur USMember ✭✭✭
    edited March 15

    Technically it should work on both . Can you debug while running iOS and post the error message you get ?
    Also just for confirmation you are passing the navigation into the constructor from the xaml code behind file right ?

  • whitewabbitwhitewabbit Member ✭✭

    This is my code in my LoginPage View
    public partial class LoginPage : ContentPage
    {
    public LoginPage ()
    {
    NavigationPage.SetHasNavigationBar(this, false);
    InitializeComponent ();
    BindingContext = new LoginViewModel(Navigation);
    }
    }

  • whitewabbitwhitewabbit Member ✭✭

    And I don't get any error, so I tried to put a breakpoint on the SignUp() method and it doesn't hit.

  • whitewabbitwhitewabbit Member ✭✭
    Accepted Answer

    NVM, I found my problem. I enabled the Linker in iOS that's why it didn't hit the method.

    Solution
    It's either Disable the Linker in iOS Properties or put [Preserve] on the method.

Sign In or Register to comment.