我很驚訝配置文件78庫中不存在方便的System.Threading.Timer類。要使用這個類我創建了另一個PCL它的目標4.0框架和周圍寫了一個簡單的包裝(因爲它是在一個博客文章建議):System.Threading.Timer在PCL配置文件78中的問題 - 警告
public class PCLTimer
{
private Timer timer;
private Action<object> action;
public PCLTimer (Action<object> action, object state, int dueTimeMilliseconds, int periodMilliseconds)
{
this.action = action;
timer = new Timer (PCLTimerCallback, state, dueTimeMilliseconds, periodMilliseconds);
}
private void PCLTimerCallback (object state)
{
action.Invoke (state);
}
public bool Change (int dueTimeMilliseconds, int periodMilliseconds)
{
return timer.Change (dueTimeMilliseconds, periodMilliseconds);
}
}
現在我可以在主PCL庫中引用這個4.0庫,並使用PCLTimer 。但是,當我嘗試建立我的主要的Android項目,我得到以下警告:
Warning CS1684: Reference to type 'System.Threading.Timer' claims it is defined in 'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.5\Profile\Profile78\mscorlib.dll', but it could not be found (CS1684) (Prototype.Core)
Warning MSB3247: Found conflicts between different versions of the same dependent assembly. (MSB3247) (Prototype.Droid)
如何擺脫這些警告正確的?
請參閱http://stackoverflow.com/questions/12555049/timer-in-便攜式圖書館 - 包括來自PCL團隊的更新ms – Stuart
是的,我看到了這個問題,並從公認的答案中實現了第三種解決方案。現在我正在詢問這些警告。關於修復4.5.1 PCL庫的問題 - Xamarin工作室還沒有提供或者什麼?我的意思是我不能引用Timer,它不存在於我的PCL中的System.Threading命名空間中。 –
我建議不要使用只插入.NET 4.0 Timer的答案,而是使用Task.Delay構建自己的計時器類,如http://stackoverflow.com/a/21095323/957673中所述。這避免了完全引用System.Threading.Timer的問題。 – Bognar