简体   繁体   English

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

[英]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. 当在子控件中处理事件时,我需要在我的MainPage.xaml.cs类中调用一个方法。 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 调用this.Parent不起作用,因为它只返回树中的第一个DependencyObject,而我无法从中获取PhoneApplicationPage

I have the following layout in my PhoneApplicationPage : 我在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. 当发生点击事件时,我需要将gridContent的内容替换为与用户点击相对应的内容。 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 ? 所以问题是如何从TabItem_Tap调用MainPage的方法?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从列表框中获取项目容器(例如stackpanel) - How to get item container (e.g. stackpanel) from listbox 如何在Windows中获取产品密钥(例如Adobe,Office)? - How to Get Product Keys (e.g. Adobe, Office) in Windows? 如何在Windows应用程序中为歌曲设置专辑封面(例如,没有tag-lib#)? - How can I set album art for a song in a Windows App (e.g. without tag-lib#)? 使用 ClickOnce 发布时如何获取状态信息(例如,7/30 已上传)? - How can I get status information (e.g., 7/30 uploaded) when publishing using ClickOnce? 如何在不打开套接字的情况下访问Windows Phone 7特定的网络类型(例如EDGE,3G等)? - How to access Windows Phone 7 specific network type (e.g. EDGE, 3G, etc.) without opening a socket? 如何在c#中解析JSON字符串获取错误无法反序列化当前的JSON对象(例如{“name”:“value”}) - How can i parse JSON string in c# Get error Cannot deserialize the current JSON object (e.g. {“name”:“value”}) 如何获取当前的windows目录,例如C#中的C:\\ - How to get current windows directory e.g. C:\ in C# 如何在 UWP 应用程序(C# 或 WinJS)中获取 Windows 10 版本(例如 1809、1903、1909...)? - How to get Windows 10 version (e.g. 1809, 1903, 1909, ...) in a UWP app (C# or WinJS)? 如何从hWnd获取Windows 10商店应用程序的“应用程序名称”(例如Edge) - How to get the “Application Name” from hWnd for Windows 10 Store Apps (e.g. Edge) url 参数传递电话号码例如+1234,如何检索它例如abc.com/save/xyz/+123 - url parameter passing phone number e.g. +1234 , how to retrieve it e.g. abc.com/save/xyz/+123
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM