How can I make a property change from a different xaml Page with the use of MVVM?

I'm newbie in MVVM . I have two xaml pages. the second one cannot be accessed ( Locked ) unless the button from the first page (Introduction page) has been clicked. How can I do that?

The lock page has this code.

<Frame Grid.Row="0" Grid.Column="1" BackgroundColor="LightGray" IsVisible="{Binding LockPage}">
            <Frame.GestureRecognizers>
                <TapGestureRecognizer Tapped="Tap_Lock" />
            </Frame.GestureRecognizers>
            <Label Text="Locked"/>
        </Frame>

The Introduction page has this code.

<StackLayout>
    <Label Text="This is only a simple Introduction Text."/>
    <Label Text=""/>
    <Button Text="Lets Go!!" Command="{Binding UnlockPageCommand}" Clicked="Tap_Next"/>

This is the the class LockModule.cs

public class LockModule : INotifyPropertyChanged

{

public LockModule()
{
    UnlockPageCommand = new Command(UnlockPage);
}

bool lockPage = true;
public event PropertyChangedEventHandler PropertyChanged;

void OnPropertyChanged(string lockpage)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(lockpage));
}

public bool LockPage
{
    get { return lockPage; }
    set
    {
            lockPage = value;

        OnPropertyChanged(nameof(LockPage));
    }
}

public Command UnlockPageCommand { get; }

void UnlockPage()
{
    if (lockPage == true)
    {
        lockPage = false;
    }
    else
    {
        lockPage = true;
    }
    OnPropertyChanged(nameof(LockPage));
}

}

and it's not working

Tagged:

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    @fiveskie05 What do you mean

    the second one cannot be accessed ( Locked ) unless the button from the first page (Introduction page) has been clicked

    This button's click event will push the navigation stack to second page, and you want the Frame's visibility to be changed depending on the LockPage which changed in the UnlockPageCommand?
    If so, it seems you used the same view model type as these two pages' BindingContext. You need to pass the first page's view model to the second, make sure these two pages use the same instance of view model not only the same type.
    Your second page's constructor could be like this:

    public SecondPage(LockModule model)
    {
        InitializeComponent();
    
        BindingContext = model;
    }
    

    And the Tap_Next click event:

    private void Tap_Next(object sender, EventArgs e)
    {
        Navigation.PushAsync(new SecondPage(BindingContext as LockModule));
    }
    
Sign In or Register to comment.