【发布时间】: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=" "/></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