scoots
scoots

Reputation: 23

How to use the back function in UWP apps to change visibility of a grid?

I would like to use the back function in the UWP in the App.xaml.cs file to change the visibility property of a grid (grid1) on the MainPage.xaml file.

//Go Back
        public void App_BackRequested(object sender, Windows.UI.Core.BackRequestedEventArgs e)
        {
            if(MainPage.MyGlobals.pageLocation == 0)
            {
                //Do Nothing
            }

            else if(MainPage.MyGlobals.pageLocation == 1)
            {
                MainPage.grid1.Visibility = Visibility.Collapsed;

                MainPage.MyGlobals.pageLocation = 0;
            }
        }

I know it's not typical practice to change xaml elements' properties from a different page, but I would really like to change how the back feature works in this app. I believe I have to make the grid pubic, but even when I (thought I) found a way to do that I still couldn't change the properties of the grid with the way I have it written in my code.

Upvotes: 1

Views: 421

Answers (2)

Jay Zuo
Jay Zuo

Reputation: 15758

As you've mentioned, changing XAML elements' properties from a different page is not a good practice. We can just use SystemNavigationManager.BackRequested event in MainPage like following to change the visibility of a grid.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    SystemNavigationManager.GetForCurrentView().BackRequested += Page_BackRequested;
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    SystemNavigationManager.GetForCurrentView().BackRequested -= Page_BackRequested;
}

private void Page_BackRequested(object sender, BackRequestedEventArgs e)
{
    if (MyGrid.Visibility == Visibility.Visible)
    {
        MyGrid.Visibility = Visibility.Collapsed;
        e.Handled = true;
    }
    else
    {
        if (this.Frame.CanGoBack)
        {
            this.Frame.GoBack();
            e.Handled = true;
        }
    }
}

And if you want to take advantage of the back function in App.xaml.cs and use a different function in MainPage, you can add a event in App that wraps SystemNavigationManager.BackRequested to allow other pages to override the default behavior by subscribing to this event like following:

public event EventHandler<BackRequestedEventArgs> BackRequested;

private void App_BackRequested(object sender, BackRequestedEventArgs e)
{
    BackRequested?.Invoke(sender, e);

    Frame rootFrame = Window.Current.Content as Frame;

    if (!e.Handled && rootFrame != null && rootFrame.CanGoBack)
    {
        rootFrame.GoBack();
        e.Handled = true;
    }
}

Then in MainPage, subscribe and unsubscribe this event like:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    (Application.Current as App).BackRequested += Page_BackRequested;
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    (Application.Current as App).BackRequested -= Page_BackRequested;
}

Upvotes: 0

WPMed
WPMed

Reputation: 1414

It doesn't work because you're trying to access the Grid as if it was a static property of the MainPage. You need a reference to the MainPage instance to manipulate the Grid, but all in all it is a really bad practice. You should have a look at the UWP/WP 8.1 navigation events.

Upvotes: 0

Related Questions