Xamarin.Forms命令绑定TargetInvocationException

3

我正在编写一个Xamarin PCL应用程序,但是我一直遇到这个错误:

System.Reflection.TargetInvocationException: 目标调用的异常。

有时也会出现以下错误:

System.Reflection.TargetInvocationException: <超时获取异常详情>

当我点击在XAML中绑定到OptionClick行的图像并且在C#中是 new Command((sender) => ShowOptionActions(message.Id, message.Sender_Id, sender)) 时,才会出现此错误。我尝试将其更改为DisplayAlert而不是该方法,但无论我放什么都会在单击它时出现错误。

它只出现在Android上,对于iOS它运行正常。它们都使用相同的代码。

我的ObjectMessage类是:

public class MessageObject : INotifyPropertyChanged
{

    private int Id = -1;
    private Command optionCommandValue;
    private string bodyValue = String.Empty;
    private Color bodyColorValue = Color.Black;
    private string likeImageSource = String.Empty;
    private Command likeCommandValue;
    private string timestampValue = String.Empty;
    private Boolean showBannersValue = true;

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private MessageObject(int id, Command optionCommand, string body, string likeImage, Command likeCommand, string timestamp)
    {
        Id = id;
        optionCommandValue = optionCommand;
        bodyValue = body;
        bodyColorValue = Color.Black;
        likeImageSource = likeImage;
        likeCommandValue = likeCommand;
        timestampValue = timestamp;
        showBannersValue = true;
    }

    public static MessageObject CreateMessage(int id, Command optionCommand, string body, string likeImage, Command likeCommand, string timestamp)
    {
        return new MessageObject(id, optionCommand, body, likeImage, likeCommand, timestamp);
    }

    public int ID
    {
        get
        {
            return this.Id;
        }
    }

    public Command OptionClick
    {
        get
        {
            return this.optionCommandValue;
        }

        set
        {
            if (value != this.optionCommandValue)
            {
                this.optionCommandValue = value;
                NotifyPropertyChanged();
            }
        }
    }

    public string Body
    {
        get
        {
            return this.bodyValue;
        }

        set
        {
            if (value != this.bodyValue)
            {
                this.bodyValue = value;
                NotifyPropertyChanged();
            }
        }
    }

    public Color BodyColor
    {
        get
        {
            return this.bodyColorValue;
        }

        set
        {
            if (value != this.bodyColorValue)
            {
                this.bodyColorValue = value;
                NotifyPropertyChanged();
            }
        }
    }

    public string LikeImageSource
    {
        get
        {
            return this.likeImageSource;
        }

        set
        {
            if (value != this.likeImageSource)
            {
                this.likeImageSource = value;
                NotifyPropertyChanged();
            }
        }
    }

    public Command LikeClick
    {
        get
        {
            return this.likeCommandValue;
        }

        set
        {
            if (value != this.likeCommandValue)
            {
                this.likeCommandValue = value;
                NotifyPropertyChanged();
            }
        }
    }

    public string Timestamp
    {
        get
        {
            return this.timestampValue;
        }

        set
        {
            if(value != this.timestampValue)
            {
                this.timestampValue = value;
                NotifyPropertyChanged();
            }
        }
    }

    public Boolean ShowBanners
    {
        get
        {
            return this.showBannersValue;
        }

        set
        {
            if (value != this.showBannersValue)
            {
                this.showBannersValue = value;
                NotifyPropertyChanged();
            }
        }
    }
}

我使用以下代码创建一个MessageObject:
MessageObject mo = MessageObject.CreateMessage(
                message.Id,
                new Command((sender) => ShowOptionActions(message.Id, message.Sender_Id, sender)),
                message.Body,
                message.Liked == 0 ? "like_icon.png" : "liked_icon.png",
                new Command((sender) => LikeMessageClick(message.Id, sender)),
                dateFormat.ToString("MMMM dd, yyyy HH:mm"));

我的XAML代码是

