Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单的c#directX示例_C#_.net_Directx - Fatal编程技术网

简单的c#directX示例

简单的c#directX示例,c#,.net,directx,C#,.net,Directx,我尝试通过本教程开始c#directX编程: 我正在使用visual Studion Community 2015,我的代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks

我尝试通过本教程开始c#directX编程: 我正在使用visual Studion Community 2015,我的代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace DirecxTest01
{
    public partial class Form1 : Form
    {
        private Device device;
        public Form1()
        {
            InitializeComponent();
            InitializeDevice();
        }
        private void InitializeDevice()
        {
            PresentParameters presentParams = new PresentParameters();
            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;
            device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0);
            device.Present();
        }
    }
}
我还添加了对所有DirectX dll的引用。当我运行程序时,什么都没有发生,甚至窗体窗口也没有出现。没有错误消息,什么都没有。我试着逐行注释DirectX的内容。即使使用此代码,程序也会挂起:

private void InitializeDevice()
    {
        PresentParameters presentParams = new PresentParameters();
        //presentParams.Windowed = true;
        //presentParams.SwapEffect = SwapEffect.Discard;
        //device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
        }
当我发表评论时

//PresentParameters presentParams=new PresentParameters()

到,至少会出现Windowas窗体


我的代码有什么问题?

您正在使用的教程适用于不推荐使用的托管DirectX 1.1程序集。它们是为.NET1.1编写的,与.NET4.0或更高版本不兼容——尽管有一些黑客试图让它们工作

  • 由于托管DirectX 1.1支持的D3DX9的最后一个版本是2006年4月,因此它使用了非常过时的HLSL编译器版本
  • 托管DirectX 1.1程序集仅为32位,因此您无法从x64本机.NET应用程序中使用它们(Windows x64系统上的
    /platform:anycpu
    )。您必须使用
    /platform:x86
    进行构建,并保持在32位应用程序2 GB内存空间的限制范围内
  • 这些程序集仅支持传统的DirectX API集,不支持Direct3D9Ex、Direct3D 10.x、Direct3D 11、Direct2D、DirectWrite、DXGI、D3DX10、D3DX11、XAUDIO2、XACT、XINPUT等
  • 由于托管DirectX 2.0从未以生产形式发布,托管DirectX 1.1程序集仍然反映.NET 1.1设计原则,不支持或使用.NET 2.0构造
  • 托管DirectX 1.1与.NET 2.0(以及2.0运行时的.NET 3.0和.NET 3.5扩展)兼容,但与.NET 4.0不兼容
  • 托管DirectX 1.1程序集仅由旧版DirectSetup和旧版DirectX SDK部署
简而言之:不要使用它们。使用更现代的工具,如SharpDX、SlimDX或Unity3D


请参阅和

我知道这是一个旧线程,但我遇到了确切的问题,并且“不使用DirectX”的解决方案不是一个选项,因为我在现有系统上工作,没有使用DirectX DLL升级所有相关部分的选项

幸运的是,我找到了一个在.NET Framework 4.6.1版上使用旧DirectX程序集的解决方案

只需将其添加到App.config文件中(就在
的正下方):


根据“截至2019年3月29日,SharpDX不再处于开发或维护阶段。”看起来SlimDX也不再处于积极维护阶段。(我知道这个答案已经有好几年了)。SharpDX代码是可用的,所以您仍然可以使用它。遗憾的是,他们无法让它继续运行,但至少它比遗留的托管DirectX 1.1更好,后者在近二十年内没有更新过,而且是封闭源代码的。SlimDX也已退役,因为维护人员在2008年左右停止了它的工作,但SlimDX更像是传统托管DX 1.1的“直接替代品”。
<startup useLegacyV2RuntimeActivationPolicy="true" >
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.61"/>
</startup>