无法找到控制Windows Phone 8.1 UAP

时间:2015-06-14 01:58:43

标签: c# xaml windows-phone-8.1 win-universal-app

我试图在Windows Phone 8.1的集线器控件中找到我的xaml下的控件。可能是我的方法在桌面上正常工作的情况,因为我之前在wpf中测试它但是我将它移植到Windows手机上并且可能无法正常工作。

<Grid>
    <Hub Header="Lists" Name="mainHub" >
        <HubSection MinWidth="600" Name="lattestLists" Header="New Lists">
            <DataTemplate>
                <Grid>
                    <ListBox Background="Transparent" Margin="6" Height="auto" BorderThickness="2" MaxHeight="580" Grid.Row="1" x:Name="listBoxobj"  >
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid Width="350" >
                                    <Border Margin="5" BorderBrush="White" BorderThickness="1">
                                        <Grid>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto"/>
                                                <RowDefinition Height="Auto"/>
                                            </Grid.RowDefinitions>
                                            <TextBlock Margin="5,0,0,0" Grid.Row="0" x:Name="NameTxt" TextWrapping="Wrap" Text="{Binding Name}" FontSize="28" Foreground="White"/>
                                            <TextBlock Grid.Row="0" Text=">" FontSize="28"  HorizontalAlignment="Right" VerticalAlignment="Center" Foreground="White"/>
                                            <TextBlock Margin="5,0,0,0" Grid.Row="1" x:Name="PhoneTxt"  TextWrapping="Wrap" Foreground="White" FontSize="18" Text="{Binding PhoneNumber}" />
                                            <TextBlock HorizontalAlignment="Right" Margin="0,0,35,0" Grid.Row="3" x:Name="CreateddateTxt" Foreground="White" FontSize="18" TextWrapping="Wrap" Text="{Binding CreationDate}" />
                                        </Grid>
                                    </Border>
                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </Grid>
            </DataTemplate>
        </HubSection>

        <HubSection Header="Tech" IsHeaderInteractive="True" 
            Background="#222222" MinWidth="250">
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="Tech news goes here."
                       Style="{ThemeResource BodyTextBlockStyle}" />
                    <TextBlock Text="Click the header to go to the Tech page."
                       Style="{ThemeResource BodyTextBlockStyle}" />
                </StackPanel>
            </DataTemplate>
        </HubSection>

        <HubSection Header="Sports" IsHeaderInteractive="True"
            Background="#444444" MinWidth="250">
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="Sports news goes here."
                       Style="{ThemeResource BodyTextBlockStyle}" />
                    <TextBlock Text="Click the header to go to the Sports page."
                       Style="{ThemeResource BodyTextBlockStyle}" />
                </StackPanel>
            </DataTemplate>
        </HubSection>
    </Hub>

</Grid>

我正在尝试使用以下方法来查找lsitbox但它无法正常工作

      ListBox listBoxobjc = FindChildControl<ListBox>(this, "listBoxobj") as ListBox;
        listBoxobjc.ItemsSource = DB_ContactList.OrderByDescending(i => i.id).ToList();//Binding DB data to LISTBOX and Latest contact ID can Display first.  

这是FindChildControl方法

  private DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
    {
        int childNumber = VisualTreeHelper.GetChildrenCount(control);
        for (int i = 0; i < childNumber; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(control, i);
            FrameworkElement fe = child as FrameworkElement;
            // Not a framework element or is null
            if (fe == null) return null;

            if (child is T && fe.Name == ctrlName)
            {
                // Found the control so return
                return child;
            }
            else
            {
                // Not found it - search children
                DependencyObject nextLevel = FindChildControl<T>(child, ctrlName);
                if (nextLevel != null)
                    return nextLevel;
            }
        }
        return null;
  }

编辑 我对代码进行了调试,并将控件显示为null,即使我已经按照与

相同的方式进行操作

1 个答案:

答案 0 :(得分:0)

在Windows Phone / Windows Runtime的不同版本中,我已经替换了我的方法,以便多次找到孩子,这个版本最可靠。如果您无法发现错误,您也可以尝试这个错误,看看它是否会产生更好的结果:

public T FindElementByName<T>(DependencyObject element, string sChildName) where T : FrameworkElement
    {
        T childElement = null;
        var nChildCount = VisualTreeHelper.GetChildrenCount(element);
        for (int i = 0; i < nChildCount; i++)
        {
            FrameworkElement child = VisualTreeHelper.GetChild(element, i) as FrameworkElement;

            if (child == null)
                continue;

            if (child is T && child.Name.Equals(sChildName))
            {
                childElement = (T)child;
                break;
            }

            childElement = FindElementByName<T>(child, sChildName);

            if (childElement != null)
                break;
        }
        return childElement;
    }

同样在创建/加载内容后不久使用此方法时,我事先调用UpdateLayout:

this.UpdateLayout();

但要小心 - 这不是性能最友好的方法之一(另请参阅:https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.updatelayout)。