如何在Xamarin.Forms中为listview应用Interaction.Behaviors命令事件?

时间:2014-10-10 10:23:08

标签: ios iphone ipad xamarin xamarin.forms

我正在尝试使用带有 Xamarin.Forms Xamarin.Behaviour 包将ListView ItemSelected或ItemTapped事件转换为绑定命令的命令。

它与Button工作正常但是当我尝试使用ListView时,它会出错:“systemReflection.TargetInvocationExaptopn”。

以下是我的ViewModel& amp;的代码。 Xaml页面

MainPage.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:b="clr-namespace:Xamarin.Behaviors;assembly=Xamarin.Behaviors"
                       x:Class="Xamarin.Behaviors.Demo.Views.MainView"
                        x:Name="MainPage">
    <StackLayout>
        <Entry Placeholder="Enter Firstname" >
            <b:Interaction.Behaviors>
                <b:BehaviorCollection>
                    <b:TextChangedBehavior Text="{Binding FirstName, Mode=TwoWay}" />
                    <b:EventToCommand EventName="Unfocused" Command="{Binding UnfocusedCommand}" CommandParameter="FirstName" />
                </b:BehaviorCollection>
            </b:Interaction.Behaviors>
        </Entry>

        <Entry  Placeholder="Enter Lastname" >
            <b:Interaction.Behaviors>
                <b:BehaviorCollection>
                    <b:TextChangedBehavior Text="{Binding LastName, Mode=TwoWay}" />
                    <b:EventToCommand BindingContext="{b:RelativeContext MainPage}" 
                                            EventName="Unfocused" 

                                        Command="{Binding UnfocusedCommand}"
                                        CommandParameter="LastName" />
                </b:BehaviorCollection>
            </b:Interaction.Behaviors>
        </Entry>

        <Button Text="Ok" Command="{Binding TestCommand}" />
        <Label Text="{Binding Message}" />
        <Label Text="{Binding WelcomeMessage}" />

        <ListView ItemsSource="{Binding Items}"
                     IsGroupingEnabled="false"
                     RowHeight="60" >
                     <b:Interaction.Behaviors>
                                        <b:BehaviorCollection>
                                            <b:EventToCommand CommandNameContext="{b:RelativeContext MainPage}"
                                                                    EventName="ItemSelected"
                                                                    CommandName="NickSelectedCommand"
                                                                    CommandParameter="{Binding NickName}" />
                                        </b:BehaviorCollection>
                                    </b:Interaction.Behaviors>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal" Padding="10" >
                            <StackLayout Orientation="Vertical"  HorizontalOptions="FillAndExpand" Spacing="-50"  >
                                <Button Text="{Binding NickName}">
                            <b:Interaction.Behaviors>
                                        <b:BehaviorCollection>
                                            <b:EventToCommand CommandNameContext="{b:RelativeContext MainPage}"
                                                                    EventName="Clicked"
                                                                    CommandName="NickSelectedCommand"
                                                                    CommandParameter="{Binding NickName}" />
                                        </b:BehaviorCollection>
                                    </b:Interaction.Behaviors>
                                </Button>

                            </StackLayout>
                        </StackLayout>

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


    </StackLayout>
</ContentPage>

MainViewModel.cs

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Xamarin.Forms;

namespace Xamarin.Behaviors.Demo.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string firstName = "FirstName";
        private string lastName = "LastName";
        private Command testCommand;
        private Command<object> unfocusedCommand;
        private string message;
        private string welcomeMessage;
        private Command<string> nickSelectedCommand;
        public MainViewModel()
        {
            this.Items = new ObservableCollection<Item>() { new Item() { NickName = "corcav" }, new Item() { NickName = "foo99" }, new Item() { NickName = "bar76" } };
        }
        public string FirstName
        {
            get { return this.firstName; }
            set {
                if (value != this.firstName)
                {
                    this.firstName = value;
                    this.RaisePropertyChanged();
                    this.TestCommand.ChangeCanExecute();
                }
            }
        }
        public string LastName
        {
            get { return this.lastName; }
            set {
                if (value != this.lastName)
                {
                    this.lastName = value;
                    this.RaisePropertyChanged();
                    this.TestCommand.ChangeCanExecute();
                }
            }
        }
        public string Message
        {
            get { return this.message; }
            private set {
                if (value != this.message) {
                    this.message = value;
                    this.RaisePropertyChanged();
                }
            }
        }
        public string WelcomeMessage
        {
            get { return this.welcomeMessage; }
            set {
                if (value != this.welcomeMessage) {
                    this.welcomeMessage = value;
                    this.RaisePropertyChanged();
                }
            }
        }
        public Command TestCommand
        {
            get {
                return this.testCommand ?? (this.testCommand = new Command(
                     () =>
                     {
                         this.WelcomeMessage = string.Format("Hello {0} {1}", this.FirstName, this.LastName);
                     },
                     () =>
                     {
                         // CanExecute delegate
                         return !string.IsNullOrEmpty(this.FirstName) && !string.IsNullOrEmpty(this.LastName);
                     }));
            }
        }
        public Command<object> UnfocusedCommand
        {
            get {
                return this.unfocusedCommand ?? (this.unfocusedCommand = new Command<object>(
                     (param) =>
                     {
                         this.Message = string.Format("Unfocused raised with param {0}", param);
                     },
                     (param) =>
                     {
                         // CanExecute delegate
                         return true;
                     }));
            }
        }
        public Command<string> NickSelectedCommand
        {
            get {
                return this.nickSelectedCommand ?? (this.nickSelectedCommand = new Command<string>(
                     (param) =>
                     {
                         this.Message = string.Format("Item {0} selected", param);
                     },
                     (param) =>
                     {
                         // CanExecute delegate
                         return true;
                     }));
            }
        }

        public ObservableCollection<Item> Items { get; private set; }
        protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

0 个答案:

没有答案