【问题标题】:UWP XAML and C# - x:Bind on a DataTemplate inside a ResourceDictionaryUWP XAML 和 C# - x:Bind 在 ResourceDictionary 内的 DataTemplate 上
【发布时间】:2019-01-08 06:21:49
【问题描述】:

上下文:我正在编写一个 UWP Twitter 客户端。我的 Tweet 类的属性之一是一个名为 IsRetweet 的布尔值 - 如果推文包含转发,则将其设置为 True。

我想将它与x:Load 一起使用,以便有条件地在我的 UI 中加载显示“@username retweeted”的额外行。

我要离开这个例子: https://docs.microsoft.com/en-us/windows/uwp/xaml-platform/x-load-attribute

这是我的 XAML,它位于 ResourceDictionary 中:

<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 绑定有问题。我对使用此 DataTemplate 的对象进行了三重检查,每个对象的 IsRetweet 属性设置肯定正确。

ETA:通过将其添加到我的 UI 页面的 XAML 中,我可以让 x:Bind 加载绑定数据:

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

但是,现在如果我将新内容动态加载到 UI 中,x:Bind 绑定不会呈现。

示例:

【问题讨论】:

  • 如果你在 DataTemplate 上设置了 x:DataType (你绝对需要它来工作),你不应该需要 x:Boolean 转换。否则,如果没有更大的样本,这看起来很好 - 最好是整个 DataTemplate 标记? (Fwiw,由于某种原因,x:Load 实际上会妨碍 DataTemplates 中的性能,原因我还没有弄清楚)(顺便说一句......让数据模板的每个实例声明它们自己的样式并不是最好的主意- 你也可以这样做一次,然后一次又一次地保存它们)
  • 我的整个项目在这里是公开的:github.com/TweeterUWP/Tweeter 我的 DataTemplate 在 ResourceDictionary 中,我的 DataType 设置为使用 DataTemplate (Tweeter.Tweet2) 的对象。我最终在 StackPanel 中使用了 Setter,因为我无法在页面级别将 Setter 应用到有问题的 StackPanel,尽管这可能只是我是个智障......
  • 我只能说,使用 GitHub 上的代码,单击推文会在新刀片中打开该推文,并在新刀片中正确应用 x:Load :/ See example。此外,您希望在 App.xaml 中进行合并,替换您在 App.xaml 中的 current ResourceDictionary 合并 - 不要在页面本身上执行此操作。在页面上进行操作可能是您新问题的根源(现在有冲突的 ResourceDictionaries - 其中一个在技术上已损坏) - 请参阅下面的答案。
  • 我想我可能没有在推文文本的绑定中使用 x:Load 推送我的最后更改。不过,将我的 MergedDictionaries 部分移动到 App.xaml 解决了这个问题。

标签: c# uwp uwp-xaml xaml-binding


【解决方案1】:

您的 App.xaml 仅合并到您的 ResourceDictionary 的 XAML 部分,因为这就是您要求它做的所有事情。

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

但是,当您在 DataTemplates 中使用 x:Bind / x:Load 时,会为您的类创建编译器生成的代码,并且此代码将永远不会加载,因为您将 ResourceDictionary 作为松散的 XAML 而不是类加载。

要将 ResourceDictionary 及其相关的编译器生成的 x:Load/x:Bind / 代码加载为完整类,请将 App.xaml 中的上述内容替换为:

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

(此时&lt;Grid Grid.Row="0" x:Name="RetweetedBy" x:Load="{x:Bind IsRetweet}"&gt; 足以让它按照您的需要工作。)

【讨论】:

  • 成功了!有什么不同?我猜这是应用程序级别踩过 ResourceDictionary 的页面级别包含?
猜你喜欢
  • 1970-01-01
  • 2015-12-28
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-19
相关资源
最近更新 更多