2501_93799267 2025-10-15 23:37 采纳率: 0%
浏览 4

tauri多webview遇到的问题

问题

我想使用 tauri 时,可以使用类似 electron 的 , 我当初的想法也是,用div占位, 用 webview填充, 和

https://blog.csdn.net/gitblog_00343/article/details/151808322

描述的一样 , 我在页面 onmount 后调用接口, 或者点击界面调用接口, 但是都会出现一个透明框,覆盖整个电脑界面。
创建的windows界面代码:

            // 第二个窗口
            let _second_window =
                WebviewWindowBuilder::new(app, "second", WebviewUrl::App("/#/layout".into()))
                    .title("副窗口")
                    .position(0.0, 0.0)
                    .decorations(false) // 去掉标题栏
                    .maximized(true) // 最大化
                    .transparent(true) // 透明
                    .always_on_top(true)
                    .skip_taskbar(true)   
                    .build()?;

调用的接口代码:

use tauri::{
  AppHandle, Manager, Runtime, WebviewBuilder, WebviewUrl, LogicalPosition, LogicalSize
};

#[tauri::command]
pub fn createwebview<R: Runtime>(app: AppHandle<R>, window_label: &str, url: &str, width: f64, height: f64) -> Result<(), String>{
  let parent_window = app.get_window("main").ok_or_else(|| format!("找不到标识为'{}'的窗口", window_label))?;
  println!("I was invoked from JavaScript! {} {} {} {} ", window_label, url, width, height); // 这里在控制台显示
  
  // 创建子WebView
  let _webview1 = parent_window.add_child(
        tauri::webview::WebviewBuilder::new("main3", WebviewUrl::External("https://flowus.cn/".parse().unwrap()))
        .auto_resize(),
        tauri::LogicalPosition::new(20., 20.),
        tauri::LogicalSize::new( 60.0,  60.0),
    ).map_err(|e| format!("创建WebView失败: {}", e))?;

    println!("创建 webview1");   // 这里在控制台不显示, 且没有见上面创建webview失败的报错
    Ok(())
}

前端点击事件调用接口:

const test = () => {
    console.log('click -- ', windowLabel, elementHeight, elementWidth)
    // 调用Rust命令,传递当前窗口label和WebView参数
    invoke('createwebview', {
      windowLabel: 'second',
      url: 'https://flowus.cn/',
      width: 400,
      height: 300,
      x: 50,  // 相对于父窗口的X偏移
      y: 50   // 相对于父窗口的Y偏移
    });
}

点击之后, 出现透明框,用任务管理器关掉之后, exe删不掉显示被占用 。

如果 window窗口 不要最大化,按默认大小, 点击后没有展示 webview

  • 写回答

5条回答 默认 最新

  • 阿里嘎多学长 2025-10-15 23:37
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    问题理解

    你想在 Tauri 中使用多个 WebView,类似 Electron 的方式。具体来说,你想使用 div 占位,然后用 webview 填充,类似于 Electron 中的渲染器。

    解决方案

    Tauri 中可以使用 webview 组件来实现多个 WebView。具体步骤如下:

    1. tauri.conf.json 中配置 webview 组件:
    {
      "tauri": {
        "build": {
          "target": ["windows", "macos", "linux"]
        },
        "plugins": {
          "webview": {
            "enabled": true
          }
        }
      }
    }
    
    1. 在你的 Rust 代码中使用 webview 组件:
    use tauri::WindowBuilder;
    
    fn main() {
        let app = tauri::Builder::default()
            .invoke_handler(tauri::generate_handler![])
            .run(tauri::generate_context!())
            .expect("error while running tauri application");
    
        let window = WindowBuilder::new(app)
            .title("My Window")
            .build()
            .expect("failed to create window");
    
        let webview = webview::Builder::new(window)
            .title("My WebView")
            .content_url("https://example.com")
            .build()
            .expect("failed to create webview");
    
        // ...
    }
    
    1. 在你的 HTML 中使用 div 占位,然后用 webview 填充:
    <!DOCTYPE html>
    <html>
      <head>
        <title>My WebView</title>
      </head>
      <body>
        <div id="webview-container"></div>
        <script>
          const webview = document.getElementById("webview-container");
          webview.innerHTML = "<webview src='https://example.com'></webview>";
        </script>
      </body>
    </html>
    

    注意

    • 在 Tauri 中,webview 组件需要在 tauri.conf.json 中配置为启用状态。
    • 在你的 Rust 代码中,需要使用 webview 组件来创建 WebView。
    • 在你的 HTML 中,需要使用 div 占位,然后用 webview 填充。

    希望这个解决方案能够帮助你解决问题!

    评论

报告相同问题?

问题事件

  • 创建了问题 10月15日