如何使用 Xamarin 从 XAML 访问局部变量



我应该知道这一点,但找不到正确的信息。我正在尝试让我的Xamarin.Forms ListView工作,我需要将ItemsSource设置为名为"theHerd"的局部变量,该变量的类型为ObsverableCollection。这是我所拥有的,我需要如何更改它才能使其工作?

<?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:HerdViewerPrototype" x:Class="HerdViewerPrototype.HerdViewerPrototypePage">
<ListView x:Name="lstview" ItemsSource="{local:theHerd}" />
</ContentPage>

你应该使用 MVVM 模式

并像这样从 XAML 调用此 ObservableColletion:

在 XAML 代码中:

<ListView ItemsSource="{Binding Items}"
          CachingStrategy="RecycleElement"
          RowHeight="60">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Margin="8">
                    <Label Text="{Binding Make}"
                           FontAttributes="Bold" />
                    <Label Text="{Binding YearOfModel}" />
                </StackLayout>
            </ViewCell>
        </DataTemplate>
</ListView.ItemTemplate>

你应该创建一个像这样的 ViewModel 类:

public class CarsViewModel : INotifyPropertyChanged {
    private ObservableCollection<Car> items;
    public ObservableCollection<Car> Items {
        get { return items; }
        set {
            items = value;
        }
    }

    public CarsViewModel() {
        // Here you can have your data form db or something else,
        // some data that you already have to put in the list
        Items = new ObservableCollection<Car>() {
            new Car()
            {
                CarID = 1,
                Make = "Tesla Model S",
                YearOfModel = 2015
            },
              new Car()
            {
                CarID = 2,
                Make = "Audi R8",
                YearOfModel = 2012
            },
        };
    }
}

最后,在MainPage.xaml中.cs像这样:

BindingContext = new CarsViewModel();

以下是在 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"
             xmlns:local="clr-namespace:ButtonRendererDemo;assembly=ButtonRendererDemo"
             x:Class="ButtonRendererDemo.ImageTapComplexPage"
             BindingContext="{StaticResource viewModel}">
      <ContentPage.Resources>
        <ResourceDictionary>
          <local:YourViewModelClassType x:Key="viewModel"/>
        </ResourceDictionary>
      </ContentPage.Resources>

然后你可以使用任何一个

 <ListView x:Name="lstItems" RowHeight="60" ItemsSource="{Binding Items}" >

从页面绑定的顶部获取模型或

<TapGestureRecognizer Command="{Binding Source={StaticResource viewModel}, Path=TapCommand}" CommandParameter="{Binding name}" />

从静态资源中获取模型

我认为你在这里混合了几件事。

正确的方法是正如威尔逊所说。使用 MVVM 模式并将 Page 的绑定上下文设置为为此目的创建的 ViewModel。

无论如何,即使你想从 xaml 做所有的事情,你总是必须将页面的 BindingContext 设置为某些东西。所以在你后面的代码中会有

this.BindingContext = this;

然后在 xaml 中:

<ListView x:Name="lstview" ItemsSource="{Binding theHerd}" />

正如我所说,这很奇怪,不是推荐的方法。

最新更新