简体   繁体   English

Windows Phone 8.1 ListView元素在绑定后执行代码

[英]Windows Phone 8.1 ListView element execute code after bind

I have a ListView defined in a XAML interface which is bound to a collection. 我在绑定到集合的XAML接口中定义了一个ListView。

The list view's DataTemplate features a WebView. 列表视图的DataTemplate具有WebView。 I need this WebView to size to its contents, which means that after the ListView is bound, and elements are created, and those elements are bound, I need to execute a fragment of code on each WebView. 我需要此WebView调整其内容大小,这意味着在绑定ListView,创建元素并绑定这些元素之后,我需要在每个WebView上执行一段代码。

I've already taken care of getting the WebView to bind, and I have the code which will size a WebView. 我已经做好了绑定WebView的工作,并且已经有了可以调整WebView大小的代码。 I simply need to know how to execute it; 我只需要知道如何执行它即可。 where to put it; 放在哪里; how to get the WebViews and when to try to get them. 如何获取WebView以及何时尝试获取WebView。

EDIT: 编辑:

Here's my list view. 这是我的列表视图。

        <ListView Opacity="{Binding IsRefreshing, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource BooleanToGhost}}" Grid.Row="0" Name="listView" ItemsSource="{Binding Messages}"  IsItemClickEnabled="False" SelectionMode="Single">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="8">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Rectangle Grid.ColumnSpan="2" Grid.RowSpan="2" Fill="White" RadiusX="12" RadiusY="12" />
                        <TextBlock Foreground="#FFAAAAAA" Grid.Row="0" Grid.Column="0" Margin="8" Text="You" HorizontalAlignment="Left" FontSize="11" Visibility="{Binding IsFromStaff, Mode=OneTime,Converter={StaticResource BooleanToInvisibility}}" />
                        <TextBlock Foreground="#FFAAAAAA" Grid.Row="0" Grid.Column="0" Margin="8" Text="Staff" HorizontalAlignment="Left" FontSize="11" Visibility="{Binding IsFromStaff, Mode=OneTime,Converter={StaticResource BooleanToVisibility}}" />
                        <TextBlock Foreground="#FFAAAAAA" Grid.Row="0" Grid.Column="1" Margin="8" Text="{Binding Timestamp, Mode=OneTime}" HorizontalAlignment="Right" FontSize="11" />
                        <WebView local:MyProperties.HtmlString="{Binding Body}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Margin="8" ScrollViewer.VerticalScrollBarVisibility="Disabled" />
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
            <interactivity:Interaction.Behaviors>
                <core:EventTriggerBehavior EventName="SelectionChanged">
                    <core:InvokeCommandAction Command="{Binding ShowCaseCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=listView, Path=SelectedItem}" />
                </core:EventTriggerBehavior>
            </interactivity:Interaction.Behaviors>
        </ListView>

The solution is something called "Behaviors". 解决方案是所谓的“行为”。

We see in the code snippet that the DataTemplate contains a WebView , which is the item I want to affect. 我们在代码片段中看到DataTemplate包含一个WebView ,这是我要影响的项目。 So I add a Behavior to the WebView, which is a class with code. 因此,我向WebView中添加了一个Behavior ,这是一个带有代码的类。 The class looks like so: 该类如下所示:

public class WebViewSizeBehavior : DependencyObject, IBehavior
{
    public DependencyObject AssociatedObject { get; private set; }

    public void Attach(DependencyObject associatedObject)
    {
        var control = associatedObject as WebView;
        if (control == null)
            throw new ArgumentException(
                "WebViewSizeBehavior can be attached only to WebView.");
        AssociatedObject = associatedObject;
        control.LoadCompleted += Control_LoadCompleted;
    }

    private void Control_LoadCompleted(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
    {
        var control = (WebView) AssociatedObject;

        var resizeTask = control.ResizeToContent();
    }

    public void Detach()
    {
        var control = (WebView) AssociatedObject;
        control.LoadCompleted -= Control_LoadCompleted;
        AssociatedObject = null;
    }
}

My namespaces and XAML are rigged up such that this class is reachable only the local namespace. 我的名称空间和XAML已装配,使得此类仅可通过local名称空间访问。 How this is done is beyond the scope of this answer. 如何完成此操作超出了此答案的范围。 So, given that, I can amend the XAML like so: 因此,鉴于此,我可以像这样修改XAML:

<ListView.ItemTemplate>
    <DataTemplate>
        …
        <WebView …>
            <interactivity:Interaction.Behaviors>
                <local:WebViewSizeBehavior />
            </interactivity:Interaction.Behaviors>
        </WebView>
    </DataTemplate>
</ListView.ItemTemplate>

I've left out content which is beyond the scope of this thread. 我遗漏了超出该线程范围之外的内容。 Information about fitting a WebView to its content, as well as namespaces and XML namespaces, is available elsewhere. 有关使WebView适应其内容以及名称空间和XML名称空间的信息,可在其他位置获得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM