将ApplicationIdleDetectionMode设为Disabled可以让程序运行在锁定屏幕下,将UserIdleDetectionMode设为Disabled可以让程序一直运行而不会休眠!

posted @ 2011-01-07 21:44 上尉 阅读(281) 评论(0) 编辑
摘要: 经过前面的准备,现在可以动手实现SinaService接口和界面布局啦!SinaService的实现不难,用linq解析下xml就好了。AppKey,用户名,密码就要自己去申请啦。代码相应的ParseService如下:代码主页面用到了Pivot Control代码每条微博的DataTemplate如下代码记得在MainPageViewModel的构造函数中调用RefreshHomeList代码最后把viewmodel和view联系起来,在App.xaml中声明代码在MainPage.xaml中声明就大功告成了![代码]因为间隔时间比较长,上面只是提到了些关键实现,有些地方可能忘记了。如有问题阅读全文
posted @ 2010-12-10 17:48 上尉 阅读(526) 评论(0) 编辑

可以调用这个方法,但它在模拟器中总是返回true,所以要自己判断下是否在模拟器中:

 

bool hasNetworkConnection = NetworkInterface.GetIsNetworkAvailable();

 

更详细的信息可以通过如下代码,不过它会block ui线程,所以最好另开个thread调用!

 

var interfaceType = NetworkInterface.NetworkInterfaceType;

 

 

posted @ 2010-12-06 18:59 上尉 阅读(194) 评论(0) 编辑

这几天在研究如何进行异步测试, silverlight unit test framework提供了相应支持,异步在这个framework中的含义就是把一些额外的任务排队并稍后执行。 比如有一个request()方法是异步的,一般情况下呼叫这个方法之后无法直接测试返回的结果;但在测试方法TestRequest()中,呼叫request()后可以加入一些额外任务到一个队列中,在退出TestRequest()后执行这些任务。说起来有些拗口看下图会明白些:

左边是正常的测试方法,右边是异步测试

一般运用下面四个方法添加任务到队列中:

EnqueueTestComplete() – 添加一个TestComplete()到队列中,这个方法告知framework测试结束了。
EnqueueCallback() – 添加一个任务到队列
EnqueueConditional() – 添加一个条件判断到队列,如果为true才继续执行
EnqueueDelay() – 添加一些等待时间到队列

下图演示了如何实际使用:

下面回到项目中,看看具体如何做:

1.添加一个MockSinaService:

代码
public class MockSinaService : ISinaService
{
public List<Status> StatusList{get;set;}

public void GetStatuses(System.Action<IEnumerable<Status>> onGetStatusesCompleted = null, System.Action<System.Exception> onError = null, System.Action onFinally = null)
{
DispatcherTimer timer
= new DispatcherTimer();
timer.Interval
= TimeSpan.FromSeconds(2);
timer.Tick
+= delegate(object sender, EventArgs e)
{
Status testStatus
=
new Status
{
Text
= "this is test status",
CreatedAt
= DateTime.Now
};

onGetStatusesCompleted(
new List<Status>() { testStatus });
timer.Stop();
};
timer.Start();
}
}
这个类是为了模拟SinaService,减少MainPageViewModel对它的依赖。因为实际中SinaService会调用webclient异步取得数据,但MainPageViewModel其实并不关心数据是怎么来的,添加这个mock会方便单元测试。注意在实现中使用定时器来模拟webclient的异步行为。

2. 添加一个公共属性SinaService到MainPageViewModel中,这样测试项目可以把它指向mock类

代码
public ISinaService SinaService
{
get
{
if (_sinaService == null)
{
_sinaService
= new SinaService();
}
return _sinaService;
}
set
{
if (value != _sinaService)
{
_sinaService
= value;
}
}
}
3. 添加RefreshHomeList()方法到MainPageViewModel中

代码
public void RefreshHomeList()
{
Trace.DetailMsg(
"RefreshHomeList");
IsHomeRefreshing
= true;
SinaService.GetStatuses(
delegate(IEnumerable<Status> statuses)
{
IsHomeRefreshing
= false;

foreach (Status status in statuses)
{
HomeList.Add(status);
}
},
delegate(Exception exception)
{
IsHomeRefreshing
= false;
});
}
4. 修改TestInitialize如下,植入mock类

代码
[TestInitialize]
public void Initialize()
{
_mainViewModel
= new MainPageViewModel();
_mockSinaService
= new MockSinaService();
_mainViewModel.SinaService
= _mockSinaService;
}
5. 添加测试方法:

代码
[TestMethod]
[Asynchronous]
public void Refresh_HomeList_Success()
{
bool isHomeListRefreshed = false;
_mainViewModel.HomeList.CollectionChanged
+=
(s, e)
=>
{
isHomeListRefreshed
= true;
Assert.AreEqual(NotifyCollectionChangedAction.Add, e.Action);
Assert.AreEqual(
1, e.NewItems.Count, "only should be +1 item");
};
_mainViewModel.RefreshHomeList();
EnqueueConditional(()
=> isHomeListRefreshed);
EnqueueCallback(()
=> Assert.AreEqual(_mainViewModel.HomeList.Count, 1, "Expected non-empty products list."));
EnqueueTestComplete();

 

注意这里的Asynchronous关键字,而且要让MainPageViewModelTests这个类继承SilverlightTest。

最后测试结果如下:

 

References:

1. silverlight2-unit-testing by jeff wilcox

2. Asynchronous test support – Silverlight unit test framework and the UI thread by jeff wilcox

3. Silverlight Unit Testing, RhinoMocks, Unity and Resharper by Justin Angel

 

posted @ 2010-11-25 20:35 上尉 阅读(1567) 评论(3) 编辑

想了一个礼拜,究竟是先实现些具体功能呢,还是继续完善TDD,还是先后者吧:具体功能无非就是些api的调用,wp7的twiter或者围脖的例子也有一些了,也不少我这一篇:)

1. 准备几个基本类,status,user和SinaService接口

代码
public class User
{
public ulong Id { get; set; }
public string ScreenName { get; set; }
public string ImageUrl { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string Description { get; set; }
public string Location { get; set; }
public int Followers { get; set; }
public int Friends { get; set; }
public DateTime CreatedAt { get; set; }
}

public class Status
{
public Status()
{
User
= new User();
}

public DateTime CreatedAt { get; set; }

public ulong Id { get; set; }

public string Text { get; set; }

public string Source { get; set; }

public User User { get; set; }

public bool Favorited { get; set; }

public int RetweetCount { get; set; }

public bool Retweeted { get; set; }

}

/// <summary>
/// A simple Sina service interface
/// </summary>
public interface ISinaService
{
/// <summary>
/// Get statuses from sina weibo
/// </summary>
/// <param name="onGetStatusesCompleted">the callback method</param>
/// <param name="onError"></param>
/// <param name="onFinally"></param>
void GetStatuses(Action<IEnumerable<Status>> onGetStatusesCompleted = null, Action<Exception> onError = null, Action onFinally = null);
}

status和user类的相应测试俺就偷懒不写了,读者有兴趣可以自己试试!

 

2. 修改下MainPageViewModel, 加入SinaService接口和HomeList,前者用来调用sina的接口取得数据,后者是最新微博的容器。注意把它们设置为internal,方便测试项目调用,还要添加如下一行在AssemblyInfo.cs中:

[assembly: InternalsVisibleTo("WeiBo7.Test")]

 

代码
public class MainPageViewModel : ViewModelBase
{
internal readonly ISinaService SinaService;
private bool _isHomeRefreshing = false;

public MainPageViewModel()
{
SinaService
= new SinaService();
HomeList
= new ObservableCollection<Status>();

}

public ObservableCollection<Status> HomeList { get; internal set; }

/// <summary>
/// Indicates whether there is an indeterminate operation in progress
/// </summary>
public bool IsHomeRefreshing
{
get { return _isHomeRefreshing; }
set
{
if (value != _isHomeRefreshing)
{
_isHomeRefreshing
= value;
OnNotifyPropertyChanged(
"IsHomeRefreshing");
}
}
}

}

3. 现在转到测试项目,先加入一个TestInitialize方法,在所有的测试方法前做些准备工作:

 

private MainPageViewModel _mainViewModel;

[TestInitialize]
public void Initialize()
{
_mainViewModel
= new MainPageViewModel();
}

还可以加入一个[TestCleanup]方法:

[TestCleanup]
public void CleanUp()
{

}

目前没干什么事情,但对于任何一个测试方法,执行顺序是TestInitialize->test method->TestCleanup, 这样保证每个测试方法是彼此独立,互不影响,不用在每个测试方法里面都初始化,节省些代码。

4. 在MainPageViewModelTests先添加一个negative test, 看下这个类如何应对NullInstance

 

代码
[TestMethod]
[ExpectedException(
typeof(NullReferenceException))]
public void NullInstance()
{
_mainViewModel
= null;
bool x = _mainViewModel.IsHomeRefreshing;
}

5. 测试初始化是否成功

 

代码
[TestMethod]
public void Init_MainPageViewModel_Success()
{
Assert.IsNotNull(_mainViewModel);
Assert.IsNotNull(_mainViewModel.HomeList);
Assert.IsNotNull(_mainViewModel.SinaService);
Assert.IsInstanceOfType(_mainViewModel,
typeof(INotifyPropertyChanged));
}

 6. 测试当添加一个item到HomeList时,是否CollectionChanged被fire 

 

代码
[TestMethod]
public void AddItem_HomeList_RaisesCollectionChanged()
{
bool changeObserved = false;
_mainViewModel.HomeList.CollectionChanged
+=
(s, e)
=>
{
changeObserved
= true;
Assert.AreEqual(NotifyCollectionChangedAction.Add, e.Action);
Assert.AreEqual(
1, e.NewItems.Count, "only should be +1 item");
};

_mainViewModel.HomeList.Add(
new Status());
Assert.IsTrue(changeObserved);
}
最后运行如下图:


posted @ 2010-11-19 23:50 上尉 阅读(1243) 评论(0) 编辑
摘要: 分享下我关注的开发资源, msdn上的例程,MIX10和PDC10的视频就不必说了,下面两个网页也强力推荐!http://wp7dev.wikispaces.com/http://www.silverlightcream.com/wp7dev.aspx阅读全文
posted @ 2010-11-16 16:31 上尉 阅读(138) 评论(0) 编辑
摘要: 既然要用MVVM和TDD,两样利器必不可少:1. MVVM Light Toolkit2. Silverlight Unit Testing Framework 都有相应的WP7版本,添加dll到项目中即可。下面大概说下要点:1. 创建一个新项目WeiBo72. 添加一个测试项目WeiBo7.Test1)添加 Microsoft.Silverlight.Testing & Microsof...阅读全文
posted @ 2010-11-11 20:44 上尉 阅读(1946) 评论(2) 编辑
摘要: 之前写了一个Twit47, 但是既然官方的应用出来,继续下去没啥意义了。就移植到新浪微博上来吧。打算严格执行mvvm,tdd,会陆续写些心得来和大家分享!先放出主屏幕!阅读全文
posted @ 2010-11-10 00:04 上尉 阅读(1176) 评论(8) 编辑
摘要: 如果是在模拟器上面,可以用这个 WP7 Screenshot Tool如果是在真机上面,微软提供了wmnet工具 ,但前提是必须装pb,zune,无语...呼吁下微软把这个工具,和之前的remote tools也集成到vs2010中就好了!阅读全文
posted @ 2010-11-09 23:29 上尉 阅读(640) 评论(3) 编辑
摘要: 刚看到2个方法,没具体试过,但应该可行:1. Runtime Intelligence for Windows Phone2. 使用Silverlight Analytics Framework阅读全文
posted @ 2010-11-03 18:33 上尉 阅读(85) 评论(0) 编辑
摘要: 刚刚才发现,如果在SrollViewer放置一个ListBox,对性能很有影响!实验了一下,ListBox中有150个item,每个item包含图片和文字,加载单独的ListBox比加载在SrollViewer中的,大概快了6~8倍。阅读全文
posted @ 2010-09-09 19:52 上尉 阅读(93) 评论(0) 编辑
摘要: Silverlight中有一个bug,似乎到wp7中还是如此:如果TextBlock在一个StackPanel中,而这个StackPanel的Orientation又被设置成了Horizontal, 那么TextBlock的TextWrapping="Wrap"将被忽略...用Grid代替 StackPanel就没问题了!阅读全文
posted @ 2010-09-08 17:35 上尉 阅读(92) 评论(0) 编辑
摘要: 一般在wp7中可以用back键回到前一个page,但如果有另一个button也需要回到前页:可以这样,从secondpage到mainpage:NavigationService.Navigate(newUri("/MainPage.xaml",UriKind.RelativeOrAbsolute)但这样的话如果你在mainpage按back键就会回到secondpage,因为mainpage被添...阅读全文
posted @ 2010-09-06 16:11 上尉 阅读(241) 评论(0) 编辑
摘要: 写了一个基本的twitter客户端,有兴趣大家可以试试看,欢迎多给意见!可在这里下载:http://cid-b7acc11dcba9a3a1.office.live.com/self.aspx/Share/twitter47.rar阅读全文
posted @ 2010-08-27 18:45 上尉 阅读(60) 评论(0) 编辑
摘要: ApplicationBar并不是一个FrameworkElement, 意味着你不能databinding,但可以在code中设置它的text,IsEnable等属性var appbar_refresh = (Microsoft.Phone.Shell.ApplicationBarIconButton)this.ApplicationBar.Buttons[0];appbar_refresh.I...阅读全文
posted @ 2010-08-26 20:18 上尉 阅读(106) 评论(0) 编辑
摘要: Microsoft.Devices.Environment.DeviceType// Summary: // Defines the device type values used by the Microsoft.Devices.Environment.DeviceType // property. public enum DeviceType { // Summary: // The devi...阅读全文
posted @ 2010-07-30 15:57 上尉 阅读(119) 评论(1) 编辑
摘要: 不废话,上代码:)代码阅读全文
posted @ 2010-07-26 19:14 上尉 阅读(69) 评论(1) 编辑
摘要: 在新的trainning kit 中有一个例子解释的很清楚了:1. Application_Launching:只有在新启动程序时触发2. Application_Closing:只有在推出程序时触发--只有在程序mainpage时按硬后退键3.Application_Activated:从home键或者其它方式离开,back键返回时触发4. Application_Deactivated:从ho...阅读全文
posted @ 2010-07-26 19:03 上尉 阅读(187) 评论(0) 编辑
摘要: The user agent for IE on Windows Phone 7 running on the Asus Galaxy device is:Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0) Asus;Galaxy6source阅读全文
posted @ 2010-04-29 00:19 上尉 阅读(69) 评论(0) 编辑
摘要: Step 1: prepare the image with size 480x800, name it splashscreenimage.jpgStep 2: add this image to the project and make it as 'content'Done!阅读全文
posted @ 2010-04-28 20:40 上尉 阅读(88) 评论(1) 编辑
摘要: Silverlight 数据绑定 (1):怎样实现数据绑定Silverlight 数据绑定 (2):Source to Targettbc...阅读全文
posted @ 2010-03-25 07:06 上尉 阅读(106) 评论(0) 编辑
摘要: Input Scopes for the Soft Input Panel代码还有其他选择可以定制SIP,真是很方便!如果能针对不同国家就更牛了,例如不同国家的邮编就需要不同的SIP布局。阅读全文
posted @ 2010-03-23 05:28 上尉 阅读(105) 评论(0) 编辑
摘要: 正在学习中,大家多交流!!http://channel9.msdn.com/learn/courses/WP7TrainingKit/http://www.silverlight.net/getstarted/devices/windows-phone/http://www.earthware.co.uk/blog/index.php/category/windows-phone-7-series...阅读全文
posted @ 2010-03-22 19:35 上尉 阅读(58) 评论(0) 编辑
摘要: By both running tcpdump on G1 and collecting data on the GPRS gateway, it shows that the Android OS itself only generates a little data traffic. It really depends on what apps and services running on ...阅读全文
posted @ 2010-03-12 21:35 上尉 阅读(80) 评论(0) 编辑
摘要: http://www.netmite.com/android/mydroid/development/pdk/docs/telephony.htmlhttp://ljh.ee.nchu.edu.tw/~cch/program/CE/RILhttp://pdk.android.com/online-pdk/guide/telephony.html阅读全文
posted @ 2010-02-02 16:55 上尉 阅读(59) 评论(0) 编辑
摘要: Creating CHome (Titanium) Plugins Part I Creating CHome (Titanium) Plugins Part II Creating Customized Titanium Layout CHome Registry (Titanium) and CPR file walk-through Titanium: CHome is the pl...阅读全文
posted @ 2010-01-15 21:23 上尉 阅读(620) 评论(0) 编辑
摘要: Reg settins according to this post: [HKEY_CURRENT_USER\Security\Software\Microsoft\Marketplace] "DownloadLocation"="\\Windows\\WMMarketplace.cab" "IsInstalled"=dword:000...阅读全文
posted @ 2010-01-13 20:19 上尉 阅读(193) 评论(0) 编辑
摘要: <EditText android:id="@+id/edtInput" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:inputType=...阅读全文
posted @ 2009-12-16 18:11 上尉 阅读(94) 评论(0) 编辑
摘要: 敏捷之道的最高境界:   个体与交互 胜过 过程与工具   可以工作的软件 胜过 面面俱到的文档   客户协作 胜过 合同谈判   响应变化 胜过 遵循计划阅读全文
posted @ 2009-11-27 00:17 上尉 阅读(10) 评论(0) 编辑
摘要: http://www.pavingways.com/mobile-widget-wiki/mobile-widget-engine-overview阅读全文
posted @ 2009-11-24 17:22 上尉 阅读(41) 评论(0) 编辑
摘要: It is simple! AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); //通话音量 int max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_VOICE_CALL ); int current ...阅读全文
posted @ 2009-11-24 16:37 上尉 阅读(242) 评论(0) 编辑
摘要: Just some useful links: Krishnaraj Varma Blog http://www.devdiv.net/redirect.php?tid=10384&goto=lastpost阅读全文
posted @ 2009-11-20 17:45 上尉 阅读(368) 评论(0) 编辑
posted @ 2008-12-03 22:37 上尉 阅读(62) 评论(0) 编辑
posted @ 2008-08-18 19:16 上尉 阅读(14) 评论(0) 编辑
posted @ 2008-07-25 19:32 上尉 阅读(39) 评论(0) 编辑
posted @ 2008-07-24 17:26 上尉 阅读(25) 评论(0) 编辑
posted @ 2008-06-17 15:41 上尉 阅读(4) 评论(0) 编辑
posted @ 2008-06-13 19:28 上尉 阅读(3) 评论(0) 编辑
posted @ 2008-06-10 17:27 上尉 阅读(157) 评论(0) 编辑
posted @ 2008-06-03 20:30 上尉 阅读(41) 评论(0) 编辑
View Hao Wang's profile on LinkedIn