Die Winui 3-App läuft mit F5 einwandfrei, kann jedoch nicht als einzelne .exe-Datei bereitgestellt werdenC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Die Winui 3-App läuft mit F5 einwandfrei, kann jedoch nicht als einzelne .exe-Datei bereitgestellt werden

Post by Anonymous »

Mit Hilfe von GPT habe ich es geschafft, eine WinUI 3-Anwendung zu erstellen, es ist überhaupt nicht viel: 2 Beschriftungen, 1 Eingabetext, 2 Schaltflächen und ein weiteres Textfeld, das Protokolle anzeigt. Hier ist ein Bild vom Laufen mit F5:
Image

Wie Sie sehen können, funktioniert es. Jetzt möchte ich es wieder mit Hilfe von GPT verpacken, am Ende bin ich bei der Konfiguration gelandet:

Code: Select all



WinExe
net8.0-windows10.0.19041.0
10.0.17763.0
Nidam_Benchmark
app.manifest

win-x64

x64
true
true

true

true

enable

























true




False
True
False
True
















PreserveNewest



PreserveNewest



PreserveNewest




PreserveNewest




PreserveNewest




None
true



Paketierung über diese Befehlszeile

Code: Select all

dotnet publish -c Release -r win-x64 --self-contained true
und auch

Code: Select all

dotnet publish -c Release -r win-x64 --self-contained true /p:PublishSingleFile=true
und die resultierende exe-Datei ist etwa 120 MB groß, was aussieht, als ob darin die für die Ausführung erforderliche Laufzeit gebündelt wäre, ohne dass der Benutzer etwas installieren müsste (dies ist eine Voraussetzung). Das Problem ist, dass beim Öffnen der Exe-Datei nichts passiert. überhaupt. Kann mir BITTE jemand helfen, diese Anwendung zu packen, die wie vorgesehen mit F5 läuft.
Ich kann Ihnen bei Bedarf Code oder Informationen geben, es ist eigentlich nichts, nur die XAML- und C#-Klassensache.
XAML

Code: Select all








...
C#

Code: Select all

using Microsoft.UI.Text;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

using System;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace Nidam_Benchmark
{
/// 
/// An empty window that can be used on its own or navigated to within a Frame.
/// 
public sealed partial class MainWindow : Window
{
private readonly int[] BackendPorts = { 4001, 4002, 4003, 7080, 7081 };
private string BaseDir => AppDomain.CurrentDomain.BaseDirectory;

private bool _loaded;

public MainWindow()
{
InitializeComponent();

StartButton.IsEnabled = true;
StopButton.IsEnabled = false;
//((FrameworkElement)Content).Loaded += MainWindow_Loaded;
RootGrid.Loaded += MainWindow_Loaded;

}

private async void MainWindow_Loaded(object sender, RoutedEventArgs e) {
if (_loaded) return;
_loaded = true;
StopButton.IsEnabled = await IsAnyBackendRunningAsync();
StatusText.Text = StopButton.IsEnabled ? "Backend running" : "Idle";
}

private async void StartButton_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(VuRangeTextBox.Text))
{
ContentDialog dialog = new ContentDialog {
Title = "Missing Input",
Content = "Please enter a VU range (e.g.  1,4,8,16)",
CloseButtonText = "OK",
XamlRoot = this.Content.XamlRoot
};

await dialog.ShowAsync();
return;
}

StartButton.IsEnabled = false;
StopButton.IsEnabled = false;
LogsTextBox.Text = "";
StatusText.Text = "Running benchmark...";

LaunchBenchmark(VuRangeTextBox.Text);

}

private async void StopButton_Click(object sender, RoutedEventArgs e) {
StopButton.IsEnabled = false;
StatusText.Text = "Stopping backend...";

await ShutdownBackend("stop-all-2.ps1");

StatusText.Text = "Backend Stopped";
StartButton.IsEnabled = true;
StopButton.IsEnabled = await IsAnyBackendRunningAsync();
}

