0

我正在用 MVVM 编写 xamarin 天气预报应用程序。

我填写ObservableCollection<DailyWeather>ObservableCollection<HourlyWeather>

在视图模型中

private ObservableCollection<DailyWeather> dailyWeather;

        public ObservableCollection<DailyWeather> DailyWeather
        {
            get => dailyWeather;
            set
            {
                dailyWeather = value;
                OnPropertyChange();
            }
        }

楷模

public class DailyWeather
    {
        public int DayOfYear { get; set; }
        public ObservableCollection<HourlyWeather> HourlyWeather { get; set; }

    }
public class HourlyWeather
    {
        public string Temperature { get; set; }
        public string Time { get; set; }
    }

Xaml 代码

<ListView ItemsSource="{Binding DailyWeather, Source={StaticResource vm}}"
          RowHeight="200">

                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout>

                                <Label Text="{Binding DayOfYear}"/>

                                <ListView ItemsSource="{Binding HourlyWeather}">
                                    <ListView.ItemTemplate>
                                        <DataTemplate>
                                            
                                             <ViewCell>
                                                <StackLayout>
                                                    <Label Text="{Binding Temperature}"/>
                                                    <Label Text="{Binding Time}"/>
                                                </StackLayout>
                                            </ViewCell>

                                        </DataTemplate>
                                    </ListView.ItemTemplate>
                                </ListView>

                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
               </ListView.ItemTemplate>
</ListView>

父 ListView 输出集合和“DayOfYear”。

Child ListView 输出集合并看到对象属性“温度”和“时间”但不输出它们,为什么?

所有收藏都已填满。

为什么如果我从 ListViews 中删除 ViewCell 那么应用程序会出现“指定的演员表无效”异常?

4

3 回答 3

0

如果你使用 mvvm ,你应该使用定位器。这是工作示例。更改了一些标签,因为现在 pc 上没有 WPF。

你的VM.cs

class YourVM : INotifyPropertyChanged
    {
        private ObservableCollection<DailyWeather> dailyWeather;

        public ObservableCollection<DailyWeather> DailyWeather
        {
            get => dailyWeather;
            set
            {
                dailyWeather = value;
                OnPropertyChange(nameof(DailyWeather));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChange(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public YourVM()
        {
            DailyWeather = new ObservableCollection<DailyWeather>()
            {
                new DailyWeather()
                {
                     DayOfYear=2011,
                      HourlyWeather=new ObservableCollection<HourlyWeather>()
                      {
                          new HourlyWeather()
                          {
                               Temperature="1",
                                Time="2011-01-02"
                          },
                          new HourlyWeather()
                          {
                                Temperature="2",
                                Time="2011-01-03"
                          },
                           new HourlyWeather()
                          {
                               Temperature="3",
                                Time="2011-01-04"
                          },
                          new HourlyWeather()
                          {
                                Temperature="4",
                                Time="2011-01-05"
                          }
                      }
                }
            };
        }
    }

MVVMLocator.cs

class MVVMLocator
    {
        public MVVMLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
            SimpleIoc.Default.Register<YourVM>();
        }

        public static YourVM YourVM => SimpleIoc.Default.GetInstance<YourVM>();
    }

应用程序.xaml

<Application
    x:Class="App2.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App2">
    <Application.Resources>
        <ResourceDictionary>
            <local:MVVMLocator x:Key="Locator"/>
        </ResourceDictionary>
    </Application.Resources>
</Application>

主页.xaml

<Page
    x:Class="App2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <ListView ItemsSource="{Binding YourVM.DailyWeather , Source={StaticResource Locator}}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Height="200">

                        <TextBlock Text="{Binding DayOfYear}"/>

                        <ListView ItemsSource="{Binding HourlyWeather}">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <TextBlock Text="{Binding Temperature}"/>
                                        <TextBlock Text="{Binding Time}"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>

                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Page>

Nuget:CommonServiceLocator,MvvmLightLibs

于 2020-09-22T15:19:49.153 回答
0

删除Source={StaticResource vm}ListView 的 in ItemSource。并在页面后面的代码中设置下面的代码。

   this.BindingContext = this;

整个代码:

  public partial class Page1 : ContentPage
{
    private ObservableCollection<DailyWeather> dailyWeather;

    public ObservableCollection<DailyWeather> DailyWeather
    {
        get => dailyWeather;
        set
        {
            dailyWeather = value;

        }
    }
    public Page1()
    {
        InitializeComponent();
        DailyWeather = new ObservableCollection<DailyWeather>()
        {
            new DailyWeather()
            {
                 DayOfYear=2011,
                  HourlyWeather=new ObservableCollection<HourlyWeather>()
                  {
                      new HourlyWeather()
                      {
                           Temperature="1",
                            Time="2011-01-02"
                      },
                      new HourlyWeather()
                      {
                            Temperature="2",
                            Time="2011-01-03"
                      },
                       new HourlyWeather()
                      {
                           Temperature="3",
                            Time="2011-01-04"
                      },
                      new HourlyWeather()
                      {
                            Temperature="4",
                            Time="2011-01-05"
                      }
                  }
            }
        };
        this.BindingContext = this;
    }
}
public class DailyWeather
{
    public int DayOfYear { get; set; }
    public ObservableCollection<HourlyWeather> HourlyWeather { get; set; }

}
public class HourlyWeather
{
    public string Temperature { get; set; }
    public string Time { get; set; }
}

截屏:

在此处输入代码

于 2020-09-22T08:18:15.253 回答
0

您需要从绑定中删除此Source={StaticResource vm} 。

将此添加到您的 XAML:

 <ContentPage.BindingContext>
    <vm:VIEWMODEL></vm:VIEWMODEL>
</ContentPage.BindingContext>

并将父列表视图更改为:

<ListView ItemsSource="{Binding DailyWeather}" RowHeight="200">

在此处输入图像描述

于 2020-09-22T05:33:36.247 回答