调试环境正常,发布时统一发布前后台
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
@@ -42,8 +43,25 @@ namespace WebView2Demo
|
||||
// 4. 监听消息
|
||||
webView.WebMessageReceived += WebView_WebMessageReceived;
|
||||
|
||||
// 5. 初始化完成后再导航
|
||||
webView.Source = new Uri("http://localhost:5174");
|
||||
// 5. [合并发布配置] 配置本地虚拟映射加载静态资源
|
||||
// 这样发布后即便没有启动 Vue 开发服务器,也可以通过本地文件运行
|
||||
string localFolder = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot");
|
||||
if (Directory.Exists(localFolder) && File.Exists(System.IO.Path.Combine(localFolder, "index.html")))
|
||||
{
|
||||
// 设置虚拟域名映射到本地文件夹
|
||||
webView.CoreWebView2.SetVirtualHostNameToFolderMapping(
|
||||
"app.local",
|
||||
localFolder,
|
||||
CoreWebView2HostResourceAccessKind.Allow);
|
||||
|
||||
// 导航到虚拟域名
|
||||
webView.Source = new Uri("https://app.local/index.html");
|
||||
}
|
||||
else
|
||||
{
|
||||
// 开发模式:如果本地没有 wwwroot,则尝试加载 Vue 开发服务器地址
|
||||
webView.Source = new Uri("http://localhost:5174");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
@@ -12,4 +12,11 @@
|
||||
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1518.46" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- 将 wwwroot 文件夹及其内容包含在输出中 -->
|
||||
<ItemGroup>
|
||||
<Content Include="wwwroot\**">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
|
||||
:root{--lightningcss-light:initial;--lightningcss-dark: ;color-scheme:light dark;color:#ffffffde;font-synthesis:none;text-rendering:optimizelegibility;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;background-color:#242424;font-family:system-ui,Avenir,Helvetica,Arial,sans-serif;font-weight:400;line-height:1.5}@media (prefers-color-scheme:dark){:root{--lightningcss-light: ;--lightningcss-dark:initial}}a{color:#646cff;-webkit-text-decoration:inherit;text-decoration:inherit;font-weight:500}a:hover{color:#535bf2}body{place-items:center;min-width:320px;min-height:100vh;margin:0;display:flex}h1{font-size:3.2em;line-height:1.1}button{cursor:pointer;background-color:#1a1a1a;border:1px solid #0000;border-radius:8px;padding:.6em 1.2em;font-family:inherit;font-size:1em;font-weight:500;transition:border-color .25s}button:hover{border-color:#646cff}button:focus,button:focus-visible{outline:4px auto -webkit-focus-ring-color}.card{padding:2em}#app{text-align:center;max-width:1280px;margin:0 auto;padding:2rem}@media (prefers-color-scheme:light){:root{color:#213547;background-color:#fff}a:hover{color:#747bff}button{background-color:#f9f9f9}}.container[data-v-d9e54285]{padding:20px;font-family:Segoe UI,Tahoma,Geneva,Verdana,sans-serif}.card[data-v-d9e54285]{background:#fff;border:1px solid #eee;border-radius:8px;margin-bottom:15px;padding:15px;box-shadow:0 2px 4px #0000000d}.btn-group[data-v-d9e54285]{flex-wrap:wrap;gap:10px;margin-top:10px;display:flex}button[data-v-d9e54285]{color:#fff;cursor:pointer;background:#42b883;border:none;border-radius:4px;padding:8px 16px}button[data-v-d9e54285]:hover{background:#3aa876}.btn-warning[data-v-d9e54285]{background:#f39c12}.btn-info[data-v-d9e54285]{background:#3498db}.result-box[data-v-d9e54285]{background:#f8f9fa;border-left:4px solid #42b883;margin-top:15px;padding:10px;font-size:.9em}
|
||||
@@ -0,0 +1,14 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="./vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>web-app</title>
|
||||
<script type="module" crossorigin src="./assets/index-DJlihbf1.js"></script>
|
||||
<link rel="stylesheet" crossorigin href="./assets/index-onfabBEH.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
||||
|
After Width: | Height: | Size: 1.5 KiB |
+36
@@ -0,0 +1,36 @@
|
||||
# 1. Path Configuration
|
||||
$rootPath = Get-Location
|
||||
$vuePath = Join-Path $rootPath "web-app"
|
||||
$wpfPath = Join-Path $rootPath "WebView2Demo"
|
||||
$publishPath = Join-Path $rootPath "Publish"
|
||||
|
||||
# 2. Cleanup old publish folder
|
||||
Write-Host ">>> Cleaning old publish files..." -ForegroundColor Cyan
|
||||
if (Test-Path $publishPath) { Remove-Item -Path $publishPath -Recurse -Force }
|
||||
New-Item -ItemType Directory -Path $publishPath | Out-Null
|
||||
|
||||
# 3. Build Vue Frontend
|
||||
Write-Host ">>> Building Vue Frontend..." -ForegroundColor Cyan
|
||||
Set-Location $vuePath
|
||||
npm run build
|
||||
if ($LASTEXITCODE -ne 0) { Write-Host "Vue build failed!" -ForegroundColor Red; exit }
|
||||
|
||||
# 4. Copy Vue assets to WPF directory
|
||||
Write-Host ">>> Syncing frontend assets to WPF project..." -ForegroundColor Cyan
|
||||
$vueDist = Join-Path $vuePath "dist"
|
||||
$wpfWwwroot = Join-Path $wpfPath "wwwroot"
|
||||
|
||||
if (Test-Path $wpfWwwroot) { Remove-Item -Path $wpfWwwroot -Recurse -Force }
|
||||
New-Item -ItemType Directory -Path $wpfWwwroot | Out-Null
|
||||
Copy-Item -Path "$vueDist\*" -Destination $wpfWwwroot -Recurse -Force
|
||||
|
||||
# 5. Build and Publish WPF Backend
|
||||
Write-Host ">>> Publishing WPF project..." -ForegroundColor Cyan
|
||||
Set-Location $wpfPath
|
||||
dotnet publish -c Release -o $publishPath
|
||||
if ($LASTEXITCODE -ne 0) { Write-Host "WPF publish failed!" -ForegroundColor Red; exit }
|
||||
|
||||
Write-Host "`n>>> Publish complete! Output directory: $publishPath" -ForegroundColor Green
|
||||
Write-Host ">>> You can run $publishPath\WebView2Demo.exe to start the application." -ForegroundColor Green
|
||||
|
||||
Set-Location $rootPath
|
||||
@@ -4,4 +4,5 @@ import vue from '@vitejs/plugin-vue'
|
||||
// https://vite.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [vue()],
|
||||
base: './', // 关键:使用相对路径,确保在本地文件协议下能正确加载资源
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user