如何从中心部分访问网格视图 数据模板 Windows 手机 8.1 在 C# 中

本文关键字:手机 Windows 视图 心部 访问 网格 数据 | 更新日期: 2023-09-27 18:31:43

请告诉我如何在中心部分数据模板中评估网格视图。在通用应用程序 ( Windows Phone 8.1 ) 中以下是 xaml

   <HubSection Header="English Newspapers" x:Name="HubEnglish">
        <DataTemplate x:Name="DTEnglish">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                </Grid.RowDefinitions>
                <StackPanel Orientation="Vertical">
                    <GridView x:Name="LstEnglishNewspapers">
                        <GridView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Margin="0,0,0,0" Orientation="Horizontal">
                                    <Image x:Name="txtNewspaperImage" Source="{Binding PicPath}" Height="60" Width="60" VerticalAlignment="Center" Margin="0,0,10,0"/>
                                    <StackPanel Orientation="Vertical">
                                        <TextBlock x:Name="txtNewspaperHeader" TextWrapping="Wrap" Foreground="{StaticResource ApplicationForegroundThemeBrush}" FontSize="14.667" FontWeight="Light" Width="200" VerticalAlignment="Center" HorizontalAlignment="Left" FontFamily="Segoe UI"/>
                                        <TextBlock x:Name="txtNewsHeader" TextWrapping="Wrap" Foreground="{StaticResource ApplicationForegroundThemeBrush}" FontSize="14.667" FontWeight="Light" Width="200" MaxHeight="20" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                                    </StackPanel>
                                </StackPanel>
                            </DataTemplate>
                        </GridView.ItemTemplate>
                    </GridView>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </HubSection>

我尝试使用此代码来获取网格,但我遇到异常。

var grid = VisualTreeHelper.GetChild(HubEnglish.ContentTemplate, 0);

异常:灾难性故障(来自 HRESULT 的异常:0x8000FFFF (E_UNEXPECTED))

如何从中心部分访问网格视图 数据模板 Windows 手机 8.1 在 C# 中

找到此代码以获取数据模板

    static DependencyObject FindChildByName(DependencyObject from, string name)
{
    int count = VisualTreeHelper.GetChildrenCount(from);
    for (int i = 0; i < count; i++)
    {
        var child = VisualTreeHelper.GetChild(from, i);
        if (child is FrameworkElement && ((FrameworkElement)child).Name == name)
            return child;
        var result = FindChildByName(child, name);
        if (result != null)
            return result;
    }
    return null;
}
private TextBlock NoArticlesTextBlock;
private void Page_Loaded(object sender, RoutedEventArgs e)
{
    // Note: No need to start searching from the root (this), we can just start
    // from the relevant HubSection or whatever. Make sure your TextBlock has
    // x:Name="NoArticlesTextBlock" attribute in the XAML.
    NoArticlesTextBlock = (TextBlock)FindChildByName(this, "NoArticlesTextBlock");
}