C# 通过单击按钮调用绑定类的方法。
因此,我有一个视图,它绑定到一个名为Timers(我创建的自定义类)的计时器对象列表,并在该视图中添加了一个start和remove按钮。当用户单击开始时,我希望他们能够调用与按钮关联的相关计时器对象方法startTimer()。我该怎么做 查看代码:C# 通过单击按钮调用绑定类的方法。,c#,data-binding,binding,xamarin,C#,Data Binding,Binding,Xamarin,因此,我有一个视图,它绑定到一个名为Timers(我创建的自定义类)的计时器对象列表,并在该视图中添加了一个start和remove按钮。当用户单击开始时,我希望他们能够调用与按钮关联的相关计时器对象方法startTimer()。我该怎么做 查看代码: <ContentPage.Content> <StackLayout Orientation="Vertical"> <ListView ItemsSource="{Binding Timers, Mode=
<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>
干杯 在查看XAML代码时,应将其添加到开始按钮:
<Button Text="Start" Command={Binding btnStartCommand} />
希望这对你有帮助,
干杯
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
}
}
<Button Text="Start" Command={Binding btnStartCommand} />
public ICommand btnStartCommand {get; set;}
public MainViewModel()
{
btnStartCommand = new Command(StartCommand);
}
public void StartCommand()
{
//here you create your call to the startTimer() method
}