我正在嘗試將Windows.Graphics.Printing3DAPI 的功能集成到 WiseJ VB.Net 應用程式中以創建 .3mf 檔案。
Printing3D 方法依賴于 UWP,因此我創建了一個允許這些方法運行的子域。一切都很好,直到它遇到我的SaveTo3mf()方法,它利用FileSavePicker. 此時我得到一個InvalidWindowHandle例外,該方法在這一行失敗:
Dim storageFile = Await savePicker.PickSaveFileAsync()
我已經研究過這個問題并且我理解或認為我理解這個問題是子域在主域之外運行,因此它無法檢索視窗句柄。我嘗試解決這個問題如下所示,使用IInitializeWithWindow. 我曾經Invoke()相信它會將有問題的方法回傳到主域,但錯誤仍然存??在。
我將代碼拼湊在一起,不能聲稱在操作域或執行緒方面有任何專業知識。是另一種方法,還是我的實施中有錯誤?該代碼可以使用所需的參考正常編譯。
<ComImport>
<Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")>
<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Interface IInitializeWithWindow
Sub Initialize(ByVal hwnd As IntPtr)
End Interface
Delegate Sub Invoker(localPackage As Printing3D3MFPackage)
Public Async Sub Build3MF()
Dim packages = Await CreatePackageAsync()'method not shown, it seems to work properly once I added the sub-domain
Dim c1 As New FilePicker
Dim msd As Invoker = AddressOf c1.SaveTo3mf
msd.Invoke(packages)
End Sub
Private Class FilePicker
Inherits Page
Public Async Sub SaveTo3mf(localPackage As Printing3D3MFPackage)
Dim savePicker As FileSavePicker = New FileSavePicker()
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary
savePicker.DefaultFileExtension = ".3mf"
savePicker.FileTypeChoices.Add("3MF File", {".3mf"})
Dim hWnd = Me.Handle'I added this to see if it produced a result; it does
Dim initWindow As IInitializeWithWindow = CType(CObj(savePicker), IInitializeWithWindow)
initWindow.Initialize(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle)
Dim storageFile = Await savePicker.PickSaveFileAsync()'Fail point
If storageFile Is Nothing Then
Else
Using stream = Await localPackage.SaveAsync()
stream.Seek(0)
Using dataReader = New DataReader(stream)
Await dataReader.LoadAsync(CUInt(stream.Size))
Dim buffer = dataReader.ReadBuffer(CUInt(stream.Size))
Await FileIO.WriteBufferAsync(storageFile, buffer)
End Using
End Using
End If
End Sub
End Class
uj5u.com熱心網友回復:
對我來說,編程是艱難學習的另一種說法。通過學習 WiseJ,我對 vb.net 的理解得到了增強。它們有一個簡單的函式 Application.Download(File,"FileName")。這使我可以直接獲取流并在此程序中應用檔案名下載它。它沒有允許用戶選擇位置的優雅,但檔案被下載到“下載”檔案夾供用戶訪問。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/463823.html
