【问题标题】:'Exception has been thrown by the target of an invocation' during data binding数据绑定期间“调用目标引发异常”
【发布时间】:2021-01-14 09:53:03
【问题描述】:

我收到以下错误'System.Reflection.TargetInvocationException: '异常已被调用的目标抛出。'

这是我的代码

ContactModel ConversationPartner;
        
        public MessagesPage(ContactModel input)
        {
            InitializeComponent();
            NavigationPage.SetHasNavigationBar(this, false);
            ConversationsList = new ObservableCollection<ConversationModel>();
            ConversationPartner = input;
           ...


          }
           ...
 public string FirstContactName
        {
            get { return ConversationPartner.contactName[0]; }
        }

        public string SecondContactId
        {
            get { return ConversationPartner.contactID[1]; }
        }

        public string SecondContactName
        {
            get { return ConversationPartner.contactName[1]; }
        }

这是我的 XAML,特别是我尝试绑定 FirstContactName、SecondContactId 和 SecondContactName 的地方

<ContentPage.BindingContext>
        <x:Reference Name="messagesPage" />
    </ContentPage.BindingContext>

...

 <Label Text="{Binding FirstContactName}" FontSize="Title" TextColor="Black"
                           VerticalOptions="Center" FontAttributes="Bold" HorizontalOptions="StartAndExpand">
                        <Label.Triggers>
                            <DataTrigger TargetType="Label" Binding="{Binding SecondContactId, Converter={StaticResource isViewerConverter}}" Value="False">
                                <Setter Property="Text"  Value="{Binding SecondContactName}"/>
                            </DataTrigger>
                        </Label.Triggers>
                    </Label>

【问题讨论】:

  • 您在哪里将数据源绑定到内容页面?只是使用&lt;ContentPage.BindingContext&gt; &lt;x:Reference Name="messagesPage" /&gt; &lt;/ContentPage.BindingContext&gt;,请看Data Bindings to mvvm 修改你的binging。

标签: c# xamarin xamarin.forms xamarin.android


【解决方案1】:

您还必须为此字段设置属性,因为数据绑定双向工作 或者您也可以将数据绑定方式更改为一种方式。

祝你好运!

【讨论】:

  • 如何插入 return ConversationPartner.contactName[1];如果它最初在 setter 中?
  • o 将绑定更改为一种方式,您不必在此属性中添加集合。编辑:更改数据绑定方式只需将 Text="{Binding FirstContactName}" 更改为 Text="{Binding Path =FirstContactName, Mode=OneWay}"
猜你喜欢
  • 1970-01-01
  • 2019-01-22
  • 2018-11-08
  • 2018-02-14
  • 1970-01-01
  • 2016-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多