Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding to an empty collection with indexing throws unhandled exception #4516

Closed
andreinitescu opened this issue Nov 22, 2018 · 2 comments · Fixed by #4525

Comments

@andreinitescu
Copy link
Contributor

commented Nov 22, 2018

Description

Binding to an empty collection when path is using property index throws unhandled exception.

The documentation doesn't say what's the expected behavior for this scenario. Other Microsoft XAML frameworks (WPF\UWP) do not throw an exception.

I hope you can see that in practice, not throwing an exception is very useful when dealing with empty collections. Empty collections coming from JSON are very common (and they're preferred over returning null).

XAML:

<Image Source="{Binding Images[0]}" />

Code:

    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            BindingContext = new MainPageViewModel();
        }
    }

    class MainPageViewModel
    {
        // Works with null value
        //public Uri[] Images { get; }

        // Doesn't work with empty array
        public Uri[] Images { get; } = { };
    }

Expected Behavior

No crash.

Actual Behavior

Throws exception

Basic Information

  • Version with issue: Latest (3.4.0.1008975)
  • Last known good version: I don't know

Reproduction Link

TestBindingToNullCollection.zip

@pauldipietro pauldipietro added this to New in Triage Nov 22, 2018

@StephaneDelcroix

This comment has been minimized.

Copy link
Member

commented Nov 23, 2018

not throwing an exception is very useful

I second that opinion

@StephaneDelcroix

This comment has been minimized.

Copy link
Member

commented Nov 23, 2018

on the plus side, I think this is handled correctly with compiled bindings. the collection is supposed to throw an IndexOutOfRangeException, and that exception is caught,

@rmarinho rmarinho moved this from New to Ready For Work in Triage Nov 23, 2018

StephaneDelcroix added a commit that referenced this issue Nov 23, 2018
[C] fallback nicely in case of IndexOutOfRangeEx
This PR does 3 things:
- In case of Binding path with indexer, lookout for
  IndexOutOfRangeException in addition to KeyNotFoundException.
- in case of KeyNotFound or IndexOutOfRange, use the FallbackValue if
  any.
- in case of an uncaught exception, throw that exception instead of a
  TargetInvocationException.

- fixes #4516

@samhouts samhouts added this to In Progress in v3.6.0 Nov 23, 2018

Triage automation moved this from Ready For Work to Closed Nov 24, 2018

StephaneDelcroix added a commit that referenced this issue Nov 24, 2018
[C] fallback nicely in case of IndexOutOfRangeEx (#4525)
This PR does 3 things:
- In case of Binding path with indexer, lookout for
  IndexOutOfRangeException in addition to KeyNotFoundException.
- in case of KeyNotFound or IndexOutOfRange, use the FallbackValue if
  any.
- in case of an uncaught exception, throw that exception instead of a
  TargetInvocationException.

- fixes #4516

@samhouts samhouts moved this from In Progress to Done in v3.6.0 Nov 26, 2018

@samhouts samhouts removed this from Closed in Triage Nov 26, 2018

@samhouts samhouts removed this from Done in v3.6.0 Jan 3, 2019

@samhouts samhouts added this to In Progress in v3.5.0 Jan 11, 2019

@samhouts samhouts moved this from In Progress to Done in v3.5.0 Jan 11, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
2 participants
You can’t perform that action at this time.