簡體   English   中英

如何在C#中從中心部分數據模板Windows Phone 8.1訪問網格視圖

[英]How to Access Grid View from hub section Data Template Windows phone 8.1 in C#

請告訴我如何評估中心部分數據模板中的網格視圖。 在通用應用程序(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))

找到此代碼以獲取數據模板

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM