这仍然相当复杂,需要您指定这两个依赖项(它们应该已经隐含在您的项目中,因为它们由tauri本身使用).我使用了Tauri 1.6.1
的匹配版本,这些版本目前已经相当旧了:
webview2-com = "0.19.1"
windows = "0.39.0"
那么您可以直接投射(在这种情况下是ICoreWebView2_10
):
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use tauri::Manager;
use webview2_com::{Microsoft::Web::WebView2::Win32::*, *};
use windows::core::Interface;
fn main() {
tauri::Builder::default()
.setup(|app| {
let main_window = app.get_window("main").unwrap();
main_window
.with_webview(|webview| {
#[cfg(windows)]
unsafe {
let Ok(webview) = webview
.controller()
.CoreWebView2()
.unwrap()
.cast::<ICoreWebView2_10>() // This is where the magic happens.
else {
panic!("Failed to cast PlatformWebview.");
};
//webview.PrintToPdf().expect("Failed to print to pdf.");
}
})
.expect("Failed to get webview.");
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}