【问题标题】:Cant Find Control Windows Phone 8.1 UAP找不到控制 Windows Phone 8.1 UAP
【发布时间】:2015-06-15 00:42:24
【问题描述】:

我试图在 windows phone 8.1 的集线器控件中找到我的 xaml 下的控件。可能是我的方法在桌面上运行良好,因为我之前在 wpf 中对其进行了测试,但我将其移植到 windows phone 并且可能无法正常工作。

<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,即使我已经以与我相同的方式完成了

【问题讨论】:

  • 您是否调试过代码以查看发生了什么以及为什么它不起作用?
  • @Romasz 是的,它显示对象为空,即仍然无法在集线器控件中找到它

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


【解决方案1】:

在不同版本的 Windows Phone / Windows 运行时,我已经多次替换我的方法来查找孩子,而这个是最可靠的。万一您无法发现错误,您也可以试试这个,看看它是否会产生更好的结果:

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)。

【讨论】:

  • 感谢这个我如何称呼它例如在集线器部分中找到一个列表框
  • 我尝试了以下但仍然声明无法找到控件 ListBox listBoxobjc = FindElementByName(this, "listBoxobj") as ListBox; listBoxobjc.ItemsSource = DB_ContactList;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多