代码之家  ›  专栏  ›  技术社区  ›  CXL

C 35;-通过x:Bind向UWP应用程序添加XAML元素

  •  0
  • CXL  · 技术社区  · 8 年前

    上下文:我正在写一个UWP-Twitter客户端。

    Twitter通过其API返回的有用数据位之一是内容的对象,这些内容可以直接链接到tweet-hashtags、@usernames、$symbols和url中。这使得从包含tweet全文的字符串中提取这些对象变得很容易,以便将它们转换为链接。

    我知道XAML需要如何查找这个 <run> <hyperlink> 标签,我已经找到了如何为每个tweet对象动态创建XAML的方法。

    我不知道的是如何将生成的XAML注入我的应用程序 DataTemplate . 因为tweet内容需要在应用程序的多个页面上显示,所以我使用 ResourceDictionary 保存我所有的XAML样式,包括 数据模板 . 所以,我完全不确定如何将生成的XAML连接到应用程序的UI。

    例如,如果tweet如下所示:

    “嘿@twitter,你真是个浪费时间的人!#周五感觉”

    我生成的XAML对象如下所示:

    <Run>Hey </Run>
    <Hyperlink link="http://twitter.com/twitter/">@twitter</Hyperlink>
    <Run>, you're a time waster! </Run>
    <Hyperlink link="http://twitter.com/search?hashtag=FridayFeeling">#FridayFeeling</Hyperlink>
    

    如果tweet的文本中没有可链接的内容,那么我可以使用 Tweet.Text 所以我想把这个和 TextBox . 如何处理动态插入此XAML?

    我是否要完全放弃数据绑定并在集合中循环以编程方式添加所有的XAML?

    这是我的数据模板:

    <DataTemplate x:Key="TweetTemplate" x:DataType="tweeter:Tweet2">
        <Grid Style="{StaticResource ListItemStyle}">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="40"/>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0" x:Name="RetweetedBy" x:Load="{x:Bind IsRetweet}" Height="28">
                <StackPanel Orientation="Horizontal" Padding="4 8 4 0">
                    <StackPanel.Resources>
                        <Style TargetType="TextBlock">
                            <Setter Property="FontSize" Value="12"/>
                            <Setter Property="Foreground" Value="{ThemeResource SystemControlPageTextBaseMediumBrush}" />
                        </Style>
                    </StackPanel.Resources>
                    <Border Height="28">
                        <TextBlock Height="24" FontFamily="{StaticResource FontAwesome}" xml:space="preserve"><Run Text="&#xf079;&#160;"/></TextBlock>
                    </Border>
                    <TextBlock Text="{x:Bind Path=User.Name}" />
                    <TextBlock Text=" retweeted"/>
                </StackPanel>
            </Grid>
            <Grid Grid.Row="1">
                <StackPanel Orientation="Horizontal" Padding="5">
                    <TextBlock Text="{x:Bind Path=Tweet.User.Name}" Margin="0 0 8 0"  FontWeight="Bold" />
                    <TextBlock Text="{x:Bind Path=Tweet.User.ScreenName, Converter={StaticResource GetHandle}}" Foreground="{ThemeResource SystemControlPageTextBaseMediumBrush}" />
                    <TextBlock Text="&#x2981;" Margin="8 0" />
                    <TextBlock Text="{x:Bind Path=Tweet.CreationDate, Converter={StaticResource FormatDate}}" />
                </StackPanel>
            </Grid>
            <Grid Grid.Row="2">
                <TextBlock Text="***this is where I'm having problems***" Padding="5" TextWrapping="WrapWholeWords"/>
            </Grid>
            <Grid Grid.Row="3">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="2.5*" MaxWidth="100"/>
                    <ColumnDefinition Width="2.5*"/>
                    <ColumnDefinition Width="2.5*"/>
                    <ColumnDefinition Width="2.5*"/>
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="0">
                    <Button x:Name="cmdComment" Content="&#xf075;" Style="{StaticResource MetaButtons}" />
                </Grid>
                <Grid Grid.Column="1">
                    <Button x:Name="cmdRetweet" Content="&#xf079;" Style="{StaticResource MetaButtons}" />
                </Grid>
                <Grid Grid.Column="2">
                    <Button x:Name="cmdLike" Content="&#xf004;" Style="{StaticResource MetaButtons}" />
                </Grid>
                <Grid Grid.Column="3">
                    <Button x:Name="cmdMessage" Content="&#xf0e0;" Style="{StaticResource MetaButtons}" />
                </Grid>
            </Grid>
        </Grid>
    </DataTemplate>
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Yves Israel    8 年前

    如果要使用附加属性,请从以下示例开始:

    public static class Twitter
    {
        public static readonly DependencyProperty InlinesProperty = DependencyProperty.RegisterAttached(
            "Inlines", typeof(ICollection<Inline>), typeof(Twitter), new PropertyMetadata(default(ICollection<Inline>), PropertyChangedCallback));
    
        private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is TextBlock tb)) return;
    
            tb.Inlines.Clear();
    
            if (!(e.NewValue is ICollection<Inline> inlines)) return;
    
            foreach (var inline in inlines)
            {
                tb.Inlines.Add(inline);
            }
        }
    
        public static void SetInlines(DependencyObject element, ICollection<Inline> value)
        {
            element.SetValue(InlinesProperty, value);
        }
    
        public static ICollection<Inline> GetInlines(DependencyObject element)
        {
            return (ICollection<Inline>) element.GetValue(InlinesProperty);
        }
    }
    

    在xaml中:

            <TextBlock local:Twitter.Inlines ="{x:Bind TwitterData}"></TextBlock>
    

    其中TwitterData是 List<Inline>

    推荐文章