CollectionView, DataBinding, MVVM - PullRequest
       0

CollectionView, DataBinding, MVVM

0 голосов
/ 23 января 2020

Привет всем,

Я пытаюсь реализовать MVVM в своем приложении, но мне не удается загрузить данные. У меня есть главная страница, и она работает нормально, но как только я хочу перейти к деталям моего объявления, а затем страница сведений пуста. Есть мои элементы управления, поэтому я знаю, что страницы загружены, только мои данные отсутствуют. Кажется, я не могу понять, где я совершил ошибку. Я новичок в кодировании, поэтому я иногда склонен делать очень глупые ошибки. Я пытался решить эту проблему в течение последних нескольких часов, иначе я бы не разместил это здесь. Спасибо за любое мнение.

My HomePage 

<Grid Grid.Row="1" Margin="0,25,0,0">
                    <CollectionView x:Name="ads"
                    ItemsSource="{Binding AdLogEntries}"
                        ItemTemplate="{StaticResource HomePageTemplate}"
                        SelectionMode="Single"
                        SelectedItem="{Binding SelectedAd, Mode=TwoWay}"
                        SelectionChanged="CollectionView_SelectionChanged"
                        Margin="12,0">
                     </CollectionView>
                </Grid>


public partial class HomePage : ContentPage

    {
        public HomePage()
        {
            InitializeComponent();
            BindingContext = new HomePage ViewModel();
        }
        async  void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedAd = (AdLogEntry)e.CurrentSelection.FirstOrDefault();
            if (selectedAd != null)
            {
                await Navigation.PushAsync(new AdDetail(selectedAd));
            }
        }
    }

namespace Baltazar.ViewModel

{
    public class HomePageViewModel : BaseViewModel

    {

        ObservableCollection<AdLogEntry> _adLogEntries;
        public ObservableCollection<AdLogEntry> AdLogEntries { get => _adLogEntries; set { _adLogEntries = value; OnPropertyChanged(); } }
        public AdLogEntry selectedAd;
        public HomePageViewModel()
        {
            AdLogEntries = new ObservableCollection<AdLogEntry>()


            {
                new AdLogEntry (){Id = 0, Image = "cat.jpg", Name = "Kocka" , Description = "seda kocka na prodej, mayliva, hrava, spolecenska " ,Price = 120, },
                new AdLogEntry (){Id = 1, Image = "cat2.jpg", Name = "Kocka", Description = "seda kocka na prodej, mayliva, hrava, spolecenska " ,Price = 120, },
                new AdLogEntry (){Id = 2, Image = "bobtailjpg.jpg", Name = "Kocka",Description = "seda kocka na prodej, mayliva, hrava, spolecenska ", Price = 120, },
            };

        }
    }
}
//Detail
 public class AdDetailViewModel : BaseViewModel
    {
        AdLogEntry _adDetail;

        public AdLogEntry AdDetail { get => _adDetail;set { _adDetail = value;OnPropertyChanged(); } }

        public AdDetailViewModel(AdLogEntry adDetail)
        {
            AdDetail = adDetail;
        }

    }
 public partial class AdDetail : ContentPage


    {
        //private AdLogEntry selectedAd;

        public AdDetail(AdLogEntry adDetail)
        {
            InitializeComponent();
            BindingContext = new AdDetailViewModel(adDetail);
        }


    }


