【发布时间】:2014-05-02 10:17:57
【问题描述】:
我很惊讶 Profile 78 库中不存在方便的 System.Threading.Timer 类。为了使用这个类,我创建了另一个针对 4.0 框架的 PCL,并编写了一个简单的包装器(正如一篇博文中所建议的那样):
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);
}
}
现在我可以引用这个 4.0 库并在主 PCL 库中使用 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)
如何正确消除这些警告?
【问题讨论】:
-
请参阅stackoverflow.com/questions/12555049/timer-in-portable-library - 包括来自 PCL 团队在 ms 中的更新
-
是的,我看到了这个问题,并从接受的答案中实施了类似于第 3 个解决方案的方法。现在我在问这些警告。 4.5.1 PCL 库的修复怎么样 - Xamarin Studio 中还没有它还是什么?我的意思是我不能引用 Timer,它在我的 PCL 的 System.Threading 命名空间中不存在。
-
我建议不要使用仅插入 .NET 4.0 Timer 的答案,而是使用 Task.Delay 构建您自己的计时器类,例如 stackoverflow.com/a/21095323/957673。这避免了完全引用 System.Threading.Timer 的问题。
-
我喜欢 Timer 类,我在很多项目中都使用过它,它测试了无数次。我不想为这么简单的事情实现我自己的类。但如果什么都没有了,也许我不得不)
标签: c# android xamarin mvvmcross portable-class-library