繁体   English   中英

Xamarin.Forms命令绑定TargetInvocationException

[英]Xamarin.Forms command binding TargetInvocationException

我正在编写一个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,它也有同样的问题。

没有,我们在这里得到太多的信息,要得到真正的错误尝试从另一个问题这个答案在这里

如果这不起作用,请尝试按部分注释掉您的xaml。 我们所知道的错误可能出现在<local:PostViewCell>中。

你的ViewModel在哪里? 它需要一个BindableBase

尝试验证您的Android原生自定义渲染器,在那里放置一些断点。

对于该命令,请尝试在主线程上运行“ShowOptionActions”方法。 您可以在ShowOptionActions方法本身中执行此操作,或者像这样:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM