I'm trying out Windows Phone 7 dev for the first time. I decided to try and port a Silverlight Timer example from the default examples in Expression Blend 4. The timer example for full-blown silverlight binds a TimerModel class to the timer, the start/stop toggle switch, etc. I've figured out how to create the datasource/datacontext and bind the properties to things on the screen. However, the Reset() method, which is a void doesn't show up in the bindable options for the Windows Phone 7 app. It's the exact same class in both, but for some reason the void method isn't bindable. Is there something I need to enable that was in the full Silverlight app that's not in Windows Phone 7? Is there something in particular that makes a class' properties or methods bindable when it's a data source? Is this just one of the limitations of Windows Phone 7's subset of Silverlight's features?
Below is the class, which is the same in both applications. I want to bind a button's click to the Reset() method.
namespace Time
{
using System;
using System.ComponentModel;
using System.Windows.Threading;
using System.Windows.Data;
public class TimerModel : INotifyPropertyChanged
{
private bool isRunning;
private DispatcherTimer timer;
private TimeSpan time;
private DateTime lastTick;
public string FormattedTime
{
get
{
return string.Format("{0:#0}:{1:00}:{2:00.00}", this.time.Hours, this.time.Minutes, (this.time.Seconds + (this.time.Milliseconds / 1000.0d)));
}
}
private void UpdateTimes()
{
this.NotifyPropertyChanged("FormattedTime");
this.NotifyPropertyChanged("Hours");
this.NotifyPropertyChanged("Minutes");
this.NotifyPropertyChanged开发者_如何学Python("Seconds");
}
public bool Increment
{
get;
set;
}
public int Hours
{
get
{
return this.time.Hours;
}
set
{
this.time = this.time.Add(TimeSpan.FromHours(value - this.time.Hours));
this.UpdateTimes();
}
}
public int Minutes
{
get
{
return this.time.Minutes;
}
set
{
this.time = this.time.Add(TimeSpan.FromMinutes(value - this.time.Minutes));
this.UpdateTimes();
}
}
public int Seconds
{
get
{
return this.time.Seconds;
}
set
{
this.time = this.time.Add(TimeSpan.FromSeconds(value - this.time.Seconds));
this.UpdateTimes();
}
}
public bool IsRunning
{
get { return this.isRunning; }
set
{
if (this.isRunning != value)
{
this.isRunning = value;
if (this.isRunning)
{
this.StartTimer();
}
else
{
this.StopTimer();
}
this.NotifyPropertyChanged("IsRunning");
}
}
}
private void StartTimer()
{
if (this.timer != null)
{
this.StopTimer();
}
this.timer = new DispatcherTimer();
this.timer.Interval = TimeSpan.FromMilliseconds(1);
this.timer.Tick += this.OnTimerTick;
this.lastTick = DateTime.Now;
this.timer.Start();
}
private void StopTimer()
{
if (this.timer != null)
{
this.timer.Stop();
this.timer = null;
}
}
private void OnTimerTick(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
TimeSpan diff = now - this.lastTick;
this.lastTick = now;
if (this.Increment)
{
this.time = this.time.Add(diff);
}
else
{
this.time = this.time.Subtract(diff);
}
if (this.time.TotalMilliseconds <= 0)
{
this.time = TimeSpan.FromMilliseconds(0);
this.IsRunning = false;
}
this.UpdateTimes();
}
public void Reset()
{
this.time = new TimeSpan();
this.UpdateTimes();
}
public TimerModel()
{
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
I use the Caliburn Micro MVVM framework. It allows binding by convention so if you call your button RefreshButton and you have a method in your view model called RefreshButton it will get bound to the Click event automatically. Very powerful and easy.
I found out that Silverlight is using the stock CallMethodAction
action, which is not available in Windows Phone 7. It seems that I have two options:
- Use the onClick event like traditional forms, and just use the codebehind to call the method programmatically.
- Create my own custom Action to recreate
CallMethodAction
. There is also an open source project which contains a port of the method from full-blown Silverlight, which you can find on Codeplex.
精彩评论