通过单击按钮调用绑定 class 的方法 xamarin.

call a method of the binding class from a button click xamarin.

所以我有一个视图,我已经绑定到一个名为计时器的计时器对象列表(我制作的自定义 class),并且在该视图中我添加了一个开始和删除按钮。当用户单击开始时,我希望他们能够调用与该按钮关联的相关计时器对象方法 startTimer()。我该怎么做?

查看代码:

    <ContentPage.Content>
<StackLayout Orientation="Vertical">
<ListView ItemsSource="{Binding Timers, Mode=TwoWay}" SeparatorVisibility="None">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
                    <StackLayout Padding="10,0,0,0" VerticalOptions="StartAndExpand" Orientation="Vertical">
                        <Label Text="{Binding _name, Mode=TwoWay}" YAlign="Center"/>
                        <Label Text="{Binding _startTime, Mode=TwoWay}" YAlign="Center" FontSize="Small"/>
                    </StackLayout>
                    <Button Text="Start" //button to associate with method//></Button>
                    <Button Text="Remove"></Button>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
<Button Text="Add New" Clicked="AddNewTimer"/>
</StackLayout>
</ContentPage.Content>

我绑定的Class:

public class MainViewModel : INotifyPropertyChanged
{
    public MainViewModel ()
    {
        Timers = DependencyService.Get<ISaveAndLoad> ().LoadTimers (); 
        if (Timers == null) {
            Timers = new ObservableCollection<Timer> (); 
        }
    }

    //When property changes notifys everything using it. 
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private ObservableCollection<Timer> _timers;
    public ObservableCollection<Timer> Timers { 
        get { return _timers; } 
        set { 
            _timers = value;
            NotifyPropertyChanged ("Timers");
        } 
    }

    private string _title;
    public string Title{ 
        get{ 
            return _title; 
        }
        set{ 
            _title = value; 
            NotifyPropertyChanged ();
        }
    }
}

和定时器class:

    public class Timer
{ 
    public int _startTime { get; set;} 
    public bool _hasStarted{ get; set; } 
    public string _name { get; set; }  

    public Timer (string name, int startTime, bool hasStarted = false)
    {
        _name = name; 
        _startTime = startTime; 
        _hasStarted = hasStarted; 
    }

    public void startTimer(){
        //do something here
    }
}

干杯。

在您的视图 XAML 代码中,您应该将此添加到您的开始按钮:

<Button Text="Start" Command={Binding btnStartCommand} />

然后在您的 "My Binded Class: " 上,您应该创建 de ICommand 属性 并在构造函数上初始化它,然后创建命令,如下所示:

public ICommand btnStartCommand {get; set;}
public MainViewModel()
{
    btnStartCommand = new Command(StartCommand);
}
public void StartCommand()
{
    //here you create your call to the startTimer() method
}

希望对你有帮助, 干杯。