XAML

问题描述

我的 XAML 中有这些代码

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

    ....

  <Label Text="{Binding ConversationPartner.contactName[0]}" FontSize="Title" TextColor="Black"
                           VerticalOptions="Center" FontAttributes="Bold" HorizontalOptions="StartAndExpand">
                        <Label.Triggers>
                            <DataTrigger targettype="Label" Binding="{Binding ConversationPartner.contactID[1],Converter={StaticResource isViewerConverter}}" Value="False">
                                <Setter Property="Text"  Value="{Binding ConversationPartner.contactName[1]}"/>
                            </DataTrigger>
                        </Label.Triggers>
                    </Label>

我想要发生的是 ConversationPartner.contactName[0] 表示的标签上的名称必须出现在我的应用程序中,但它没有。

这是后面的代码

public partial class MessagesPage : ContentPage
{
        DataClass dataClass = DataClass.GetInstance;
        public ICommand CloseMsg => new Command(async () => await Navigation.PopModalAsync(true));
        public ICommand SendCommand => new Command(Send);
        
        ContactModel ConversationPartner;
        public MessagesPage(ContactModel input)
        {
            InitializeComponent();
            NavigationPage.SetHasNavigationBar(this,false);
            ConversationsList = new ObservableCollection<ConversationModel>();
            ConversationPartner = input;
            /// some cloud firestore code here
        }
}

解决方法

找到了解决方案。

我制作了一个可观察的集合,以便我可以轻松地绑定。 我背后的代码:

     public MessagesPage(ContactModel input)
        {
            InitializeComponent();
            NavigationPage.SetHasNavigationBar(this,false);
            ConversationsList = new ObservableCollection<ConversationModel>();
            convoers = new ObservableCollection<string> {input.contactName[0],input.contactName[1],input.contactID[1] };
            ConversationPartner = input;
            ...
        }

然后在我的 XAML 中,

 <Label Text="{Binding convoers[0]}" FontSize="Title" TextColor="Black"
                           VerticalOptions="Center" FontAttributes="Bold" HorizontalOptions="StartAndExpand">
                        <Label.Triggers>
                            <DataTrigger TargetType="Label" Binding="{Binding convoers[2],Converter={StaticResource isViewerConverter}}" Value="False">
                                <Setter Property="Text"  Value="{Binding convoers[1]}"/>
                            </DataTrigger>
                        </Label.Triggers>
                    </Label>