Xamarin.ListView. Как работает переплет в itemtemplate / datatemplate? - PullRequest
0 голосов
/ 12 февраля 2020

У меня есть этот код XAML.

 <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Android_1.MainPage" Title="">
    <StackLayout>
        <Label Text="{Binding Type}"/>
        <Label Text="{Binding Question}"/>
        <ListView ItemsSource="{Binding Answers}"/>
        <Button Text="Next Question" Clicked="Button_Clicked"/>
    </StackLayout>
</ContentPage>

В классе MainPage C# код:

        public MyView answerView;
        public MainPage()
        {
            InitializeComponent();
            answerView = new MyView();
            this.BindingContext = answerView;
        }
        private void Button_Clicked(object sender, EventArgs e)
        {
             answerView.QuestCase = Program.GetRandomCase();
        }

Теперь все работает. Код выше работает. Все привязки работают.

Когда я изменяю XAML, привязка в ListView перестает работать, привязка в метках все еще работает:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Android_1.MainPage" Title="">
    <StackLayout>
        <Label Text="{Binding Type}"/>
        <Label Text="{Binding Question}"/>
        <ListView ItemsSource="{Binding Answers}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label LineBreakMode="WordWrap"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Button Text="Next Question" Clicked="Button_Clicked"/>
    </StackLayout>
</ContentPage>

Как ее решить? Спасибо.


И класс MyView:

  public class MyView : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private QuestCase questCase;

            public QuestCase QuestCase
            {
                get { return questCase; }
                set {
                    if(value != questCase)
                    {
                        questCase = value;
                        OnPropertyChanged("Question");
                        OnPropertyChanged("Type");
                        OnPropertyChanged("Answers");                  
                    }
                }
            }
            public MyView()
            {
                questCase = Program.GetRandomCase();
            }
            public string Question
            {
                get { return questCase.Question; }
            }
            public string Type
            {
                get { return questCase.Type; }
            }
            public string[] Answers
            {
                get { return questCase.Answers.Keys.ToArray(); }
            }

            protected void OnPropertyChanged(string propName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

1 Ответ

0 голосов
/ 12 февраля 2020
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local ="clr-namespace:Android_1"
             x:Class="Android_1.MainPage" Title="">
    <ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="MyDataTemplate">
                <ViewCell>
                    <Label LineBreakMode="WordWrap" Text="{Binding}"></Label>
                </ViewCell>
            </DataTemplate>
        </ResourceDictionary>
    </ContentPage.Resources>
    <StackLayout>
        <Label Text="{Binding Type}"/>
        <Label Text="{Binding Question}"/>
        <ListView x:Name="ListAnswers" ItemsSource="{Binding Answers}" ItemTemplate="{x:StaticResource MyDataTemplate}">
        </ListView>
        <Button Text="Next Question" Clicked="Button_Clicked"/>
    </StackLayout>
</ContentPage>

Это работает!

...