【问题标题】:Access xaml control inside ith item in flipview在翻转视图中访问第 i 个项目内的 xaml 控件
【发布时间】:2014-10-07 13:03:30
【问题描述】:

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

<FlipView  x:Name="ArticleDetail" Grid.Row="1" AutomationProperties.AutomationId="ItemsFlipView" AutomationProperties.Name="Item Details" TabIndex="1"
             DataContext="{Binding LatestArticlesModel}"
             ItemsSource="{Binding Items}"
             ItemTemplate="{StaticResource LatestArticles1DetailDetail}"
             SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
             ItemContainerStyle="{StaticResource FlipItemStyle}">
</FlipView>

<!--Data template for flipview-->
<DataTemplate x:Key="LatestArticles1DetailDetail">
      <ScrollViewer>
        <StackPanel>
             <TextBlock Margin="0,16" 
                  Text="{Binding Title, Converter={StaticResource    TextPlainConverter}, ConverterParameter = 140}" 
                  Style="{StaticResource SubHeaderText}" />
              <Image Source="{Binding ImageUrl, Converter={StaticResource ThumbnailConverter},      ConverterParameter=300}" Stretch="UniformToFill" />
              <TextBlock 
                   x:Name="FeedUrl" 
                   Margin="0,12"  Style="{StaticResource Html2XamlStyleText}"
                   Text="{Binding FeedUrl}" 
                   Visibility="Collapsed"/>
              <RichTextBlock 
                    x:Name="Content" 
                    Margin="0,12"  
                    Style="{StaticResource Html2XamlStyle}"/>
            </StackPanel>
        </ScrollViewer>
</DataTemplate>

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

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

处理后在名为“content”的富文本块中显示一些内容。

我面临的唯一问题是如何在翻转视图的每个项目中获取对 textblock 和richtextblock 的引用。

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


  1. 我试过这个code 但是这条线

var myTextBlock= _Children.OfType&lt;TextBlock&gt;().FirstOrDefault(c =&gt; c.Name.Equals("test")); 具体

.OfType&lt;TextBlock&gt;() 报错

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


  1. 我还尝试了给定here 的另一个解决方案,但我总是得到一个空引用。

我也收到一条警告

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<TextBlock>(container, "FeedUrl");
     if (tbFeedURL == null)
     {
         test.Text = "null text";
     }
     else
     {
         tbFeedURL.Text = tbFeedURL.Text + "Test";
     }
 } 
}

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

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

}

其中包含一个成员状态。 statusup 作为一个标志,当它为 true 时,表示翻转视图数据已经更新过一次,不需要再次更新。

【问题讨论】:

    标签: c# xaml windows-phone-8.1 datatemplate flipview


    【解决方案1】:

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

    首先将 FindElementByName 方法放入代码隐藏文件的某个位置:

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

    现在可以开始使用方法了:

    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<TextBlock>(container, "FeedUrl");
    // And again deny if we got null
    if (tbFeedURL == null)
        return;
    /*
      Start doing your stuff here.
    */
    

    【讨论】:

    • var container = this.ArticleDetail.ItemContainerGenerator.ContainerFromItem(fvItem); 给出警告Windows.UI.Xaml.Controls.ItemContainerGenerator.ContainerFromItem(Object); is obsolote.'ContainerForm' may be unavailable for releases after Windows Phone 8.1. Use itemsControl.ContainerFromItem instead. 空引用返回到容器变量。
    • 尝试致电UpdateLayout()beforehand。我用当前的电话替换了过时的电话。告诉我它现在是否有效。
    • 它正在部分工作。在翻转视图中的 10 个项目中。只有两个更新了,其余的没有。即使我尝试使用从 i=0 到 Article.Items.Count-1 的循环,然后使用 var fvItem = this.ArticleDetail.Items[i];但即便如此,也只有前两个项目被更新。另外,无论哪种情况,我都无法浏览列表中的项目
    • 您可能想用您的代码更新您的问题。没有要检查的代码时很难提供帮助。
    • 我已经用代码更新了问题,请查看它。问题与我在之前的评论中提到的相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 2011-01-04
    • 1970-01-01
    相关资源
    最近更新 更多