访问flipview中第i项内的xaml控件

我有一个由一些代码填充的flipview(我不明白如何修改应用程序)。

             

从名为“FeedUrl”的文本块我想提取存储在其中的URL。

使用url解析该url指向的html页面

处理完后在richtextblock中显示一些名为“content”的内容。

我面临的唯一问题是如何在flipview的每个项目中引用textblock和richtextblock。

为了获得参考项目,我尝试了两种解决方案:


  1. 我已经尝试过这段代码了

var myTextBlock= _Children.OfType().FirstOrDefault(c => c.Name.Equals("test")); 特别

.OfType()给出错误

'System.Collections.Generic.List' does not contain a definition for 'OfType' and no extension method 'OfType' accepting a first argument of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)


  1. 我也试过这里给出的另一个解决方案,但我总是得到一个空引用。

我也收到警告线

var item = itemsControl.ItemContainerGenerator.ContainerFromItem(o); Windows.UI.Xaml.Controls.ItemContainerGenerator.ContainerFromItem(o); is obsolote.'ContainerForm' may be unavailable for releases after Windows Phone 8.1. Use itemsControl.ContainerFromItem instead.

即使我使用itemsControl.ContainerFromItem它总是返回一个空引用。

请帮忙


更新:

我正在使用以下内容

 if(!statusiop.statusup){ this.UpdateLayout(); for (int i = 0; i < ArticleDetail.Items.Count; i++) { var fvItem = this.ArticleDetail.Items[i]; var container = this.ArticleDetail.ContainerFromItem(fvItem); if (container == null) { Text = "null container"; } else { var tbFeedURL = FindElementByName(container, "FeedUrl"); if (tbFeedURL == null) { test.Text = "null text"; } else { tbFeedURL.Text = tbFeedURL.Text + "Test"; } } } 

我遍历flipview中的所有项目,并根据需要修改数据。 我也在使用公共静态类

 public static class statusiop { public static Boolean statusup= false; } 

其中包含成员状态。 statusup用作标志,当为true时表示flipview数据已更新一次且无需再次更新。

您需要一个VisualTreeHelper方法。 这只是我正在使用的一些代码。 我认为你可以根据自己的需要轻松调整它。

首先将FindElementByName方法放在代码隐藏文件的某处:

 public T FindElementByName(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(child, sChildName); if (childElement != null) break; } return childElement; } 

现在您可以开始使用该方法:

 this.UpdateLayout(); var fvItem = this.ArticleDetail.Items[ArticleDetail.SelectedIndex]; var container = this.ArticleDetail.ContainerFromItem(fvItem); // NPE safety, deny first if (container == null) return; var tbFeedURL = FindElementByName(container, "FeedUrl"); // And again deny if we got null if (tbFeedURL == null) return; /* Start doing your stuff here. */