【问题标题】:Xamarin.Forms command binding TargetInvocationExceptionXamarin.Forms 命令绑定 TargetInvocationException
【发布时间】:2018-07-26 22:51:50
【问题描述】:

我正在编写 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>

我的完整堆栈跟踪是here

编辑:

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

【问题讨论】:

  • 不确定这是否是问题所在,但您的 XAML 有一个 CommandParameter 但您的实际命令没有
  • @Jason 我尝试删除 CommandParameter,结果相同。
  • 查看是否有提供更多细节的 InnerException 值
  • 这里没有分配信息,所以很难看出问题所在。我建议更改此行:OptionClick = new Command((sender) =&gt; page.DisplayAlert("ok", "ok", "ok"))OptionClick = new Command(() =&gt; page.DisplayAlert("ok", "ok", "ok"))
  • @ChristoNel 代码仍然导致错误。 :(

标签: c# xaml xamarin xamarin.forms xamarin.ios


【解决方案1】:

我们在这里获得的信息不多,要获得真正的错误,请尝试从另一个问题 here 中获得此答案。

如果这不起作用,请尝试逐节注释掉您的 xaml。据我们所知,错误可能在&lt;local:PostViewCell&gt;

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

【讨论】:

  • 我把上面所有的代码都贴出来了。但它只发生在 OptionClick 正在运行时。
  • 我开始收到 System.Reflection.TargetInvocationException: 现在
【解决方案2】:

尝试验证您的 android 原生自定义渲染器,在此处放置一些断点。

【讨论】:

  • 这可能是我的 android 属性中的东西吗? imgur.com/a/bKd5V
  • 你的属性没问题。您可以再次运行,等到它中断并点击 VS 上的继续按钮并获取日志吗?有时真正的问题是在中断/崩溃之后才写的。
  • 它说“您的应用程序已进入中断状态,但没有代码可显示,因为所有线程都在执行外部代码。”并单击继续使其显示“没有兼容的代码正在运行”
  • 堆栈跟踪似乎没有指出错误在哪里pastebin.com/cvZcc2Rb
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-25
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 2010-12-23
    相关资源
    最近更新 更多