【桌面应用开发】Windows 环境下 Dioxus 桌面应用开发环境搭建

Windows 环境下 Dioxus 桌面应用开发环境搭建

    • 一、环境准备与系统要求
      • [1.1 系统要求](#1.1 系统要求)
      • [1.2 环境架构图](#1.2 环境架构图)
    • 二、完整安装步骤
      • [2.1 第一步:安装 Rust 工具链](#2.1 第一步:安装 Rust 工具链)
      • [2.2 第二步:安装 Windows 构建工具](#2.2 第二步:安装 Windows 构建工具)
      • [2.3 第三步:安装 Dioxus CLI 和相关工具](#2.3 第三步:安装 Dioxus CLI 和相关工具)
      • [2.4 第四步:安装开发工具](#2.4 第四步:安装开发工具)
        • [安装 VS Code](#安装 VS Code)
        • [安装 Git](#安装 Git)
      • [2.5 第五步:可选工具安装](#2.5 第五步:可选工具安装)
        • [安装 Node.js (用于 Web 开发)](#安装 Node.js (用于 Web 开发))
        • [安装 Chocolatey (Windows 包管理器)](#安装 Chocolatey (Windows 包管理器))
    • [三、创建第一个 Dioxus 桌面应用](#三、创建第一个 Dioxus 桌面应用)
      • [3.1 创建新项目](#3.1 创建新项目)
      • [3.2 项目结构](#3.2 项目结构)
      • [3.3 编写示例代码](#3.3 编写示例代码)
      • [3.4 运行应用](#3.4 运行应用)
    • 四、构建和发布
      • [4.1 调试构建](#4.1 调试构建)
      • [4.2 发布构建](#4.2 发布构建)
      • [4.3 打包为安装程序](#4.3 打包为安装程序)
    • 五、常见问题解决
      • [5.1 编译错误解决](#5.1 编译错误解决)
        • [问题1: 链接器错误](#问题1: 链接器错误)
        • [问题2: 找不到 wasm-bindgen](#问题2: 找不到 wasm-bindgen)
      • [5.2 性能优化](#5.2 性能优化)
      • [5.3 网络问题解决](#5.3 网络问题解决)
    • 六、进阶配置
      • [6.1 配置桌面应用属性](#6.1 配置桌面应用属性)
      • [6.2 添加图标和资源](#6.2 添加图标和资源)
      • [6.3 系统托盘和菜单](#6.3 系统托盘和菜单)
    • 七、项目模板和示例
      • [7.1 使用官方模板](#7.1 使用官方模板)
      • [7.2 社区模板](#7.2 社区模板)
    • 八、调试和测试
      • [8.1 调试配置](#8.1 调试配置)
      • [8.2 日志配置](#8.2 日志配置)
    • 九、性能监控
      • [9.1 添加性能监控](#9.1 添加性能监控)
    • 十、打包发布清单
    • 关联知识
    • 总结

一、环境准备与系统要求

1.1 系统要求

  • 操作系统: Windows 10/11 (64位)
  • 内存: 至少 8GB RAM (推荐 16GB+)
  • 硬盘空间: 至少 10GB 可用空间
  • 管理员权限: 需要管理员权限安装部分工具

1.2 环境架构图

复制代码
┌─────────────────────────────────────────────────────┐
│              Windows 开发环境                        │
├─────────────────────────────────────────────────────┤
│  Rust 工具链 (rustup, cargo, rustc)                  │
│  ├─── MSVC 构建工具 (Visual Studio Build Tools)       │
│  ├─── Windows SDK                                   │
│  └─── LLVM/Clang                                    │
├─────────────────────────────────────────────────────┤
│  Dioxus CLI 和依赖                                    │
│  ├─── Dioxus-CLI                                    │
│  ├─── Trunk (WASM 打包工具)                          │
│  └─── Tauri (桌面应用框架)                            │
├─────────────────────────────────────────────────────┤
│  开发工具                                             │
│  ├─── VS Code + 扩展                                 │
│  ├─── Git                                           │
│  └─── Web 开发工具 (Node.js 可选)                     │
└─────────────────────────────────────────────────────┘

二、完整安装步骤

2.1 第一步:安装 Rust 工具链

方法A:使用 rustup(推荐)
powershell 复制代码
# 1. 下载并运行 rustup-init.exe
# 访问: https://rustup.rs/
# 或使用 PowerShell 命令:

# 以管理员身份打开 PowerShell
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# 如果 curl 不可用,使用以下命令:
iwr -useb https://win.rustup.rs/x86_64 | iex

# 2. 安装过程选择选项
# 选择 "1) Proceed with standard installation"
# 安装完成后重启终端

# 3. 验证安装
rustc --version
cargo --version
rustup --version

# 4. 添加目标平台(用于桌面应用)
rustup target add wasm32-unknown-unknown
方法B:手动下载安装
powershell 复制代码
# 1. 下载 rustup-init.exe
# https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe

# 2. 运行安装程序,按照提示完成

# 3. 设置系统环境变量(如果未自动添加)
# 添加到 PATH: C:\Users\用户名\.cargo\bin

2.2 第二步:安装 Windows 构建工具

必须安装的组件:
powershell 复制代码
# 1. 安装 Visual Studio Build Tools
# 下载地址: https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2022

# 安装时选择:
# - "使用C++的桌面开发"
# - Windows 10/11 SDK
# - MSVC v143 - VS 2022 C++ x64/x86 生成工具
# - C++ CMake 工具

# 2. 或者安装 Visual Studio Community 版本(包含所有必要组件)
# 下载地址: https://visualstudio.microsoft.com/vs/community/

# 安装时工作负载选择:
# - "使用C++的桌面开发"
# - ".NET 桌面开发" (如果使用WPF后端)
验证 MSVC 安装:
powershell 复制代码
# 检查 cl.exe 是否可用
cl
# 如果命令不存在,可能需要运行 vcvars
# 通常在: "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"

# 永久设置环境变量(可选)
[System.Environment]::SetEnvironmentVariable(
    "PATH", 
    [System.Environment]::GetEnvironmentVariable("PATH", "User") + 
    ";C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.xx.xxxxx\bin\Hostx64\x64",
    "User"
)

2.3 第三步:安装 Dioxus CLI 和相关工具

powershell 复制代码
# 1. 安装 Dioxus CLI
cargo install dioxus-cli --features desktop

# 如果安装缓慢,可以使用国内镜像
# 设置 Rust 国内镜像
# 在 C:\Users\用户名\.cargo 下创建 config 文件,内容:
[source.crates-io]
replace-with = 'tuna'

[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"

# 2. 验证安装
dx --version

# 3. 安装 Trunk (WASM 构建工具)
cargo install trunk

# 4. 安装 Tauri CLI (用于桌面应用打包)
cargo install tauri-cli

# 5. 安装 wasm-bindgen
cargo install wasm-bindgen-cli

2.4 第四步:安装开发工具

安装 VS Code
powershell 复制代码
# 1. 下载并安装 VS Code
# https://code.visualstudio.com/download

# 2. 安装必要的扩展
# 打开 VS Code,按 Ctrl+Shift+X,搜索并安装:

# 必须安装的扩展:
# - rust-analyzer (Rust 语言支持)
# - Dioxus Language Server (Dioxus 支持)
# - Tauri (Tauri 框架支持)
# - Error Lens (错误提示增强)
# - GitLens (Git 增强)

# 3. 配置 VS Code
# 创建或修改 settings.json:
{
    "rust-analyzer.checkOnSave.command": "clippy",
    "rust-analyzer.linkedProjects": [
        "./Cargo.toml"
    ],
    "editor.formatOnSave": true,
    "files.autoSave": "afterDelay"
}
安装 Git
powershell 复制代码
# 下载并安装 Git for Windows
# https://git-scm.com/download/win

# 安装时选择:
# - 将 Git 添加到 PATH
# - 使用 OpenSSL 库
# - 检出 Windows 风格,提交 Unix 风格行结束符
# - 使用 Windows 的默认控制台窗口

# 验证安装
git --version

2.5 第五步:可选工具安装

安装 Node.js (用于 Web 开发)
powershell 复制代码
# 1. 下载 Node.js LTS 版本
# https://nodejs.org/

# 2. 安装验证
node --version
npm --version

# 3. 设置 npm 国内镜像(可选)
npm config set registry https://registry.npmmirror.com
安装 Chocolatey (Windows 包管理器)
powershell 复制代码
# 以管理员身份运行 PowerShell
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

# 验证安装
choco --version

三、创建第一个 Dioxus 桌面应用

3.1 创建新项目

powershell 复制代码
# 1. 创建工作目录
mkdir dioxus-projects
cd dioxus-projects

# 2. 创建桌面应用
dx new my-first-app --platform desktop

# 或者手动创建:
# 创建项目目录
mkdir my-desktop-app
cd my-desktop-app

# 初始化 Cargo 项目
cargo init

# 添加依赖
# 编辑 Cargo.toml:

Cargo.toml 配置示例:

toml 复制代码
[package]
name = "my-desktop-app"
version = "0.1.0"
edition = "2021"

[dependencies]
dioxus = "0.5"
dioxus-desktop = "0.5"
serde = { version = "1.0", features = ["derive"] }

[build-dependencies]
dioxus-cli = "0.5"

3.2 项目结构

复制代码
my-desktop-app/
├── Cargo.toml          # Rust 项目配置
├── Cargo.lock         # 依赖锁文件
├── src/
│   ├── main.rs        # 应用入口
│   └── components/     # 组件目录
├── assets/            # 静态资源
│   ├── favicon.ico
│   └── style.css
├── public/            # 公共文件
└── target/            # 构建输出

3.3 编写示例代码

src/main.rs:

rust 复制代码
use dioxus::prelude::*;
use dioxus_desktop::Config;

fn main() {
    // 启动 Dioxus 桌面应用
    dioxus_desktop::launch_cfg(
        app,
        Config::new()
            .with_window(
                dioxus_desktop::WindowBuilder::new()
                    .with_title("我的第一个 Dioxus 应用")
                    .with_inner_size(dioxus_desktop::LogicalSize::new(800.0, 600.0))
            )
    );
}

fn app(cx: Scope) -> Element {
    let mut count = use_state(cx, || 0);
    
    cx.render(rsx! {
        div {
            style: "text-align: center; font-family: Arial, sans-serif;",
            
            h1 { "欢迎使用 Dioxus! 🎉" }
            p { "计数器: {count}" }
            
            button {
                onclick: move |_| count += 1,
                style: "
                    padding: 10px 20px;
                    font-size: 16px;
                    margin: 5px;
                    background-color: #4CAF50;
                    color: white;
                    border: none;
                    border-radius: 5px;
                    cursor: pointer;
                ",
                "增加"
            }
            
            button {
                onclick: move |_| count.set(0),
                style: "
                    padding: 10px 20px;
                    font-size: 16px;
                    margin: 5px;
                    background-color: #f44336;
                    color: white;
                    border: none;
                    border-radius: 5px;
                    cursor: pointer;
                ",
                "重置"
            }
            
            br {}
            
            p {
                style: "margin-top: 20px; color: #666;",
                "这是一个使用 Dioxus 构建的桌面应用"
            }
        }
    })
}

3.4 运行应用

powershell 复制代码
# 1. 进入项目目录
cd my-desktop-app

# 2. 运行开发模式
dx serve --platform desktop
# 或
cargo run

# 3. 应用将在 http://localhost:8080 可用
# 桌面窗口会自动打开

# 4. 热重载功能
# 修改代码后会自动重新编译和刷新

四、构建和发布

4.1 调试构建

powershell 复制代码
# 调试模式构建
cargo build
# 或
dx build --platform desktop

# 构建产物在: target/debug/my-desktop-app.exe

4.2 发布构建

powershell 复制代码
# 1. 发布模式构建(优化)
cargo build --release
# 或
dx build --release --platform desktop

# 构建产物在: target/release/my-desktop-app.exe

# 2. 查看构建文件大小
Get-ChildItem "target/release/my-desktop-app.exe" | Select-Object Name, Length

4.3 打包为安装程序

powershell 复制代码
# 1. 安装 WiX Toolset (用于创建 MSI 安装包)
# 下载地址: https://wixtoolset.org/docs/wix3/

# 2. 使用 Tauri 打包
# 首先添加 Tauri 依赖
cargo add tauri

# 创建 tauri.conf.json
# 运行打包命令
cargo tauri build

# 3. 或者使用 Inno Setup
# 下载地址: https://jrsoftware.org/isdl.php

五、常见问题解决

5.1 编译错误解决

问题1: 链接器错误
复制代码
error: linking with `link.exe` failed

解决方案:

powershell 复制代码
# 确保安装了正确的构建工具
# 重新运行 Visual Studio Installer,确保选择了:
# - MSVC v143 - VS 2022 C++ x64/x86 build tools
# - Windows 10/11 SDK

# 或者运行 vcvars
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
问题2: 找不到 wasm-bindgen
复制代码
error: couldn't find the wasm-bindgen executable

解决方案:

powershell 复制代码
# 安装 wasm-bindgen
cargo install wasm-bindgen-cli

# 或添加到 Cargo.toml
[dependencies]
wasm-bindgen = "0.2"

5.2 性能优化

减少构建时间
toml 复制代码
# 在项目根目录创建 .cargo/config.toml
[build]
rustflags = ["-C", "target-cpu=native"]

[target.wasm32-unknown-unknown]
rustflags = ["-C", "opt-level=z", "-C", "lto"]

# 使用 sccache 加速编译
# 安装 sccache
cargo install sccache

# 设置环境变量
[env]
RUSTC_WRAPPER = "sccache"

5.3 网络问题解决

使用国内镜像
toml 复制代码
# 创建或修改 C:\Users\用户名\.cargo\config
[source.crates-io]
replace-with = 'ustc'

[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

# 或者使用 清华大学镜像
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"

[net]
git-fetch-with-cli = true

六、进阶配置

6.1 配置桌面应用属性

rust 复制代码
// 在 main.rs 中配置
use dioxus_desktop::{Config, WindowBuilder};
use dioxus::prelude::*;

fn main() {
    dioxus_desktop::launch_cfg(
        app,
        Config::new()
            .with_window(
                WindowBuilder::new()
                    .with_title("我的应用")
                    .with_inner_size(dioxus_desktop::LogicalSize::new(1024.0, 768.0))
                    .with_resizable(true)
                    .with_maximized(false)
                    .with_decorations(true)
                    .with_transparent(false)
            )
            .with_custom_head(
                r#"
                <style>
                    body { margin: 0; padding: 0; }
                </style>
                "#
                .to_string(),
            )
    );
}

6.2 添加图标和资源

powershell 复制代码
# 项目结构
my-desktop-app/
├── assets/
│   ├── icons/
│   │   ├── icon.ico     # Windows 图标
│   │   ├── icon.png
│   │   └── icon.icns    # macOS 图标
│   ├── images/
│   └── fonts/
└── ...

# 在代码中使用资源
fn app(cx: Scope) -> Element {
    cx.render(rsx! {
        div {
            img {
                src: "assets/images/logo.png",
                alt: "Logo"
            }
        }
    })
}

6.3 系统托盘和菜单

rust 复制代码
use dioxus::prelude::*;
use dioxus_desktop::{Config, Menu, MenuItem, WindowBuilder, LogicalSize};
use dioxus_desktop::tao::menu::MenuBar;

fn main() {
    // 创建菜单
    let menu = Menu::new()
        .add_submenu("文件", Menu::new()
            .add_item(MenuItem::CloseWindow)
            .add_separator()
            .add_item(MenuItem::Quit))
        .add_submenu("编辑", Menu::new()
            .add_item(MenuItem::Undo)
            .add_item(MenuItem::Redo)
            .add_separator()
            .add_item(MenuItem::Cut)
            .add_item(MenuItem::Copy)
            .add_item(MenuItem::Paste));

    dioxus_desktop::launch_cfg(
        app,
        Config::new()
            .with_window(
                WindowBuilder::new()
                    .with_title("带有菜单的应用")
                    .with_inner_size(LogicalSize::new(800.0, 600.0))
            )
            .with_menu(menu)
    );
}

七、项目模板和示例

7.1 使用官方模板

powershell 复制代码
# 创建带有路由的项目
dx new my-app-with-router --template router

# 创建带有状态管理的项目
dx new my-app-with-state --template state

# 查看所有可用模板
dx new --help

7.2 社区模板

powershell 复制代码
# 从 GitHub 模板创建
git clone https://github.com/DioxusLabs/example-projects.git
cd example-projects/desktop-app
cargo run

八、调试和测试

8.1 调试配置

json 复制代码
// .vscode/launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Desktop App",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}/target/debug/my-desktop-app.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console": "integratedTerminal"
        }
    ]
}

8.2 日志配置

rust 复制代码
use dioxus::prelude::*;
use dioxus_desktop::Config;
use log::LevelFilter;

fn main() {
    // 初始化日志
    env_logger::builder()
        .filter_level(LevelFilter::Debug)
        .init();
    
    dioxus_desktop::launch_cfg(app, Config::new());
}

九、性能监控

9.1 添加性能监控

rust 复制代码
use dioxus::prelude::*;
use dioxus_desktop::Config;
use std::time::Instant;

fn app(cx: Scope) -> Element {
    let start_time = use_state(cx, || Instant::now());
    
    // 记录渲染时间
    let elapsed = start_time.elapsed().as_millis();
    
    cx.render(rsx! {
        div {
            p { "渲染时间: {elapsed}ms" }
            // 你的组件...
        }
    })
}

十、打包发布清单

发布检查表

  • 代码优化完成
  • 测试通过
  • 图标和资源文件就位
  • 版本号已更新
  • 配置文件已检查
  • 依赖已锁定
  • 构建脚本已测试
  • 安装包测试通过

发布命令

powershell 复制代码
# 清理旧构建
cargo clean

# 发布构建
cargo build --release

# 创建安装包
# 使用 Inno Setup 或 WiX

关联知识

【知识科普】.toml配置文件说明
【Rust编程知识】在 Windows 下搭建完整的 Rust 开发环境
【开发语言】Rust语言介绍

总结

安装验证清单

  1. ✅ Rust 工具链安装成功
  2. ✅ Windows SDK 和 MSVC 安装成功
  3. ✅ Dioxus CLI 安装成功
  4. ✅ 示例项目运行成功
  5. ✅ 开发工具配置完成
  6. ✅ 构建发布流程测试通过

下一步学习建议

  1. 学习 Dioxus 组件系统
  2. 掌握状态管理
  3. 学习路由配置
  4. 了解与后端交互
  5. 探索高级桌面功能(系统托盘、通知等)

获取帮助

相关推荐
lucky67072 小时前
Windows 上彻底卸载 Node.js
windows·node.js
编程小白20262 小时前
从 C++ 基础到效率翻倍:Qt 开发环境搭建与Windows 神级快捷键指南
开发语言·c++·windows·qt·学习
凯子坚持 c4 小时前
CANN 性能剖析实战:从原始事件到交互式火焰图
windows·microsoft
开开心心就好4 小时前
发票合并打印工具,多页布局设置实时预览
linux·运维·服务器·windows·pdf·harmonyos·1024程序员节
獨枭5 小时前
PyCharm 跑通 SAM 全流程实战
windows
仙剑魔尊重楼5 小时前
音乐制作电子软件FL Studio2025.2.4.5242中文版新功能介绍
windows·音频·录屏·音乐·fl studio
PHP小志6 小时前
Windows 服务器怎么修改密码和用户名?账户被系统锁定如何解锁
windows
专注VB编程开发20年7 小时前
vb.net datatable新增数据时改用数组缓存
java·linux·windows
仙剑魔尊重楼7 小时前
专业音乐制作软件fl Studio 2025.2.4.5242中文版新功能
windows·音乐·fl studio
rjc_lihui9 小时前
Windows 运程共享linux系统的方法
windows