зеркало из https://github.com/DeGsoft/maui-linux.git
[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
This commit is contained in:
Родитель
94e6621f72
Коммит
9d9140eeb6
|
@ -610,15 +610,16 @@ namespace Xamarin.Forms
|
||||||
{
|
{
|
||||||
if (IsIndexer)
|
if (IsIndexer)
|
||||||
{
|
{
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
value = LastGetter.Invoke(value, Arguments);
|
value = LastGetter.Invoke(value, Arguments);
|
||||||
}
|
}
|
||||||
catch (TargetInvocationException ex)
|
catch (TargetInvocationException ex) {
|
||||||
{
|
if (ex.InnerException is KeyNotFoundException || ex.InnerException is IndexOutOfRangeException) {
|
||||||
if (!(ex.InnerException is KeyNotFoundException))
|
value = null;
|
||||||
throw;
|
return false;
|
||||||
value = null;
|
}
|
||||||
|
else
|
||||||
|
throw ex.InnerException;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -197,7 +197,7 @@ namespace Xamarin.Forms.Internals
|
||||||
if (isTSource) {
|
if (isTSource) {
|
||||||
try {
|
try {
|
||||||
value = GetSourceValue(_getter((TSource)sourceObject), property.ReturnType);
|
value = GetSourceValue(_getter((TSource)sourceObject), property.ReturnType);
|
||||||
} catch (Exception ex) when (ex is NullReferenceException || ex is KeyNotFoundException) {
|
} catch (Exception ex) when (ex is NullReferenceException || ex is KeyNotFoundException || ex is IndexOutOfRangeException) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!BindingExpression.TryConvert(ref value, property, property.ReturnType, true)) {
|
if (!BindingExpression.TryConvert(ref value, property, property.ReturnType, true)) {
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ContentPage
|
||||||
|
xmlns="http://xamarin.com/schemas/2014/forms"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
|
||||||
|
xmlns:local="using:Xamarin.Forms.Xaml.UnitTests"
|
||||||
|
x:Class="Xamarin.Forms.Xaml.UnitTests.Gh4516">
|
||||||
|
<Image x:Name="image" Source="{Binding Images[0], FallbackValue='foo.jpg'}" />
|
||||||
|
</ContentPage>
|
|
@ -0,0 +1,36 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using Xamarin.Forms;
|
||||||
|
using Xamarin.Forms.Core.UnitTests;
|
||||||
|
|
||||||
|
namespace Xamarin.Forms.Xaml.UnitTests
|
||||||
|
{
|
||||||
|
public class Gh4516VM {
|
||||||
|
public Uri[] Images { get; } = { };
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class Gh4516 : ContentPage
|
||||||
|
{
|
||||||
|
public Gh4516() => InitializeComponent();
|
||||||
|
public Gh4516(bool useCompiledXaml)
|
||||||
|
{
|
||||||
|
//this stub will be replaced at compile time
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
class Tests
|
||||||
|
{
|
||||||
|
[SetUp] public void Setup() => Device.PlatformServices = new MockPlatformServices();
|
||||||
|
[TearDown] public void TearDown() => Device.PlatformServices = null;
|
||||||
|
|
||||||
|
[TestCase(true), TestCase(false)]
|
||||||
|
public void BindingToEmptyCollection(bool useCompiledXaml)
|
||||||
|
{
|
||||||
|
Gh4516 layout = null;
|
||||||
|
Assert.DoesNotThrow(() => layout = new Gh4516(useCompiledXaml) { BindingContext = new Gh4516VM() });
|
||||||
|
Assert.That((layout.image.Source as FileImageSource).File, Is.EqualTo("foo.jpg"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Загрузка…
Ссылка в новой задаче