【发布时间】:2020-07-07 10:38:04
【问题描述】:
所以我已经尝试解决这个问题几个小时了,我几乎放弃了。 必须有一种方法可以在另一个线程后面访问 Xamarin C# 代码上的普通标签。
这就是我基本上在做的事情:
namespace Traktor
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TrackerPage : ContentPage
{
public delegate void TrackedTimeUpdateHandler(double elapsedTime); // Delegate to notify the TrackingPage of how much time elapsed
private TimeTracker tracker;
// Ctor, and other methods etc. are here
// ...
// Initialize the tracker and add handler to it (called in Ctor)
void Initialize()
{
tracker = new TimeTracker();
tracker.TrackedTimeUpdate += new TrackedTimeUpdateHandler(UpdateGUI);
}
// This method is called periodically by the inner thread of 'tracker'
void UpdateGUI(double elapsedTime)
{
// Status label is a label on the page this refers to
statusLabel.Text = String.Format("{0:F2}", elapsedTime);
// Throws exception: "Android.Util.AndroidRuntimeException: 'Only the original thread that created a view hierarchy can touch its views.'"
}
}
}
一个单独的线程在TimeTracker tracker 中运行,它不断引发一个事件来通知这个 TrackerPage 已经过去了多少时间。然后调用UpdateGUI(...)- 方法。那只是将 statusLabel 的文本设置为经过的时间。一切都像魅力一样。但我无法通过 UI-Thread 本身以外的另一个线程访问statusLabel.Text。
我已经尝试了这么多,这很烦人,因为大多数情况下都丢失了一些东西,或者其他东西不起作用。比如statusLabel不是实现ISynchronizeInvoke的Class,或者我不能使用System.Windows.Forms.Control-命名空间。我不知道为什么。
所以我的问题是:
有没有办法访问标签(我的ContentPage 上的 UI 的一部分)并从另一个线程修改它(或以任何方式使elapsedTime 可以定期显示)?我不能调用和异步似乎不适合这里。我真的不知道如何解决这个问题。或者是否有在 UI 线程上调用的 Xamarin 原生方式?
到目前为止我所尝试的:
-> 我不能使用System.Windows.Forms.Control- 命名空间。我不知道为什么。也许是因为 Xamarin 不希望我这样做。所以我无法访问System.Windows.Forms.Control.Invoke 等。
【问题讨论】:
-
非常感谢!老实说,我不知道为什么我没有偶然发现。你能回答吗?或者我应该回答这个问题,以便我可以宣布这个问题已结束?
标签: c# android multithreading xamarin invoke