[英]C# - Adding XAML elements to UWP app via x:Bind
Context: I'm writing a UWP Twitter client. 上下文:我正在编写UWP Twitter客户端。
One of the useful bits of data Twitter returns via its API are objects for content that can be linked directly in a tweet - #hashtags, @usernames, $symbols, and URLs. Twitter通过其API返回的有用数据之一是可以直接在推文中链接的内容的对象-#hashtags,@ usernames,$ symbols和URL。 This makes it easy to extract these objects from the string containing a tweet's full text, in order to turn them into links.
这使得从包含推文全文的字符串中提取这些对象变得容易,以便将它们转换为链接。
I understand how the XAML needs to look for this, with <run>
and <hyperlink>
tags, and I've figured out how to create that XAML dynamically for each tweet object. 我了解XAML如何使用
<run>
和<hyperlink>
标签查找此内容,并且已经弄清楚如何为每个tweet对象动态创建该XAML。
What I can't figure out is how to inject my generated XAML into my app's DataTemplate
. 我不知道如何将生成的XAML注入应用程序的
DataTemplate
。 Because tweet content needs to be displayed on multiple pages in the app, I'm using a ResourceDictionary
to hold all my XAML styling, including my DataTemplate
. 由于推文内容需要在应用程序的多个页面上显示,因此我正在使用
ResourceDictionary
来保存我的所有XAML样式,包括DataTemplate
。 So, I'm entirely unsure how to connect my generated XAML to my app's UI. 因此,我完全不确定如何将生成的XAML连接到应用程序的UI。
For example, if a tweet looks like this: 例如,如果一条推文如下所示:
"Hey @twitter, you're a time waster! #FridayFeeling" “嘿@twitter,您很浪费时间!#FridayFeeling”
My generated XAML objects look like this: 我生成的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>
If there's nothing to link in a tweet's text, then I can just use Tweet.Text
as-is, so I'm trying to bind this to a TextBox
. 如果在推文的文本中没有任何链接,那么我可以
Tweet.Text
原样使用Tweet.Text
,因此我试图将其绑定到TextBox
。 How can I handle inserting this XAML dynamically? 如何处理动态插入的XAML?
Am I stuck abandoning data binding entirely and looping through my collection to programmatically add all my XAML? 我是否会完全放弃数据绑定并循环遍历我的集合以以编程方式添加所有XAML?
Here's my DataTemplate: 这是我的DataTemplate:
<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>
If you want to use attached property, here is an example to start with : 如果要使用附加属性,请以以下示例开头:
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);
}
}
And in xaml : 在xaml中:
<TextBlock local:Twitter.Inlines ="{x:Bind TwitterData}"></TextBlock>
Where TwitterData is a List<Inline>
其中TwitterData是
List<Inline>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.