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