C# 将模型绑定到DLL函数
我有一个WinForms应用程序,我正在重写它以使用WPF和MVVM模式。 它使用了用C++编写的DLL来公开一些我需要在WPF应用程序中绑定模型的函数。 例如,我有这个函数从DLLC# 将模型绑定到DLL函数,c#,wpf,mvvm,C#,Wpf,Mvvm,我有一个WinForms应用程序,我正在重写它以使用WPF和MVVM模式。 它使用了用C++编写的DLL来公开一些我需要在WPF应用程序中绑定模型的函数。 例如,我有这个函数从DLL // The user supplied function will be called whenever a frame of data arrives. SetDataHandlerFunc(void (*MyFunction)(sFrameOfData* pFrameOfData)); 我有一个包装器dll
// The user supplied function will be called whenever a frame of data arrives.
SetDataHandlerFunc(void (*MyFunction)(sFrameOfData* pFrameOfData));
我有一个包装器dll,如下所示:
public delegate void HandlerFunction(IntPtr frameData);
[DllImport("lib\\SDK.dll")]
public extern static int SetDataHandlerFunc(HandlerFunction function);
private HandlerFunction myFunction;
myFunction = new HandlerFunction(threadFunction);
private void threadFunction(IntPtr FrameData)
{
sFrameOfData frame = (sFrameOfData)Marshal.PtrToStructure(FrameData, typeof(sFrameOfData));
}
让我以后在表单应用程序中使用它,如下所示:
public delegate void HandlerFunction(IntPtr frameData);
[DllImport("lib\\SDK.dll")]
public extern static int SetDataHandlerFunc(HandlerFunction function);
private HandlerFunction myFunction;
myFunction = new HandlerFunction(threadFunction);
private void threadFunction(IntPtr FrameData)
{
sFrameOfData frame = (sFrameOfData)Marshal.PtrToStructure(FrameData, typeof(sFrameOfData));
}
但我真的不知道如何将其转换为MVVM和WPF数据绑定。
我想我可能正在考虑使用ObjectDataProvider绑定到一个方法
我需要从帧数据(上面作为sFrameOfData对象返回)中获取一些参数,并在GUI中显示它们,并随着数据的更改进行更新
关于如何实现这样的东西有什么想法吗?您实际上不会绑定到该方法。 在视图模型中,具有表示您感兴趣的参数并绑定到这些参数的属性 因此,在threadFunction中,返回sFrameOfData对象,然后在viewmodel上设置属性值。
确保实现INotifyPropertyChanged以将property changed事件引发到视图中,在该视图中,您有绑定到viewmodel上的属性的控件。您能不能在视图模型上以及调用threadfunction时为这些参数创建属性(我假设这是从dll调用的,以便在需要时更新框架数据),更新那些属性(应该已经发出更改通知)?从而更新绑定的xaml控件?这些控件被绑定为上述属性。