Sending LoadingPivotItem event to PivotItems (User

2019-09-09 10:35发布

问题:

So, i have Pivot and PivotItems as UserControls. I'd like to know, when every PivotItem is NavigatedTo and NavigatedFrom.

I made a base class (PivotItems are inheriting it), added there 2 methods (To and From), and i have LoadingPivotItemCommand() in the pivot, so i know, which PivotItem is loaded.

But how to broadcast this event to pivots? I tried some ways, but all of them are nulls.

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 are defined in xaml:

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

回答1:

I'm guessing ProductUserControl is the one inheriting form BaseUserControl. If your BaseUserControl is always inside a Grid like in the xaml you show then you could just use :

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

Otherwise if your UserControl can be place at different part inside the PivotItem then you can just use the function that I gave you, just replacing Image by BaseUserControl.