DaveDev
DaveDev

Reputation: 42185

How can I get the parent container, e.g. PhoneApplicationPage, in Windows Phone?

I need to call a method in my MainPage.xaml.cs class when an event is handled in a child control. Calling this.Parent doesn't work, because it only returns the first DependencyObject in the tree, and I can't get the PhoneApplicationPage from that

I have the following layout in my PhoneApplicationPage:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="AUTO"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="AUTO"/>
    </Grid.RowDefinitions>

    <Grid Height="85" VerticalAlignment="Top" Grid.Row="0"></Grid>

    <Grid Grid.Row="1" Name="gridContent" />

    <ug:UniformGrid Rows="1" Columns="5" Height="85" VerticalAlignment="Bottom" Grid.Row="2">
        <tabs:TabItem Name="tabOverview" TabItemText="OVERVIEW" TabItemImage="overview_64.png" />
        <tabs:TabItem Name="tabLogs" TabItemText="LOGS" TabItemImage="log_64.png"/>           
    </ug:UniformGrid>
</Grid>

with the following code:

public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();

        gridContent.Children.Add(new OverviewUserControl());
    }

    public void UpdateContent(UserControl control)
    {
        // I need to call this method from the TabItem Tap event
        gridContent.Children.Clear();
        gridContent.Children.Add(control);
    }
}

When a tap event occurs, I need to replace the content of gridContent with whatever corresponds to the user's tap. this is how I handle the tap event:

private void TabItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{

    // var parent = this.Parent; //<-- this doesn't get the PhoneApplicationPage

    var ti = sender as TabItem;
    if (ti != null)
    {
        string tab = "";
        switch (ti.Name)
        {
            case "tabOverview":
                // I need a reference to MainPage here to call
                // MainPage.UpdateContent(new LogsUserControl())
                break;
            case "tabLogs":
                // I need a reference to MainPage here to call
                // MainPage.UpdateContent(new OverviewUserControl())
                break;
        }

    }
}

Question

So the question is how can I call a method in MainPage from TabItem_Tap?

Upvotes: 0

Views: 2071

Answers (1)

DaveDev
DaveDev

Reputation: 42185

This did it:

var currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content as MainPage;

Usage:

private void TabItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    var currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content as MainPage;

    if (ti != null)
    {
        switch (ti.Name)
        {
            case "tabOverview":
                currentPage.UpdateContent(new OverviewUserControl());
                break;
            case "tabLogs":
                currentPage.UpdateContent(new LogsUserControl());
                break;
        }
    }
}

Upvotes: 2

Related Questions