繁体   English   中英

绑定到 ViewModel 在 Xamarin.Forms 中不起作用

[英]Binding to the ViewModel not working in Xamarin.Forms

我正在尝试制作一个显示国家列表的应用程序,当单击一个国家/地区时,它会显示该国家/地区的城市列表。

国家.cs:

public class Country {
  public string Name { get; set; }
  public ImageSource Image { get; set; }
  public IList<City> Cities { get; set; }
}

城市.cs:

public class City
{
    public string Name { get; set; }
}

通过以下方式在 CountryPageViewModel.cs 中执行导航:

async void OnSelectedCountry(Country country)
    {
        if (country == null)
        {
            return;
        }

        await Shell.Current.GoToAsync($"{nameof(CitiesPage)}?{nameof(CitiesViewModel.Name)}={country.Name}");
    }

城市被加载到 CitiesViewModel.cs 中:

[QueryProperty(nameof(Name), nameof(Name))]
public class CitiesViewModel : BaseViewModel
{
    private string countryName;

    public IList<City> CityList { get; set; }

    public string Name
    {
        get
        {
            return countryName;
        }

        set
        {
            countryName = value;
            LoadCities(value);
        }
    }

    public async void LoadCities(string countryName)
    {
        try
        {
            Country country = await DataStore.GetItemAsync(cityName);
            IList<City> cities = country.Cities;
            CityList = cities;
        }
        catch (Exception)
        {
            Console.WriteLine("Impossible to load cities");
        }
    }
}

在 XAML (CitiesPage.xaml) 中:

<CollectionView x:Name="citiesCollectionView"  x:DataType="viewmodels:CitiesViewModel"
            ItemsSource="{Binding CityList}"
            EmptyView="Impossible to load cities" >
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Grid Padding="5" x:DataType="models:City">
                <Frame Style="{StaticResource CityCard}">
                    <Grid RowDefinitions="Auto,Auto"
                          ColumnDefinitions="Auto" >
                        <Label Grid.Column="1" 
                            Text="{Binding Name}" 
                            FontAttributes="Bold"
                            VerticalTextAlignment="Center"
                            Padding="10"
                            HeightRequest="50" />
                    </Grid>
                </Frame>
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

但是“城市”页面中没有显示任何内容。 我已将 CitiesPage.xaml.cs(代码隐藏)中的 BindingContext 设置为 ViewModel。

我究竟做错了什么?

对于谁可能有同样的问题,正如 xamarin 文件所说

视图可访问的所有视图 model 和 model 类都应实现 INotifyPropertyChanged 接口。 Implementing this interface in a view model or model class allows the class to provide change notifications to any data-bound controls in the view when the underlying property value changes.

以下是有关PropertyChanged 的更多提示:

- 如果公共属性的值发生变化,则始终引发 PropertyChanged 事件。 不要因为知道 XAML 绑定是如何发生的而假设可以忽略引发 PropertyChanged 事件。

始终为视图 model 或 model 中的其他属性使用其值的任何计算属性引发 PropertyChanged 事件。

始终在进行属性更改的方法结束时引发 PropertyChanged 事件,或者当已知 object 位于安全 state 中时。 引发事件会通过同步调用事件的处理程序来中断操作。 如果这发生在操作中间,当 object 处于不安全、部分更新的 state 中时,它可能会将 object 暴露给回调函数。 此外,PropertyChanged 事件可能会触发级联更改。 级联更改通常需要在级联更改可以安全执行之前完成更新。

如果属性未更改,则永远不要引发 PropertyChanged 事件。 这意味着您必须在引发 PropertyChanged 事件之前比较旧值和新值。

如果您正在初始化属性,则永远不要在视图模型的构造函数期间引发 PropertyChanged 事件。 此时视图中的数据绑定控件将不会订阅接收更改通知。

在 class 的公共方法的单个同步调用中,绝不会引发多个具有相同属性名称参数的 PropertyChanged 事件。 例如,给定一个 NumberOfItems 属性,其后备存储是 _numberOfItems 字段,如果一个方法在循环执行期间将 _numberOfItems 递增 50 次,则在所有工作完成后,它应该只在 NumberOfItems 属性上引发一次属性更改通知。 对于异步方法,在异步延续链的每个同步段中为给定属性名称引发 PropertyChanged 事件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM