【问题标题】:I'm getting this error and need help fixing it if possible.I'm trying to build my project to see the output but it won't let me我收到此错误,如果可能,需要帮助修复它。我正在尝试构建我的项目以查看输出,但它不会让我
【发布时间】: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


【解决方案1】:

代码显然是错误的,没有更多上下文就无法修复它。但是,您真的应该问您的教授为什么它不起作用,而不是在 Stack Overflow 上发布您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 2019-09-08
    • 2021-12-12
    • 2022-11-22
    • 1970-01-01
    • 2021-02-16
    相关资源
    最近更新 更多