将PivotItem事件发送到PivotItems(用户控件)

时间:2013-09-19 12:56:52

标签: xaml windows-phone-7 navigation

所以,我有Pivot和PivotItems作为UserControls。 我想知道,每个PivotItem都是NavigatedTo和NavigatedFrom。

我创建了一个基类(PivotItems正在继承它),添加了2个方法(To和From),并且我在pivot中有LoadingPivotItemCommand(),所以我知道,哪个PivotItem被加载。

但是如何将这个事件转播到枢轴?我试过一些方法,但所有这些都是空的。

void LoadingPivotItemCommand(PivotItemEventArgs args)
    {
        var b = args.Item.Parent as BaseUserControl;
        var a = args.Item.Content as BaseUserControl;
        var a1 = args.Item.Content as UserControl;

        var c = args.Item.DataContext as BaseUserControl;

        if (c != null) 
            c.OnPivotItemActivated();
    }

PivotItems在xaml中定义:

   <controls:PivotItem Header="{Binding Path=MainResources.Products, Source={StaticResource LocalizedStrings}, Converter={StaticResource StringToLowerCaseConverter}}"
                            Name="PivotItemProducts">
            <Grid>
                <productsView:ProductUserControl />     
            </Grid>
        </controls:PivotItem>

1 个答案:

答案 0 :(得分:1)

我猜测ProductUserControl是继承BaseUserControl的形式。如果您的BaseUserControl始终位于Grid中,就像您显示的xaml一样,那么您可以使用:

var a1 = (args.Item.Content as Grid).Children[0] as BaseUserControl;

否则,如果您的UserControl可以放在PivotItem内的不同部分,那么您可以使用I gave you的函数,只需用BaseUserControl替换Image。