简体   繁体   English

WPF设计器实例化对象并导致编译器错误

[英]WPF designer instantiating object and causing compiler error

I have solution called MySol . 我有名为MySol的解决方案。

I have a DLL project in MySol called DeviceProj . 我在MySol中有一个名为DeviceProj的DLL项目。 This project defines a class: 该项目定义了一个类:

public class Device
{
    public int NumFields { get; set; }
}

I also have a WPF UserControl project in MySol called DeviceControl . 我在MySol中还有一个WPF UserControl项目,称为DeviceControl This class looks something like this: 这个类看起来像这样:

public class DeviceControl : UserControl
{
    private Device _MyDevice;

    public Device MyDevice 
    {
        get { return _MyDevice; }
        set
        {
            _MyDevice = value;
            for (var i = 0; i < _MyDevice.NumFields; i++)
                Main.Children.Add(new TextBox());
        }
    }

    public DeviceControl()
    {
        MyDevice = new Device { NumFields = 4 };

        InitializeComponent();
    }
}

Finally, I have a WPF Application project in MySol called DeviceWindow . 最后,我在MySol中有一个WPF应用程序项目,称为DeviceWindow When I add a DeviceControl to the DeviceWindow Designer, I see my control with 4 text boxes (this is expected: the designer creates an instance of DeviceControl and, since the default Device has its NumFields property set to 4, there are 4 text boxes). 当我将DeviceControl添加到DeviceWindow Designer时,我看到带有4个文本框的控件(这是预期的:设计器创建DeviceControl的实例,并且由于默认DeviceNumFields属性设置为4,所以有4个文本框) 。

But, when I compile MySol , I get the error: 但是,当我编译MySol时 ,出现错误:

Error 1 Unable to copy file "C:\\path\\to\\Device.dll" to "bin\\Debug\\Device.dll". 错误1无法将文件“ C:\\ path \\ to \\ Device.dll”复制到“ bin \\ Debug \\ Device.dll”。 The process cannot access the file 'bin\\Debug\\Device.dll' because it is being used by another process. 该进程无法访问文件“ bin \\ Debug \\ Device.dll”,因为它正在被另一个进程使用。

I assume this is because an instance of Device is being used by the WPF designer, so the DLL cannot be overwritten. 我认为这是因为WPF设计正在使用Device的实例,因此DLL无法被覆盖。

Is there any way around this problem? 有什么办法可以解决这个问题?

Shorter, easier to understand (maybe?) explanation: 更简短,更容易理解(也许是?)的解释:

The WPF Window designer creates an instance of all UserControls declared in the window. WPF窗口设计器创建窗口中声明的所有UserControl的实例。 If one of those UserControls contains a reference to an object declared in a different project (we'll call it ObjectProject ) in the same solution, the solution will not build because the ObjectProject .dll will be in use by the WPF designer. 如果这些UserControl中的一个包含对在同一解决方案中在不同项目(我们称为ObjectProject )中声明的对象的引用,则该解决方案将无法构建,因为WPF设计人员将使用ObjectProject .dll。 Visual Studio cannot overwrite .dll files that are in use. Visual Studio无法覆盖正在使用的.dll文件。

The cause of this issue is your .DLL and your WPF application targets may differ. 此问题的原因是您的.DLL和WPF应用程序目标可能不同。 Your application target may be Client Profile. 您的应用程序目标可能是“客户资料”。 Keep both the target to same. 保持两个目标相同。

For example, if your library targets .NET 4, make sure your WPF Application targets also 4, not client profile. 例如,如果您的库以.NET 4为目标,请确保WPF应用程序也以4为目标,而不是客户端配置文件。

Go to C:\\path\\to\\Device.dll and bin\\Debug\\Device.dll folder of your project then delete the Device.dll file from these two locations. 转到项目的C:\\path\\to\\Device.dllbin\\Debug\\Device.dll文件夹,然后从这两个位置删除Device.dll文件。 When you run your project again, these files will be created automatically in the directory. 当您再次运行项目时,这些文件将在目录中自动创建。 Then you can build without this error. 然后,您可以进行构建而不会出现此错误。

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

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