所以我遇到了一个前所未有的问题,我无法找到有关我具体问题的任何信息。基本上,当我启动程序时,我有一个显示时间和天气信息的面板。当我启动它时,天气会更新以及没有问题的时间(问题从//错误开始!!!)我认为这可能与计时器有关,因为没有计时器它可以正常工作但是我需要它,所以我可以不断刷新时间和天气。我会在最后加入错误。
这可能是什么原因?如果您需要更多信息,请告诉我们!
public async void WeatherInfoRefresh()
{
TimeRefresh();
RootObject myWeather = await CurrentWeather.GetWeather("Adelaide");
ResultTextBlock.Text = myWeather.name + " • " + myWeather.main.temp + "°C";
if (myWeather.weather[0].description == "broken clouds")
{
WeatherImage.Source = new BitmapImage(new Uri("ms-appx:///Assets/Files/AAA/brokenclouds.png"));
}
}
我的计时器空洞:
public void startTimer()
{
ticker = new Timer(TimerMethod, null, 30000, 3000);
}
public void TimerMethod(object state)
{
WeatherInfoRefresh();
}
我的时间刷新无效:
public void TimeRefresh()
{
//Timedate
DayTextBlock.Text = DateTime.Now.ToString("dddd"); //ERROR HERE - This almost always works without the timer!!!
DateTextBlock.Text = DateTime.Now.ToString("dd MMMM");
TimeTextBlock.Text = DateTime.Now.ToString("h:mm tt");
}
错误:
An exception of type 'System.Exception' occurred in Mashy Home Automation.exe but was not handled in user code
Additional information: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
答案 0 :(得分:3)
计时器和UI在不同的线程上运行,您无法从计时器线程更新UI。