Xamarin 表单 System.Reflection.TargetInvocationException:'Exception has been thrown by the target of an invocation.'
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 正在运行,Binding 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.'
感谢 Jason 的评论,我发现我忘了初始化团队 class。不幸的是,我从 Xamarin 收到的错误消息并没有那么有用。
嘿,我是 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 正在运行,Binding 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.'
感谢 Jason 的评论,我发现我忘了初始化团队 class。不幸的是,我从 Xamarin 收到的错误消息并没有那么有用。