【发布时间】:2018-08-05 22:01:14
【问题描述】:
上下文:我正在编写一个 UWP Twitter 客户端。
Twitter 通过其 API 返回的有用数据位之一是可以在推文中直接链接的内容对象 - #hashtags、@usernames、$symbols 和 URL。这使得从包含推文全文的字符串中提取这些对象变得容易,以便将它们转换为链接。
我了解 XAML 如何使用 <run> 和 <hyperlink> 标记来查找此内容,并且我已经弄清楚如何为每个推文对象动态创建 XAML。
我不知道如何将生成的 XAML 注入我的应用程序的 DataTemplate。因为需要在应用程序的多个页面上显示推文内容,所以我使用 ResourceDictionary 来保存我的所有 XAML 样式,包括我的 DataTemplate。所以,我完全不确定如何将我生成的 XAML 连接到我的应用程序的 UI。
例如,如果一条推文如下所示:
“嘿@twitter,你在浪费时间!#FridayFeeling”
我生成的 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.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=" "/></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="⦁" 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="" Style="{StaticResource MetaButtons}" />
</Grid>
<Grid Grid.Column="1">
<Button x:Name="cmdRetweet" Content="" Style="{StaticResource MetaButtons}" />
</Grid>
<Grid Grid.Column="2">
<Button x:Name="cmdLike" Content="" Style="{StaticResource MetaButtons}" />
</Grid>
<Grid Grid.Column="3">
<Button x:Name="cmdMessage" Content="" Style="{StaticResource MetaButtons}" />
</Grid>
</Grid>
</Grid>
</DataTemplate>
【问题讨论】:
-
您可以生成所有内容(运行和超链接)并通过转换器的附加属性将它们添加为文本块的内联集合;或创建一个自定义控件,以便您可以使用您的模板并公开一些您需要访问的依赖项属性
-
我应该一次添加一个内容数组,还是在创建每个内容项时添加它?我对 Attachedproperty 不熟悉 - 你能稍微扩展一下吗?
-
这取决于你的实现。如果您拥有所有内容,您将立即添加它们。 attachproperty 解决方案是帮助您访问和控制文本块的 InlineCollection。您可以根据需要添加/删除内容。这也将允许您使用数据绑定
标签: c# uwp datatemplate uwp-xaml xbind