Microsoft-ui-xaml: ItemsRepeater throws System.InvalidCastException when using converter

Created on 9 Nov 2019  路  9Comments  路  Source: microsoft/microsoft-ui-xaml

Describe the bug
I'm designing an app like NetFlix home page. For a better user experience, I used some converters in ItemsRepeater data template.
Then the app crashes every time staring, and throw exceoption System.InvalidCastException.
But if I don't use converter, the app starts and runs well.

Steps to reproduce the bug

Steps to reproduce the behavior:

  1. Go to MovieTemplate, change Image's source to
    Source="{x:Bind PosterUrl, Converter={StaticResource imageUrlCompress}}"
  2. Run app.

Expected behavior
App runs well and show the normal home page.

Screenshots

Version Info

Win: 18362.418

NuGet package version:

Microsoft.UI.Xaml: 2.2.190917002


| Windows 10 version | Saw the problem? |
| :--------------------------------- | :-------------------- |
| Insider Build (xxxxx) | |
| May 2019 Update (18362) | Yes |
| October 2018 Update (17763) | |
| April 2018 Update (17134) | |
| Fall Creators Update (16299) | |
| Creators Update (15063) | |


| Device form factor | Saw the problem? |
| :-------------------- | :------------------- |
| Desktop | Yes |
| Mobile | |
| Xbox | |
| Surface Hub | |
| IoT | |

Additional context
Here is a small repo, and special thanks to @XamlBrewer's repo.

In WINUI 3.0 alpha, All converters causes crash, including TextBlock converter.

area-ItemsRepeater bug team-Controls

All 9 comments

@ranjeshj @danzil Does x:Bind do something to understand the difference between an ItemTemplate context and a UserControl context? Or is this a logic issue in ItemsRepeater?

In WINUI 3.0 alpha, All converters causes crash, including TextBlock converter.

@hupo376787 did you file a separate issue for this? @DanZil FYI

x:Bind has some different handling when inside of a ControlTemplate vs a DataTemplate. For this particular case, when using a converter, it expects whatever the converter returns to be castable to the property being bound to.

@hupo376787 please let me know of a sample converter markup that crashed for you in winui3.

@danzil I got it. For image, the converter should return BitmapImage.
And this is a small repo for winui 3.0 alpha.

I see what the issue is. In your DataTemplate, you are saying that the type of the items are NameS, but the collection that you are setting on the ItemsSource is a list of strings (not NameS). If you change the ItemsSource to be a collection of NameS objects, it works as expected.

 private List<NameS> listName = new List<NameS>();
        public MainPage()
        {
            this.InitializeComponent();

            for (int i = 0; i < 100; i++)
            {
                listName.Add(new NameS() { name = "Name: " + i.ToString() });
            }
        }

OK, thanks, guys.

But I wish WinUI can do something more, such as allowing string type when setting Image's Source, and int type when setting Height/Width.

Ok. That sounds like you are asking for more implicit converters. Can you file a new bug/feature request for that with more details on exactly which ones would be useful ?

@hupo376787 x:Bind is a typed binding system unlike Binding. Whatever is in the x:DataType must match the Item. If that's not the intended behavior, and you want flexibility at the expence of perf, perhaps you need to use a converter that just calls ConvertValue, or do the conversion yourself by calling the constructor for ImageSource.

Was this page helpful?
0 / 5 - 0 ratings