Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 找不到控制Windows Phone 8.1 UAP_C#_Xaml_Windows Phone 8.1_Win Universal App - Fatal编程技术网

C# 找不到控制Windows Phone 8.1 UAP

C# 找不到控制Windows Phone 8.1 UAP,c#,xaml,windows-phone-8.1,win-universal-app,C#,Xaml,Windows Phone 8.1,Win Universal App,我试图在WindowsPhone8.1的集线器控件中找到我的xaml下的控件。这可能是我的方法在桌面上运行良好的情况,因为我以前在wpf中测试过它,但我正在将它移植到windows phone上,可能不起同样的作用 <Grid> <Hub Header="Lists" Name="mainHub" > <HubSection MinWidth="600" Name="lattestLists" Header="New Lists">

我试图在WindowsPhone8.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.  
ListBox listBoxobjc=FindChildControl(这个“listBoxobj”)作为ListBox;
listBoxobjc.ItemsSource=DB_ContactList.OrderByDescending(i=>i.id).ToList()//将DB数据绑定到LISTBOX和最新联系人ID可以首先显示。
这是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;
  }
私有DependencyObject FindChildControl(DependencyObject控件,字符串ctrlName)
{
int childNumber=VisualTreeHelper.GetChildrenCount(控件);
for(int i=0;i
编辑
我调试了代码,并将控件显示为null,尽管我的方法与我的方法相同

我已经在不同版本的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;
    }

但要小心——这不是性能最友好的方法之一(另请参见:).

您是否调试了代码以查看发生了什么以及为什么不起作用?@Romasz是的,它显示对象为空,即在中心控件中仍然找不到它。为此,我如何调用它例如在中心部分中查找列表框我尝试了以下操作,但仍然无法找到控件列表框listBoxobjc=FindElementByName(这是“listBoxobj”)作为ListBox;listBoxobjc.ItemsSource=DB_ContactList;
this.UpdateLayout();