簡體   English   中英

通過單擊xamarin按鈕來調用綁定類的方法。

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

因此,我有一個視圖,該視圖已綁定到稱為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
    }
}

干杯。

在View XAML代碼上,應將其添加到“開始”按鈕中:

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

然后在“我的綁定類:”上,創建de ICommand屬性,並在構造函數上對其進行初始化,然后創建Command,如下所示:

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

希望這對您有幫助,干杯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM