简体   繁体   中英

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

Hey I am new to Xamarin Forms and I somehow don't see through that binding thing.

Here is the Code where I set the BindingContext:

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

And here is the 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 is working, Binding Team1.Name is displaying nothing and Team1Name is throwing the exception.

And here is the View:

<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>

The Exception I get is

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

Thanks to Jasons comment I figured out that i forgot to initialize the Team class. Unfortunately the error message I got from Xamarin wasn't that helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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