简体   繁体   English

无法通过嵌套类型“FormMain.ImageDelegateClass”访问外部类型“FormMain”的非静态成员

[英]Cannot access a non-static member of outer type 'FormMain' via nested type 'FormMain.ImageDelegateClass'

I need to update a C# WinForms PictureBox from a MemoryStream input.我需要从 MemoryStream 输入更新 C# WinForms PictureBox。 I was able to accomplish this using我能够使用

pictureBox.Image = new Bitmap(new MemoryStream(payload));

in the thread that parses the stream [ RxThread() ] but was advised to use a delegate to avoid undesirable effects.在解析 stream [ RxThread() ] 的线程中,但建议使用委托以避免不良影响。 So I've implemented this:所以我实现了这个:

private void RxThread()
{
    ...
    var imageDelegateClass = new ImageDelegateClass();
    var imageDelegate = new ImageDelegate(imageDelegateClass.SetImage);
    imageDelegate(payload);
    ...
}

delegate void ImageDelegate(byte[] payload);
class ImageDelegateClass
{
    public void SetImage(byte[] payload)
    {
        pictureBox.Image = new Bitmap(new MemoryStream(payload));
    }
}

but get the following error code when I try to compile:但是当我尝试编译时得到以下错误代码:

Cannot access a non-static member of outer type 'FormMain' via nested type 'FormMain.ImageDelegateClass'无法通过嵌套类型“FormMain.ImageDelegateClass”访问外部类型“FormMain”的非静态成员

I am sure its a bad idea to make pictureBox static since it is WinForms generated.我确信制作图片框 static 是个坏主意,因为它是由 WinForms 生成的。 I know the repair is probably simple but I am a bit new to C#.我知道维修可能很简单,但我对 C# 有点陌生。 I have read the chapter on Delegates in Jon Skeets C# In Depth 2nd Edition multiple times but this is the first time I've actually tried to use one.我已经多次阅读 Jon Skeets C# In Depth 2nd Edition 中关于代表的章节,但这是我第一次真正尝试使用它。 How can I change SetImage() so it can access pictureBox?如何更改SetImage()以便它可以访问图片框?

You should move the method to the form class itself.您应该将该方法移至 class 本身的形式。
You don't need a separate class at all.您根本不需要单独的 class。

Also, you can use the built-in Action<byte[]> delegate instead of creating your own delegate type.此外,您可以使用内置的Action<byte[]>委托,而不是创建自己的委托类型。

Also, just calling the the delegate directly doesn't do you any good;此外,直接调用委托对您没有任何好处。 it will still run on the background thread.它仍将在后台线程上运行。

You need to call BeginInvoke(new Action<byte[]>(this.SetImage), payload) to run the delegate on the UI thread.您需要调用BeginInvoke(new Action<byte[]>(this.SetImage), payload)在 UI 线程上运行委托。

Two problems here: 1, your function that will be the delegate can and should be in the same class (as the other answer points out) instead of a class of its own.这里有两个问题:1,将成为代表的 function 可以而且应该在同一个 class 中(正如其他答案指出的那样),而不是自己的 class 。

Second, you are not following the advice of the previous answer correctly.其次,您没有正确遵循上一个答案的建议。 It is not enough to make the update in a delegate;在委托中进行更新是不够的; you have to use the Invoke method on your control to run the delegate so it runs in the UI thread.您必须在控件上使用 Invoke 方法来运行委托,以便它在 UI 线程中运行。 If you run the delegate the way you are doing now it is still on the same thread and you will still have the same problem.如果您按照现在的方式运行委托,它仍然在同一个线程上,您仍然会遇到同样的问题。

You can raise event in setImage to pass data and notification to main form.您可以在 setImage 中引发事件以将数据和通知传递给主窗体。 Other way is to use InvokeRequied, to move current call to the thread control was created.另一种方法是使用 InvokeRequied,将当前调用移动到创建的线程控件。

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

相关问题 无法通过嵌套类型访问外部类型的非静态成员 - Cannot access a non-static member of outer type via nested type 无法通过嵌套类型X访问外部类型X的非静态成员 - Cannot access a non-static member of outer type X via nested type X 无法使用第三方提供的类通过嵌套类型访问外部类型XXX的非静态成员 - Cannot access a non-static member of outer type XXX via nested type with a third-party provided class C#无法访问外部类型的非静态成员 - C# cannot access a non-static member of outer type 无法访问外部类型的非静态成员 - Cannot access a non-static member of outer type 使标签静态错误:无法通过嵌套类型“windowsForm8.Form1.DBConnect”访问外部类型“windowsForm8.Form1”的非静态成员 - make a label static error : cannot access a non-static member of outer type 'windowsForm8.Form1' via nested Type 'windowsForm8.Form1.DBConnect' 无法通过嵌套类型访问外部类型的非静态成员 - Cannot access a nonstatic member of outer type… via nested type 嵌套类:无法访问静态上下文中的非静态字段 - Nested class: Cannot access non-static field in static context 无法访问外部类型的非静态成员 - Cannot access a nonstatic member of outer type 使用静态类型成员以确保非静态类中的类型安全 - Using static type member to ensure type-safety in a non-static class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM