嘿,我是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获得的错误消息并没有帮助。