//BaseViewModel
public class BaseViewModel : INotifyPropertyChanged

    {
        public event PropertyChangedEventHandler PropertyChanged; 

        protected BaseViewModel()
        {

        }

        protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="300" />
            <RowDefinition Height="40" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ffimageloading:CachedImage
      x:Name="HeaderView"
      Grid.Row="0"
      Grid.RowSpan="2"
      Aspect="AspectFill"
      Source="{Binding AdLogEntries.Image}"/>
        <controls:Parallax Control
      x:Name="Parallax"
      Grid.Row="0"
      Grid.RowSpan="3">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="300" />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <yummy:Pancake View
          Grid.Row="1"
          CornerRadius="24, 24, 0, 0"
          BackgroundColor="{StaticResource BackgroundColor}"
          Margin="0, 20, 0, 0">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <!-- Animal NAME -->
                        <Label
              Grid.Row="0"
              Text="{Binding AdLogEntries.Name}"
              />
                        <!-- QUANTITY -->
                        <Grid
              Grid.Row="1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                            <Label
                Grid.Column="1"
                Text="{Binding AdLogEntries.Price, StringFormat='{0:C0}'}"
                />
                        </Grid>
                        <!-- ABOUT -->
                        <Label
              Grid.Row="2"
              Text="Popis"
              />
                        <!-- DESCRIPTION -->
                        <Label
              Grid.Row="3"
              Text="{Binding AdLogEntries.Description}"
              />
                        <Grid
              Grid.Row="4"
              Margin="0, 12">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="80"/>
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                      <!-- BUY NOW BUTTON -->
                            <yummy:PancakeView
                Grid.Column="1"
                HeightRequest="48"
                CornerRadius="24, 0, 24, 0"
                Background Color="Accent"
                Margin="24, 0, 0, 0">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="30" />
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Label
                    Grid.Column="1"
                    Text="Buy now"
                    />
                                    <Label
                    Grid.Column="2"
                    Text=">"
                    />
                                </Grid>
                            </yummy:Pancake View>
                        </Grid>
                    </Grid>
                </yummy:Pancake View>
            </Grid>
        </controls:Parallax Control>
    </Grid>
</ContentPage.Content>

Ответы [ 3 ]

0 голосов
/ 23 января 2020

Проблема в вашей подробной странице XAML. Если быть точным, проблема в ваших выражениях привязки XAML. Все привязки на странице сведений имеют неверный путь. Например, для управления изображением это должно быть Source="{Binding AdDetail.Image}" вместо Source="{Binding AdLogEntries.Image}". Это происходит постоянно, и вы обнаруживаете подобные ошибки в выходном журнале при отладке приложения.

0 голосов
/ 24 января 2020

Ваш SelectedItem никогда не изменяется, потому что вы не запускаете OnPropertyChange. Вы можете изменить свой вид модели logi c следующим образом:

   private AdLogEntry selectedAd;
   public AdLogEntry SelectedAd { get => selectedAd; set { selectedAd = value; OnPropertyChanged("SelectedAd"); } }
   public ICommand AdSelectionChangedCommand => new Command(AdSelectionChanged);

    private void AdSelectionChanged()
    {
        OnPropertyChanged("SelectedAd");
        Application.Current.MainPage.Navigation.PushModalAsync(new DetailPage(SelectedAd));
    }

И в своем XAML:

<CollectionView x:Name="ads"
                        ItemsSource="{Binding AdLogEntries}"
                        SelectionMode="Single"
                        SelectionChangedCommand="{Binding AdSelectionChangedCommand}"
                        SelectedItem="{Binding SelectedAd, Mode=TwoWay}"
                        Margin="12,0">

Ваша страница сведений:

 public DetailPage(AdLogEntry detail)
 {
        this.BindingContext = new AdDetailViewModel(adDetail);
        InitializeComponent();
 }

In Деталь XAML:

   <Label Text="{Binding AdDetail.Name}"/>
0 голосов
/ 23 января 2020

ваш BindingContext равен AdDetailViewModel

BindingContext = new AdDetailViewModel(adDetail);

, а ваши пути привязки имеют вид

<Label Grid.Row="3" Text="{Binding AdLogEntries.Description}"

AdDetailViewModel и не имеют свойства AdLogEntries. У него есть AdDetail, поэтому, вероятно, вы должны использовать его как путь привязки

<Label Grid.Row="3" Text="{Binding AdDetail.Description}"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...