【问题标题】:call a method of the binding class from a button click xamarin.从按钮单击 xamarin 调用绑定类的方法。
【发布时间】:2015-10-31 23:18:48
【问题描述】:

所以我有一个视图,我绑定到一个名为 Timers 的 Timer 对象列表(我创建的自定义类),并且在视图中我添加了一个开始和删除按钮。当用户单击开始时,我希望他们能够调用与按钮关联的相关计时器对象方法 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>

我的绑定类:

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 ();
        }
    }
}

还有定时器类:

    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
    }
}

干杯。

【问题讨论】:

    标签: c# data-binding binding xamarin


    【解决方案1】:

    在您的 View XAML 代码中,您应该将其添加到您的开始按钮:

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

    然后在“我的绑定类:”上创建 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
    }
    

    希望对你有帮助, 干杯。

    【讨论】:

      猜你喜欢
      • 2011-04-01
      • 2013-10-13
      • 1970-01-01
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 2014-10-29
      相关资源
      最近更新 更多