private void LaunchBenchmark(string range)
{
StartProcessWithLogs(
"powershell.exe",
$"-ExecutionPolicy Bypass -File \"{Path.Combine(BaseDir, "run-benchmark.ps1")}\" \"{range}\"",
onExited: () =>
{
DispatcherQueue.TryEnqueue(() =>
{

StatusText.Text = "Benchmark finished";
StartButton.IsEnabled = true;
StopButton.IsEnabled = false;
_ = UpdateStopButtonAsync();
});
}
);
}

private async Task UpdateStopButtonAsync() {
StopButton.IsEnabled = await IsAnyBackendRunningAsync();
}

//private async Task ShutdownBackend(string script) {
//    using var process = StartProcessWithLogs("powershell.exe",
//        $"-ExecutionPolicy Bypass -File \"{Path.Combine(BaseDir, script)}\"");
//    await process.WaitForExitAsync();
//}

private async Task ShutdownBackend(string script)
{
var psi = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = $"-ExecutionPolicy Bypass -File \"{Path.Combine(BaseDir, script)}\"",
UseShellExecute = false,
WorkingDirectory = BaseDir,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};

using var process = new Process { StartInfo = psi };

process.Start();

// Read stdout
var stdoutTask = Task.Run(async () =>
{
while (!process.StandardOutput.EndOfStream)
{
var line = await process.StandardOutput.ReadLineAsync();
if (line != null)
{
DispatcherQueue.TryEnqueue(() =>
AppendLog(line + Environment.NewLine));
}
}
});

// Read stderr
var stderrTask = Task.Run(async () =>
{
while (!process.StandardError.EndOfStream)
{
var line = await process.StandardError.ReadLineAsync();
if (line != null)
{
DispatcherQueue.TryEnqueue(() =>
AppendLog("[ERR] "  + line + Environment.NewLine));
}
}
});

await Task.WhenAll(stdoutTask, stderrTask);

await process.WaitForExitAsync();
}

private Process StartProcessWithLogs(string fileName, string arguments, Action? onExited = null)
{
var psi = new ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
WorkingDirectory = BaseDir,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};

var process = new Process
{
StartInfo = psi,
EnableRaisingEvents = true
};

process.OutputDataReceived += (_, e) =>
{
if (e.Data != null)
DispatcherQueue.TryEnqueue(() => AppendLog(e.Data + Environment.NewLine));

};

process.ErrorDataReceived += (_, e) =>
{
if (e.Data != null)
DispatcherQueue.TryEnqueue(() => AppendLog("[ERR] " + e.Data + Environment.NewLine));
};

if (onExited != null)
{
process.Exited += (_, _) => DispatcherQueue.TryEnqueue(() => onExited());
}

process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();

return process;
}

private async Task IsAnyBackendRunningAsync()
{
foreach (var port in BackendPorts)
{
if (await IsPortOpenAsync(port))
return true;
}
return false;
}

private async Task IsPortOpenAsync(int port)
{
try
{
using var client = new TcpClient();
var connectTask = client.ConnectAsync("localhost", port);
var timeoutTask = Task.Delay(300); // fast timeout

return await Task.WhenAny(connectTask, timeoutTask) == connectTask;
}
catch
{
return false;
}
}

// TextBox
private void AppendLog(string text)
{
//LogsTextBox.Text += text;
//LogsTextBox.SelectionStart = LogsTextBox.Text.Length;
//LogsTextBox.SelectionLength = 0;
//LogsTextBox.Focus(FocusState.Programmatic);

var box = LogsTextBox;

// Save selection
int selStart = box.SelectionStart;
int selLength = box.SelectionLength;
bool hasSelection = selLength > 0;

// Append without losing caret state
box.Text += text;

// Restore selection
if (hasSelection)
{
box.SelectionStart = selStart;
box.SelectionLength = selLength;
}
else
{
// Auto-scroll only if user isn't selecting
box.SelectionStart = box.Text.Length;
box.SelectionLength = 0;
}
}
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post