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

uwp xaml和c-x:绑定到ResourceDictionary内的数据模板

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

    上下文:我正在写一个UWPTwitter客户端。我的tweet类的一个属性是一个名为 is retweet的bool。如果tweet包含retweet,则此属性设置为true。

    我想将其与 x:load一起使用,以便在我的用户界面中有条件地加载一个显示“@username retweeted”的额外行。

    我将放弃这个例子: https://docs.microsoft.com/en-us/windows/uwp/xaml-platform/x-load-attribute

    这是我的XAML,它在一个资源字典中:

    <grid grid.row=“0”x:name=“retweetedby”x:load=“x:bind(x:boolean)isretweet,converter=staticresource debugthis_”>
    <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;”/>/lt;/textBlock>
    </border>
    <textBlock text=“绑定路径=user.name”/>
    <textblock text=“retweted”/>
    </stackpanel>
    </grid>
    

    我在为x:load绑定的字段中添加了一个名为debugthis的临时转换器,如下所示:

    {
    公共对象转换(对象值、类型TargetType、对象参数、字符串语言)
    {
    bool isretweet=(bool)值;
    
    返回Isretweet;
    }
    
    公共对象转换回(对象值、类型TargetType、对象参数、字符串语言)
    {
    抛出new NotImplementedException();
    }
    }
    

    我在这上面插入了一个断点,甚至没有命中转换器,所以我猜我的XAML绑定有问题。我已经三次检查了使用此数据模板的对象,并且每个对象都肯定具有isretweetproperty set correctly.

    eta:我可以通过将其添加到我的用户界面页面的xaml:来加载绑定数据。

    <page.resources>
    <资源字典>
    <resourceDictionary.mergedDictionaries>
    <tweeter:visuals/>
    </resourcedictionary.mergeddictionaries>
    </resourcedictionary>
    </page.resources>
    

    但是,现在,如果我动态地将新内容加载到UI中,x:bindbindings are not rendering.

    示例: .

    我想用这个x:Load为了有条件地在我的用户界面中加载一个显示“@username retweeted”的额外行。

    我将放弃这个例子: https://docs.microsoft.com/en-us/windows/uwp/xaml-platform/x-load-attribute

    这是我的XAML,它在一个资源字典中:

    <Grid Grid.Row="0" x:Name="RetweetedBy"  x:Load="{x:Bind (x:Boolean)IsRetweet, Converter={StaticResource DebugThis}}">
        <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="{Binding Path=User.Name}" />
            <TextBlock Text=" retweeted"/>
        </StackPanel>
    </Grid>
    

    我在为x:load绑定的字段中添加了一个名为debugthis的临时转换器,如下所示:

    public class DebugThis : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            bool IsRetweet = (bool)value;
    
            return IsRetweet;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    

    我在这上面插入了一个断点,甚至没有命中转换器,所以我猜我的XAML绑定有问题。我已经三次检查了使用这个数据模板的对象,并且每个对象都有Isretweet公司属性设置正确。

    埃塔:我能x:Bind要通过将绑定数据添加到我的UI页的XAML中来加载绑定数据,请执行以下操作:

    <Page.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <tweeter:Visuals />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Page.Resources>
    

    但是,现在如果我动态地将新内容加载到UI中,X:绑定绑定没有呈现。

    例子:

    1 回复  |  直到 8 年前
        1
  •  2
  •   Johnny Westlake    8 年前

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Visuals.xaml"/> <!-- Only loads XAML -->
    </ResourceDictionary.MergedDictionaries>
    

    <ResourceDictionary.MergedDictionaries>
        <local:Visuals />
    </ResourceDictionary.MergedDictionaries>
    

    <Grid Grid.Row="0" x:Name="RetweetedBy" x:Load="{x:Bind IsRetweet}">