【问题标题】:How to access local variable from XAML using Xamarin如何使用 Xamarin 从 XAML 访问局部变量
【发布时间】:2017-10-24 02:37:57
【问题描述】:

我应该知道这一点,但找不到正确的信息。我试图让我的 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>

【问题讨论】:

  • 您没有发布任何代码。但通常您可以使用 DataBinding 在 XAML 中设置您的 ItemSource,或者只是在您的代码隐藏中分配它。

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

你应该使用MVVM pattern

然后像这样从 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();

【讨论】:

  • 我可以把 lstView.ItemsSource=theHerd;在文件后面的代码中,一切正常,但是我试图从 XAML 文件中做所有事情,这应该是可能的。
  • 我想从 XAML 文件中执行 BindingContext,而不是从文件后面的代码中。
【解决方案2】:

这是在 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}" />

从静态资源中获取模型

【讨论】:

    【解决方案3】:

    我认为你在这里混合了一些东西。

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

    不管怎样,即使你想从 xaml 做所有的事情,你也总是必须将 Page 的 BindingContext 设置为一些东西。所以在你后面的代码中会有

    this.BindingContext = this;
    

    然后在 xaml 中:

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

    正如我所说,这很奇怪,不推荐这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多