it all depends the value which I set to it.
If you want to add a binding property to control the timer, you can achieve it like following code in custom view.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace App64
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class View1 : ContentView
{
// public static MyTimer myTimer;
public static DeviceTimer deviceTimer;
public int TimeWait = 1;
public View1()
{
InitializeComponent();
//myTimer = new MyTimer(new TimeSpan(1), () => {
// TimeWait++;
// mylabel.Text = TimeWait.ToString();
//});
//myTimer.Start();
deviceTimer = new DeviceTimer( () => {
TimeWait++;
mylabel.Text = TimeWait.ToString();
}, new TimeSpan(1),true,true);
}
public static readonly BindableProperty EventNameProperty = BindableProperty.Create("EventName", typeof(string), typeof(View1), propertyChanged: OnEventNameChanged);
private static void OnEventNameChanged(BindableObject bindable, object oldValue, object newValue)
{
if(newValue.Equals("stop"))
{
deviceTimer.Stop();
}
else if (newValue.Equals("Restart"))
{
deviceTimer.Restart();
}
}
public string EventName
{
get { return (string)GetValue(EventNameProperty); }
set { SetValue(EventNameProperty, value); }
}
}
}
Here is customview's forground code.
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App64.View1">
<ContentView.Content>
<StackLayout>
<Label x:Name="mylabel" Text="Hello Xamarin.Forms!" />
</StackLayout>
</ContentView.Content>
</ContentView
When we use it in the contentpage
we can use Button
to make a test.
<app64:View1 EventName="{Binding MyEvent}" >
<app64:View1.Effects>
<app64:ViewLifecycleEffect Loaded="ViewLifecycleEffect_Loaded" Unloaded="ViewLifecycleEffect_Unloaded"/>
</app64:View1.Effects>
</app64:View1>
<Button Text="navi" Clicked="Button_Clicked"></Button>
<Button Text="stop" Clicked="Button_Clicked_1"/>
<Button Text="restart" Clicked="Button_Clicked_2"/>
background code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace App64
{
public partial class MainPage : ContentPage
{
string myEvent;
public string MyEvent
{
set
{
if (myEvent != value)
{
myEvent = value;
OnPropertyChanged("MyEvent");
}
}
get
{
return myEvent;
}
}
public MainPage()
{
InitializeComponent();
BindingContext = this;
}
protected override void OnAppearing()
{
base.OnAppearing();
View1.deviceTimer.Restart();
}
protected override void OnDisappearing()
{
base.OnDisappearing();
View1.deviceTimer.Stop();
// View1.myTimer.Stop();
}
private void Button_Clicked_1(object sender, EventArgs e)
{
MyEvent = "stop";
}
private void Button_Clicked_2(object sender, EventArgs e)
{
MyEvent = "Restart";
}
private void ViewLifecycleEffect_Loaded(object sender, EventArgs e)
{
DisplayAlert("INFO","SHOWING","OK");
}
private void ViewLifecycleEffect_Unloaded(object sender, EventArgs e)
{
DisplayAlert("INFO", "disappear", "OK");
}
private void Button_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new Page1());
}
}
}