为什么我一刷我的CarouselView就得到一个目标调用异常?
自从我开始学习.NET MAUI以来,CarouselView一直工作,但由于某种原因,现在它不再工作了。就我而言,我没有做任何奇怪的事情,只是一个<CarouselView/>
,一个ItemsSource
和(显然是这里的罪魁祸首)一个CurrentItem
,现在当它以任何方式改变时,它会突然抛出一个异常。无论是将Command与当前项一起用作CommandParameter,还是直接绑定CurrentItem属性,都将引发相同的异常。System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'
一切都正常工作,直到我将CurrentItem
添加到CarouselView
。它将呈现页面,但只要我滑动查看其中的其他项目,应用程序就会崩溃。直接绑定CurrentItem
或使用命令更改它:
CurrentItemChangedCommand="{Binding CarouselItemChangedCommand}"
CurrentItemChangedCommandParameter="{Binding Source={RelativeSource Self}, Path=CurrentItem}"
都抛出这个异常。
简单的CarouselView
:
<VerticalStackLayout>
<CarouselView ItemsSource="{Binding Supports}"
CurrentItem="{Binding Support}"
IndicatorView="indicatorView"
IsSwipeEnabled="True"
Loop="False">
<CarouselView.ItemTemplate>
<DataTemplate x:DataType="m:Support">
<VerticalStackLayout HorizontalOptions="Center">
<Frame Style="{StaticResource ItemFrameVisible}"
StyleClass="ItemFrame"
Padding="0,16,0,8"
Margin="0,16">
<VerticalStackLayout>
<Image Source="{Binding ImageFileSuffix, StringFormat='sp_{0}.svg'}"
WidthRequest="280"/>
<Label Text="{Binding Title}"
Style="{StaticResource TitleTiny}"
StyleClass="Title"
HorizontalOptions="Center"
Margin="0,-56,0,0"/>
</VerticalStackLayout>
</Frame>
</VerticalStackLayout>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
<IndicatorView SelectedIndicatorColor="{StaticResource GreenMain}"
IndicatorColor="{StaticResource Gray200}"
HorizontalOptions="Center"
x:Name="indicatorView"
MaximumVisible="14"
IndicatorSize="10"
Margin="0,12,0,0"/>
</VerticalStackLayout>
将Loop
设置为true或false也没有区别。从CarouselView
中删除CurrentItem
属性解决了这个问题,但我不再能够使用ViewModel中的当前项来执行任务。
视图模型
private Support support;
public Support Support
{
get => support;
set => SetProperty(ref support, value);
}
public ObservableRangeCollection<Support> Supports { get; set; } = new();
BaseViewModel
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetProperty<TValue>(ref TValue backingField, TValue value, [CallerMemberName] string propertyName = null)
{
if (Comparer<TValue>.Default.Compare(backingField, value) == 0)
{
return false;
}
backingField = value;
OnPropertyChanged(propertyName);
return true;
}
支持模式
标题、描述和ImageFileSuffix等数据在显示数据时正在工作,即使在滑动时,我也可以在应用程序崩溃之前清楚地看到下一个或上一个项目数据。
[Table("Support")]
public class Support
{
[PrimaryKey]
public int SupportId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string ImageFileSuffix { get; set; }
}
调用堆栈
0xFFFFFFFFFFFFFFFF in Android.Runtime.JNIEnv.monodroid_debugger_unhandled_exception C# Annotated Frame
> 0x1A in Android.Runtime.JNINativeWrapper._unhandled_exception at C:\Users\genericUser13\AppData\Local\SourceServer\bb123c2c4d433ebca46498fd0d4b0728a9cd5eedc6e5d8398523e286da2078c9\JNINativeWrapper.g.cs:12,5 C# Non-user code
0x22 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLII_V at C:\Users\genericUser13\AppData\Local\SourceServer\bb123c2c4d433ebca46498fd0d4b0728a9cd5eedc6e5d8398523e286da2078c9\JNINativeWrapper.g.cs:287,26 C# Non-user code
0x32 in System.Reflection.MethodInvoker.InterpretedInvoke C# Annotated Frame
0x4 in System.Reflection.MethodInvoker.InlinedInvoke C# Annotated Frame
0xCE in System.Reflection.RuntimeMethodInfo.Invoke C# Annotated Frame
0x6 in System.Reflection.MethodBase.Invoke C# Annotated Frame
0x31C in Microsoft.Maui.Controls.BindingExpression.ApplyCore at C:\Users\genericUser13\AppData\Local\SourceServer\62e8268ae92440603c63914b6d8a3b490f47ceaad21fcc1a5e51804834c6a305\BindingExpression.cs:188,5 C# Non-user code
0x48 in Microsoft.Maui.Controls.BindingExpression.Apply at C:\Users\genericUser13\AppData\Local\SourceServer\62e8268ae92440603c63914b6d8a3b490f47ceaad21fcc1a5e51804834c6a305\BindingExpression.cs:56,5 C# Non-user code
0x27 in Microsoft.Maui.Controls.Binding.Apply at C:\Users\genericUser13\AppData\Local\SourceServer\6dd575e617d2dceeb55a7d6f2e56ce8ff72455f7905177f12ed1e461a05ed3b4\Binding.cs:114,4 C# Non-user code
0x108 in Microsoft.Maui.Controls.BindableObject.SetValueActual at C:\Users\genericUser13\AppData\Local\SourceServer\aefda1b85f6059f8ec86428ab2810170588db594e00bdaaaf0dcb2947296edc9\BindableObject.cs:529,6 C# Non-user code
0x20C in Microsoft.Maui.Controls.BindableObject.SetValueCore at C:\Users\genericUser13\AppData\Local\SourceServer\aefda1b85f6059f8ec86428ab2810170588db594e00bdaaaf0dcb2947296edc9\BindableObject.cs:469,5 C# Non-user code
0x5 in Microsoft.Maui.Controls.BindableObject.SetValueCore at C:\Users\genericUser13\AppData\Local\SourceServer\aefda1b85f6059f8ec86428ab2810170588db594e00bdaaaf0dcb2947296edc9\BindableObject.cs:401,7 C# Non-user code
0x4 in Microsoft.Maui.Controls.Element.SetValueFromRenderer at C:\Users\genericUser13\AppData\Local\SourceServer\256b87211b5393e640c1d246067a29d574a61932c703f979f99c781ccba9b4c2\Element.cs:261,4 C# Non-user code
0x61 in Microsoft.Maui.Controls.Handlers.Items.MauiCarouselRecyclerView.SetCurrentItem at C:\Users\genericUser13\AppData\Local\SourceServer\097a9d3b14092fcbf9468085ff0c7d892d23df3ce428f46e9656242399956b02\MauiCarouselRecyclerView.cs:456,4 C# Non-user code
0x119 in Microsoft.Maui.Controls.Handlers.Items.MauiCarouselRecyclerView.Microsoft.Maui.Controls.Handlers.Items.IMauiCarouselRecyclerView.UpdateFromPosition at C:\Users\genericUser13\AppData\Local\SourceServer\097a9d3b14092fcbf9468085ff0c7d892d23df3ce428f46e9656242399956b02\MauiCarouselRecyclerView.cs:508,4 C# Non-user code
0xB in Microsoft.Maui.Controls.Handlers.Items.CarouselViewHandler.MapPosition at C:\Users\genericUser13\AppData\Local\SourceServer\f03296ca69e671647e1230133f792254ee365c7cbaa947f2f3077fdf8d867aab\CarouselViewHandler.Android.cs:45,4 C# Non-user code
0x21 in Microsoft.Maui.PropertyMapper<Microsoft.Maui.Controls.CarouselView,Microsoft.Maui.Controls.Handlers.Items.CarouselViewHandler>. at C:\Users\genericUser13\AppData\Local\SourceServer\00bf22cc3914378ecafa781a5dea0f3c344a52a09ca64c376cb2202c4c2e1ca3\PropertyMapper.cs:172,6 C# Non-user code
0x17 in Microsoft.Maui.PropertyMapper.UpdatePropertyCore at C:\Users\genericUser13\AppData\Local\SourceServer\00bf22cc3914378ecafa781a5dea0f3c344a52a09ca64c376cb2202c4c2e1ca3\PropertyMapper.cs:47,4 C# Non-user code
0x8 in Microsoft.Maui.PropertyMapper.UpdateProperty at C:\Users\genericUser13\AppData\Local\SourceServer\00bf22cc3914378ecafa781a5dea0f3c344a52a09ca64c376cb2202c4c2e1ca3\PropertyMapper.cs:72,4 C# Non-user code
0x1C in Microsoft.Maui.Handlers.ElementHandler.UpdateValue at C:\Users\genericUser13\AppData\Local\SourceServer\13aa2389aeaa5f2f85d844344acd9ffd5bb52b2308b48237d1a1f7aba0bc87cd\ElementHandler.cs:87,4 C# Non-user code
0x14 in Microsoft.Maui.Controls.Element.OnPropertyChanged at C:\Users\genericUser13\AppData\Local\SourceServer\256b87211b5393e640c1d246067a29d574a61932c703f979f99c781ccba9b4c2\Element.cs:383,4 C# Non-user code
0x11B in Microsoft.Maui.Controls.BindableObject.SetValueActual at C:\Users\genericUser13\AppData\Local\SourceServer\aefda1b85f6059f8ec86428ab2810170588db594e00bdaaaf0dcb2947296edc9\BindableObject.cs:533,5 C# Non-user code
0x20C in Microsoft.Maui.Controls.BindableObject.SetValueCore at C:\Users\genericUser13\AppData\Local\SourceServer\aefda1b85f6059f8ec86428ab2810170588db594e00bdaaaf0dcb2947296edc9\BindableObject.cs:469,5 C# Non-user code
0x5 in Microsoft.Maui.Controls.BindableObject.SetValueCore at C:\Users\genericUser13\AppData\Local\SourceServer\aefda1b85f6059f8ec86428ab2810170588db594e00bdaaaf0dcb2947296edc9\BindableObject.cs:401,7 C# Non-user code
0x4 in Microsoft.Maui.Controls.Element.SetValueFromRenderer at C:\Users\genericUser13\AppData\Local\SourceServer\256b87211b5393e640c1d246067a29d574a61932c703f979f99c781ccba9b4c2\Element.cs:261,4 C# Non-user code
0x3A in Microsoft.Maui.Controls.Handlers.Items.MauiCarouselRecyclerView.UpdatePosition at C:\Users\genericUser13\AppData\Local\SourceServer\097a9d3b14092fcbf9468085ff0c7d892d23df3ce428f46e9656242399956b02\MauiCarouselRecyclerView.cs:447,5 C# Non-user code
0x42 in Microsoft.Maui.Controls.Handlers.Items.MauiCarouselRecyclerView.CarouselViewScrolled at C:\Users\genericUser13\AppData\Local\SourceServer\097a9d3b14092fcbf9468085ff0c7d892d23df3ce428f46e9656242399956b02\MauiCarouselRecyclerView.cs:417,4 C# Non-user code
0xE in Microsoft.Maui.Controls.ItemsView.SendScrolled at C:\Users\genericUser13\AppData\Local\SourceServer\4fe8e933f734ce9480a3c27fc094f2c3ee717b35003a01f015cbf8402e421275\ItemsView.cs:225,4 C# Non-user code
0xA9 in Microsoft.Maui.Controls.Handlers.Items.RecyclerViewScrollListener<Microsoft.Maui.Controls.CarouselView,Microsoft.Maui.Controls.Handlers.Items.IItemsViewSource>.OnScrolled at C:\Users\genericUser13\AppData\Local\SourceServer\b212eda26d70d29f6668cbfaaf86f1a91cb82a7f5cac658b3d34bb2e99f6fb81\RecyclerViewScrollListener.cs:55,4 C# Non-user code
0x4 in Microsoft.Maui.Controls.Handlers.Items.CarouselViewOnScrollListener.OnScrolled at C:\Users\genericUser13\AppData\Local\SourceServer\e515b26a062e48cba2b6fd7d25c47f0cbd339d0a041b04aabf55197dc71a562f\CarouselViewOnScrollListener.cs:33,4 C# Non-user code
0x14 in AndroidX.RecyclerView.Widget.RecyclerView.OnScrollListener.n_OnScrolled_Landroidx_recyclerview_widget_RecyclerView_II at C:\a\_work\3\s\generated\androidx.recyclerview.recyclerview\obj\Release\net6.0-android\generated\src\AndroidX.RecyclerView.Widget.RecyclerView.cs:9152,5 C# Annotated Frame
0xD in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLII_V at C:\Users\genericUser13\AppData\Local\SourceServer\bb123c2c4d433ebca46498fd0d4b0728a9cd5eedc6e5d8398523e286da2078c9\JNINativeWrapper.g.cs:286,5 C# Non-user code
1条答案
按热度按时间bfrts1fy1#
我可以重现你的问题,问题是在
BaseViewModel
的函数protected bool SetProperty<TValue>()
中。请尝试将其更改为以下代码:
注:
请不要忘记为您的绑定添加符号
}
,就像ToolmakerSteve提到的那样。