1

好的,我基本上有两个ListViews 加载一个集合,然后需要以某种样式呈现。为此,我使用DataTemplate如下所示的 a 。

在两者DataTemplate中,我都需要加载图像和文本。文本工作得很好,但是为了让我返回URL图像的 ,我必须先得到一个user id,然后是一个image hash,将它们合并在一起并返回URL.

所以我所做的user idConverterParameter绑定x:Bindimage hash. 这个想法是我取这两个值,然后转换器返回 final string。thevalue和 theparameter都是strings( URI's)。

在编译时(不是在运行时),我得到了这个errors,我可以在任何地方找到答案,但我不知道它们是什么意思。事件值不能跨越线边界...

我得到的编译错误!

DataTemplates 原样:

xmlns:conv="using:App_Name.Converters"

    <conv:IconIdToUrlConverter x:Key="IconIdToUrlConverter"/>
    <conv:AvatarIdToUrlConverter x:Key="AvatarIdToUrlConverter"/>

    <DataTemplate x:Key="GuildsListDataTemplate" x:DataType="discweb:SocketGuild">
        <StackPanel Margin="0,5,0,0">
            <Ellipse Width="40" Height="40">
                <Ellipse.Fill>
                    <ImageBrush ImageSource="{x:Bind IconId, Mode=OneWay,Converter={StaticResource IconIdToUrlConverter},ConverterParameter={x:Bind Id,Mode=OneWay}}"/>
                </Ellipse.Fill>
            </Ellipse>
            <TextBlock TextAlignment="Center" TextWrapping="WrapWholeWords" Margin="0,2,0,0" Text="{x:Bind Name, Mode=OneWay}" Foreground="White" FontSize="12" />
        </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="GuildUsersListDataTemplate" x:DataType="discweb:SocketGuildUser">
        <StackPanel Orientation="Horizontal" Margin="0,0,2,0">
            <Ellipse Width="10" Height="10">
                <Ellipse.Fill>
                    <ImageBrush ImageSource="{x:Bind AvatarId, Mode=OneWay,Converter={StaticResource AvatarIdToUrlConverter},ConverterParameter={x:Bind Discriminator,Mode=OneWay}}"/>
                </Ellipse.Fill>
            </Ellipse>
            <TextBlock TextAlignment="Center" TextWrapping="WrapWholeWords" Text="{x:Bind Username,Mode=OneWay}" Foreground="White" FontSize="10" />
        </StackPanel>
    </DataTemplate>

ListView(两者之一,虽然相同):

<ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled"
    ScrollViewer.IsHorizontalRailEnabled="False"
    ItemsSource="{x:Bind ChatViewModel.GuildUserList, Mode=OneWay}"
    SelectionMode="None" Grid.Column="1"
    ItemTemplate="{StaticResource GuildUsersListDataTemplate}"/>

注意:我使用的Visual Studio 2017是当前的最新版本!此外,我观察到,当使用Binding而不是x:Bind编译器时不会给出任何错误,但代码并没有做预期要做的事情,因为绑定搜索是在Page'sDataContext而不是我在我的DataTemplate.

编辑(基于评论):

第 33 行和第 59 行是我的 DataTemplates 中的两个 ImageBrush,我已经用它们更新了上面的代码。两个 ConverterParameter 值都返回一个 int。以下代码包含转换器类:

public class AvatarIdToUrlConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        BitmapImage s;
        if (value == null || parameter == null)
            s = new BitmapImage(new Uri("https://something.com/assets/dd4dbc0016779df1378e7812eabaa04d.png"));
        else
            s = new BitmapImage(new Uri(String.Format("https://something.com/avatars/" + parameter.ToString() + "/" + value.ToString() + ".png?size=128")));
        return s;

    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

public class IconIdToUrlConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        BitmapImage s;
        if (value == null || parameter == null)
            s = new BitmapImage(new Uri("https://something.com/assets/dd4dbc0016779df1378e7812eabaa04d.png"));
        else
            s = new BitmapImage(new Uri(String.Format("https://something.com/icons/" + parameter.ToString() + "/" + value.ToString() + ".png?size=128")));
        return s;

    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}
4

0 回答 0