C# 如何在Windows Phone中获取父容器,例如PhoneApplicationPage?

C# 如何在Windows Phone中获取父容器,例如PhoneApplicationPage?,c#,windows-phone-8,windows-phone,C#,Windows Phone 8,Windows Phone,在子控件中处理事件时,我需要在MainPage.xaml.cs类中调用一个方法。调用this.Parent无效,因为它只返回树中的第一个DependencyObject,我无法从中获取PhoneApplicationPage 我的电话应用程序页面中有以下布局: <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Hei

在子控件中处理事件时,我需要在MainPage.xaml.cs类中调用一个方法。调用
this.Parent
无效,因为它只返回树中的第一个DependencyObject,我无法从中获取
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>
当点击事件发生时,我需要将
gridContent
的内容替换为与用户点击相对应的内容。以下是我处理点击事件的方式:

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;
        }

    }
}
private void TabItem_轻触(对象发送者,System.Windows.Input.GestureEventArgs e)
{
//var parent=this.parent;//这样做了:

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

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;
        }
    }
}
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;
        }
    }
}