【发布时间】:2020-08-10 14:05:38
【问题描述】:
CS0029 无法将类型“System.Threading.Timer”隐式转换为“Windows.UI.Xaml.DispatcherTimer” 这是我收到的错误代码,我相信这是我无法构建项目的原因。我的教授主持了一次缩放会议并制作了一个 youtube 视频,这是他写的一些代码,我们过去了。
namespace DataCollector
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
private DispatcherTimer timer;
MeasureLengthDevice measurementDevice = null;
MainViewData displayData = new MainViewData();
Frame frame = null;
MainPage page = null;
public MainPage()
{
this.InitializeComponent();
timer = new Timer(Timer_Tick, null, (int)TimeSpan.FromSeconds(1).TotalMilliseconds,
(int)TimeSpan.FromSeconds(15).TotalMilliseconds);
measurementDevice = new MeasureLengthDevice();
displayData = new MainViewData
{
Measurement = measurementDevice.ToString(),
History = measurementDevice.History
};
}
private void Button_Click(object sender, RoutedEventArgs e)
{
frame = (Frame)Window.Current.Content;
page = (MainPage)frame.Content;
measure.Text = measurementDevice.Measurement.ToString();
measureHistory.Text = measurementDevice.History;
}
private async void Timer_Tick(object state)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
if (page != null)
{
page.measure.Text = measurementDevice.Measurement.ToString();
}
displayData.History = measurementDevice.History;
});
}
}
}
【问题讨论】:
-
根据你显示的错误,我建议你可以修改代码'timer = new Timer(Timer_Tick, null, (int)TimeSpan.FromSeconds(1).TotalMilliseconds, (int)TimeSpan. FromSeconds(15).TotalMilliseconds);'到“计时器”=新的 DispatcherTimer();。如果您想了解更多关于dispactchtimer的信息,可以参考DispatcherTimer Class。
标签: c# visual-studio error-handling