<ContentPage.Content>
    <StackLayout BackgroundColor="#7ed6df">
        <local:PostListView x:Name="MessageView" HasUnevenRows="True" IsPullToRefreshEnabled="True" Refreshing="MessageView_Refreshing" SeparatorVisibility="None" BackgroundColor="#7ed6df">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <local:PostViewCell>
                        <StackLayout>
                            <StackLayout x:Name="MessageLayout" BackgroundColor="White" Margin="10, 10, 10, 10" Padding="10, 10, 15, 10">
                                <Image Source="options_icon.png" HeightRequest="18" HorizontalOptions="End" Margin="5, 0, 5, 0" IsVisible="{Binding ShowBanners}">
                                    <Image.GestureRecognizers>
                                        <TapGestureRecognizer Command="{Binding OptionClick}" CommandParameter="{Binding .}"/>
                                    </Image.GestureRecognizers>
                                </Image>
                                <Label Text="{Binding Body}" HorizontalOptions="CenterAndExpand" TextColor="{Binding BodyColor}" FontSize="15" Margin="0, 10, 0, 10"/>
                                <StackLayout x:Name="MessageFooter" Orientation="Horizontal" IsVisible="{Binding ShowBanners}">
                                    <Image x:Name="LikeSource" Source="{Binding LikeImageSource}" HeightRequest="20" HorizontalOptions="StartAndExpand" Margin="0, 0, 10, 0">
                                        <Image.GestureRecognizers>
                                            <TapGestureRecognizer Command="{Binding LikeClick}" CommandParameter="{Binding .}"/>
                                        </Image.GestureRecognizers>
                                    </Image>
                                    <Label Text="{Binding Timestamp}" TextColor="Black" FontSize="10" HorizontalOptions="EndAndExpand"/>
                                </StackLayout>
                            </StackLayout>
                        </StackLayout>
                    </local:PostViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </local:PostListView>
    </StackLayout>
</ContentPage.Content>

我的完整堆栈跟踪在这里

编辑:

我为一个新项目添加了一个新的TapGesture,但它也有同样的问题。


@Jason 我尝试移除CommandParameter,但结果相同。 - Dan
查看是否有 InnerException 值提供了更多的细节。 - Jason
这里的信息不太充分,很难看出问题所在。 我建议将这一行更改为: OptionClick = new Command(() => page.DisplayAlert("ok", "ok", "ok")) - Christo Nel
你能展示一下 OptionClick 的定义以及异常的堆栈跟踪吗?(请勿作为评论,编辑问题) - Sven-Michael Stübe
我添加了所有的代码。@ChristoNel - Dan
显示剩余6条评论
3个回答

2

这里提供的信息不是很多,为了得到真正的错误信息,请参考另一个问题这里

如果那个方法无效,请逐个注释XAML部分。我们不知道错误可能在<local:PostViewCell>中。

另外,你的ViewModel在哪里?它需要一个BindableBase。


我已经发布了所有的代码。但它只会在运行OptionClick时发生。 - Dan
我现在开始收到“_System.Reflection.TargetInvocationException: <超时获取异常详细信息>_”的错误信息。 - Dan

1
尝试验证您的Android本地自定义渲染器,将一些断点放在那里。

可能是我的安卓属性有什么问题吗?https://imgur.com/a/bKd5V - Dan
你的属性没问题。你能再运行一次,等到它崩溃后在VS上点击继续按钮并获取日志吗?有时真正的问题只会在崩溃后才被记录下来。 - Alan Jones Rios
它说:“您的应用程序已进入中断状态,但没有代码可显示,因为所有线程都在执行外部代码。”单击“继续”会显示“没有兼容的代码正在运行”。 - Dan
堆栈跟踪好像没有指向错误的地方。https://pastebin.com/cvZcc2Rb - Dan

1

对于该命令,尝试在主线程上运行“ShowOptionActions”方法。您可以在ShowOptionActions方法内部执行此操作,也可以像这样执行:

new Command((sender) =>
    Device.BeginInvokeOnMainThread(() => { 
        ShowOptionActions(message.Id, message.Sender_Id, sender));
    })

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接