Toast -Benachrichtigungsaktion öffnet eine neue Instanz meiner Winui -App, anstatt eine bestimmte Logik auszuführenC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Toast -Benachrichtigungsaktion öffnet eine neue Instanz meiner Winui -App, anstatt eine bestimmte Logik auszuführen

Post by Anonymous »

Diese App wurde mit Winui 3 < /p>
gemacht. Lassen Sie mich also ein bisschen meine App erklären. Free (
https://apps.microsoft.com/detail/9nblg ... n-us&gl=us)

In meinem MainviewModel, ich werde ein Grundstück erfassen, und ich werde es benutzen, um es zu verwenden. an anderer Stelle. < /p>

Code: Select all

public partial class MainViewModel : ObservableObject {

[ObservableProperty]

string _fileName = string.Empty;

#region variables and constants

#region Relay commands

[RelayCommand]

async Task SaveToSTLAsync() {

// Create the dialog

var fileNameDialog = new ContentDialog {

Title = "Save Design",

Content = new StackPanel {

Children =

{

new TextBlock { Text = "Enter a name for your design:" },

new TextBox { PlaceholderText = "MyDesign", Name = "FileNameTextBox" }

}

},

PrimaryButtonText = "Save",

CloseButtonText = "Cancel",

XamlRoot = _rootContainer?.XamlRoot

};

// Show the dialog

var result = await fileNameDialog.ShowAsync();

if(result == ContentDialogResult.Primary) {

// Retrieve the TextBox value

var stackPanel = (StackPanel)fileNameDialog.Content;

var textBox = stackPanel.Children.OfType().First();

var fileName = textBox.Text;

// Validate the file name

if(string.IsNullOrWhiteSpace(fileName)) {

// Use a default file name if none is provided

fileName = "MyDesign.stl";

}

else if(!fileName.EndsWith(".stl", StringComparison.OrdinalIgnoreCase)) {

// Append the ".stl" extension if it's missing

fileName += ".stl";

}

FileName = fileName;

GetLineData(); // Ensure LineData is updated

Toast(fileName);

ExportSTL(fileName, 5.0);

}

}
< /code>
Ich habe auch eine Methode, die einen Toast anzeigt, um die Datei zu öffnen < /p>

private static void Toast(string fileName) {

// Define the toast content with a button

string toastXmlString =

"" +

"" +

"" +

$"Success" +

$"'{fileName}' saved successfully." +

"" +

"" +

"" +

"" +

"" +

"";

// Create an XML document for the toast content

XmlDocument toastXml = new();

toastXml.LoadXml(toastXmlString);

// Create and display the toast notification

ToastNotification toast = new(toastXml);

ToastNotificationManager.CreateToastNotifier().Show(toast);

}

< /code>
Da ich die Datei nicht öffnen kann, habe ich mehrere Methoden gekreuzt, die ausgeführt werden sollen, wenn die Schaltfläche des Toasts auf < /p>
App.aml.cs geklickt wird. Hier versuche ich, den 3D -Viewer herunterzuladen, einen Dialogfeld anzuzeigen und die Datei zu öffnen.  Wenn ich jedoch auf die TOST -Schaltfläche klicke, wird es eine andere Instanz meiner App < /p>
öffnen

protected override void OnLaunched(LaunchActivatedEventArgs args) {

ArgumentNullException.ThrowIfNull(args);

if(!string.IsNullOrEmpty(args.Arguments) && args.Arguments == "openSTL") {

OpenFileIn3DViewer();

}

var window = Services!.GetRequiredService();

window.Activate();

var hwnd = WindowNative.GetWindowHandle(window);

var windowId = Win32Interop.GetWindowIdFromWindow(hwnd);

var appWindow = AppWindow.GetFromWindowId(windowId);

var presenter = appWindow.Presenter as OverlappedPresenter;

presenter?.Maximize();

}

private async void OpenFileIn3DViewer() {

var mainViewModel = Services!.GetRequiredService();

var filePath = Path.Combine(

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),

NANOFLOWFOLDER, mainViewModel.FileName);

if(!File.Exists(filePath)) {

Debug.WriteLine($"The file {filePath} does not exist.");

return;

}

if(Is3DViewerInstalled()) {

// Open the STL file in 3D Viewer

OpenSTLFile(filePath);

}

else {

// Redirect the user to install 3D Viewer and show progress dialog

await ShowProgressDialogAsync();

Open3DViewerStorePage();

}

}

private static void Open3DViewerStorePage() {

var storeUri = new Uri("ms-windows-store://pdp/?ProductId=9NBLGGH42THS"); // 3D Viewer Store link

var processStartInfo = new ProcessStartInfo {

FileName = storeUri.ToString(),

UseShellExecute = true

};

Process.Start(processStartInfo);

}

private static async Task ShowProgressDialogAsync() {

var progressDialog = new ContentDialog {

Title = "Installing 3D Viewer",

Content = new ProgressBar {

IsIndeterminate = true,

Width = 300

},

CloseButtonText = "Cancel",

XamlRoot = Services!.GetRequiredService().Content.XamlRoot

};

await progressDialog.ShowAsync();

}

private static void OpenSTLFile(string filePath) {

var processStartInfo = new ProcessStartInfo {

FileName = filePath,

UseShellExecute = true

};

Process.Start(processStartInfo);

}

private static bool Is3DViewerInstalled() {

var packageFamilyName = "Microsoft.Microsoft3DViewer_8wekyb3d8bbwe"; // 3D Viewer package family name

var packageManager = new Windows.Management.Deployment.PackageManager();

var packages = packageManager.FindPackagesForUser(string.Empty);

return packages.Any(p => p.Id.FamilyName == packageFamilyName);

}

}
< /code>
Also, mein [url=viewtopic.php?t=15738]Problem[/url] ist, dass wenn ich auf den Toast klicke, eine neue Instanz meiner App gestartet wird und es nicht so ist, was ich es gesagt habe. sind freundlicher), aber wenn ich die Frage beschreibt, sagt es mir, dass ich nicht autorisiert bin (ich dies hat Probleme), ich hoffe, das [url=viewtopic.php?t=15738]Problem[/url] ist bald, weil ich den ehrlichen Stapelüberlauf sehr giftig ist. 3D -Viewer
 [b] Update [/b]  
Ich verwende jetzt 
new ToastContentBuilder()
.AddText("Success")
.AddText($"'{fileName}' saved successfully.")
.AddButton(new ToastButton()
.SetContent("Open File")
.AddArgument("action", "openSTL")
.SetBackgroundActivation())
.Show();
und wenn ich die Taste drücke, löst die Einleitungen nicht aus, sondern löst das erste Mal die App aus, und wenn ich die Taste drücke, kann ich "Toast Benachrichtigung mit Action = OpenStl gesendet", aber nicht auf den Breakpoint

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post