简体   繁体   English

C# VS2015 从表单更改为通用应用程序:缺少程序集错误

[英]C# VS2015 Changing from Forms to Universal Apps: Missing assembly error

I am writing a little program to control LEGO Mindstorms car using monobrick C# library from Monobrick.dk .我写了一个小程序使用monobrick C#库从控制LEGO MINDSTORMS车Monobrick.dk I have used the library in Windows Forms and it worked.我已经在 Windows 窗体中使用了该库并且它起作用了。 As I heard Forms are outdated way to write Windows apps so I would like to switch to Windows Universal Platform.我听说 Forms 是编写 Windows 应用程序的过时方式,所以我想切换到 Windows 通用平台。 I made a little code similar to the working one but I get error:我制作了一些类似于工作代码的小代码,但出现错误:

The type or namespace name 'Thread' does not exist in the namespace 'System.Threading' (are you missing an assembly reference?) App1 \\App1\\MainPage.xaml.cs 37命名空间“System.Threading”中不存在类型或命名空间名称“Thread”(您是否缺少程序集引用?)App1\\App1\\MainPage.xaml.cs 37

It is strange cause in Forms "System.Threading.Thread" worked.表单“System.Threading.Thread”工作的原因很奇怪。

Here is the code.这是代码。 It is supposed to turn Motor A in half power for 3 secs.它应该以一半的功率转动电机 A 3 秒。 after pushing an on screen button.按下屏幕上的按钮后。

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    using MonoBrick.EV3;

    namespace App1
    {
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        var ev3 = new Brick<Sensor, Sensor, Sensor, Sensor>("wifi");
        ev3.Connection.Open();
        ev3.MotorA.On(50);
        System.Threading.Thread.Sleep(3000);
        ev3.MotorA.Off();
           }
        }
    }

What am I missing here?我在这里缺少什么? Isn't WUP a little overkill for beginner? WUP 对初学者来说是不是有点矫枉过正? Maybe I should stick to WPF?也许我应该坚持使用 WPF?

Will be grateful for your advice.将不胜感激您的建议。

The System.Threading.Thread has been removed from the Windows Runtime (which powers the Universal Windows Platform). System.Threading.Thread 已从 Windows 运行时(为通用 Windows 平台提供支持)中删除。 Instead, Microsoft wants you to focus on asynchronous programming patterns instead of using the Thread class directly (see this page for the asynchronous programming pattern documentation).相反,Microsoft 希望您专注于异步编程模式,而不是直接使用 Thread 类(有关异步编程模式文档,请参阅此页面)。

As answered in this question you should use the Task.Delay method, so your code would become something along the following lines:正如在这个问题中所回答的,您应该使用Task.Delay方法,因此您的代码将变成以下几行:

private async void button_Click(object sender, RoutedEventArgs e)
{
    // code before
    await Task.Delay(3000);
    // code after
}

This has the added benefit that you UI would not become unresponsive, as the Thread.Sleep method would freeze the UI thread if it was not executed in another thread or asynchronous call.这有一个额外的好处,您的 UI 不会变得无响应,因为Thread.Sleep方法将冻结 UI 线程,如果它没有在另一个线程或异步调用中执行。

You also might want to read this article about asynchronous programming in C# and VB.NET .您可能还想阅读这篇有关 C# 和 VB.NET 异步编程的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM