一个非常奇怪的场景......也许有人可以提供帮助。我突然意识到这一点。
private string _myText = "Hello";
public string MyText
{
get { return _myText; }
set
{
_myText = value;
OnPropertyChanged();
//OnPropertyChanged("IsSearching");
}
}
我将MyText绑定到XAML Xamarin视图中的Label,以调试运行此方法时发生的事情。
private async void Edit_Survey_Button_OnClicked(object sender, EventArgs e)
{
MyText = "Text Before Long Method";
Stopwatch s = new Stopwatch();
await Task.Run(() =>
{
s.Start();
Task.Delay(3000).Wait();
MyText = "Text after long method" + s.ElapsedMilliseconds.ToString();
});
}
因此,当单击按钮时,文本会更新,令人惊讶,但它只会在10-15秒内第二次更新,因为它应该在3中更新!我不知道为什么,我已经尝试了很多关于如何运行它的选项,但我想这可能与MyText设置在不同的线程上有关。
我正在进行此测试,因为我将有一个数据下载而不是延迟,而是在数据下载之后,我想从初始状态更改一些UI元素。现在我非常怀疑如何做到这一点,并希望其他一些人遇到类似的情况。
________按照SushiHangover的建议更新1
private void Edit_Survey_Button_OnClicked(object sender, EventArgs e)
{
IsSearching = "Method Start Edit";
Task.Run(async () =>
{
await Task.Delay(3000);
Device.BeginInvokeOnMainThread(() => { IsSearching = "Yeeeeehuuuuuuu!"; });
});
}
没有帮助;
答案 0 :(得分:1)
试试这个:
private async void Edit_Survey_Button_OnClicked(object sender, EventArgs e)
{
MyText = "Text Before Long Method";
Stopwatch s = new Stopwatch();
s.Start();
await Task.Delay(3000);
s.Stop();
MyText = "Text after long method" + s.ElapsedMilliseconds.ToString();
}
答案 1 :(得分:1)
我在IO模拟器和Android中运行此代码,它运行正常:
namespace Yuri1
{
public partial class Yuri1Page : ContentPage
{
async void Handle_Clicked(object sender, System.EventArgs e)
{
Stopwatch s = new Stopwatch();
await Task.Run(() =>
{
s.Start();
Task.Delay(3000).Wait();
});
MyEntry.Text="Text after long method: " + s.ElapsedMilliseconds.ToString();
}
//MyText.Text = "Text after long method" + s.ElapsedMilliseconds.ToString(); }
public Yuri1Page()
{
InitializeComponent();
}
}
}
答案 2 :(得分:0)
参与此次讨论的每个人都非常感谢。因此,在经过多次研究之后,似乎最合适的解决方案看起来像这样。在延迟任务完成后更新UI所需的litle延迟似乎工作正常。在示例中,我对线程ID进行了调试,并尝试了两种类型的绑定x引用和数据绑定。
List<string> threads = new List<string>();
private BindableProperty IsSearchingProperty =
BindableProperty.Create("IsSearching", typeof(string), typeof(MainPage), "Hello");
public string IsSearching
{
get { return (string)GetValue(IsSearchingProperty); }
set { SetValue(IsSearchingProperty, value); }
}
public MainPage()
{
BindingContext = this;
InitializeComponent();
threads.Add(GetThreadId() + " in constructor");
}
private string GetThreadId()
{
return Thread.CurrentThread.ManagedThreadId.ToString();
}
private async void Button_OnClicked(object sender, EventArgs e)
{
threads.Add(GetThreadId() + " in method");
IsSearching = "Method Start Edit";
MyRef.Text = "Reference Text Start";
Stopwatch s = new Stopwatch();
await Task.Run( () =>
{
threads.Add(GetThreadId() + " in task");
s.Start();
Task.Delay(3000).Wait();
Device.BeginInvokeOnMainThread(() =>
{
IsSearching = "Yeeeeehuuuuuuu!";
MyRef.Text = "Reference Text End" + s.ElapsedMilliseconds.ToString(); ;
threads.Add(GetThreadId() + " in task in device begin invoke");
});
});
threads.Add(GetThreadId() + " after task");
DisplayAlert("My Threads", DiplayThreadInfo(), "Cancel");
threads = null;
}
private string DiplayThreadInfo()
{
string threadsInfo = null;
foreach (var thread in threads)
{
threadsInfo ="/"+ threadsInfo + thread + "/"+ Environment.NewLine;
}
return threadsInfo;
}
}
在线程调试之后,我们可以看到UI更新在UI线程中执行。看来这是至关重要的,如果不这样做,问题就会开始发生。总的来说,这可以完成这项工作。谢谢大家。