繁体   English   中英

Xamarin Forms System.Reflection.TargetInvocationException: '调用的目标已抛出异常。'

[英]Xamarin Forms System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'

嘿,我是 Xamarin Forms 的新手,不知何故我看不透那个有约束力的东西。

这是我设置 BindingContext 的代码:

public MainPage()
{
    InitializeComponent();
    BindingContext = new GameViewModel();
}

这是 ViewModel:

public class GameViewModel
{
    private Team Team1 { get; set; }
    public Team Team2 { get; set; }

    public string Team1Name { get { return Team1.Name; } }
    public string StaticString { get { return "static"; } }

}

StaticString 正在工作,绑定 Team1.Name 未显示任何内容,而 Team1Name 正在引发异常。

这是视图:

<StackLayout Orientation="Horizontal">
    <StackLayout Orientation="Vertical">
        <Entry Placeholder="{Binding Team1.Name}" />
        <Label Text="Welcome to Xamarin.Forms!" />
    </StackLayout>
    <StackLayout Orientation="Vertical">
        <Entry Placeholder="{Binding Team1Name}" />
        <Label Text="Welcome to Xamarin.Forms!" />
    </StackLayout>
</StackLayout>

我得到的例外是

System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'

感谢 Jasons 的评论,我发现我忘记初始化 Team 类。 不幸的是,我从 Xamarin 收到的错误消息并没有那么有用。

暂无
暂无

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

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