簡體   English   中英

C#-通過x:Bind將XAML元素添加到UWP應用

[英]C# - Adding XAML elements to UWP app via x:Bind

上下文:我正在編寫UWP Twitter客戶端。

Twitter通過其API返回的有用數據之一是可以直接在推文中鏈接的內容的對象-#hashtags,@ usernames,$ symbols和URL。 這使得從包含推文全文的字符串中提取這些對象變得容易,以便將它們轉換為鏈接。

我了解XAML如何使用<run><hyperlink>標簽查找此內容,並且已經弄清楚如何為每個tweet對象動態創建該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原樣使用Tweet.Text ,因此我試圖將其綁定到TextBox 如何處理動態插入的XAML?

我是否會完全放棄數據綁定並循環遍歷我的集合以以編程方式添加所有XAML?

這是我的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="&#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>

如果要使用附加屬性,請以以下示例開頭:

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM