I am creating a general purpose Exception message dialog, and want to have a "Copy Error" button to copy the error text to the clipboard. I cannot figure out what to use for SecondaryButtonCommand = ICommand.
All my research about implementing ICommand with WinUI 3 ends up on some variation of RelayCommand and MVVM Toolkit, which I could not get to work.
I got the Copy to Clipboard to work. I now want that to happen when user clicks a button on the dialog.
Note this dialog is created in its own class in code, and is intended to be used from any page.
What do I use for SecondaryButtonCommand = ???
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using Windows.ApplicationModel.DataTransfer;
namespace JimPrototypes2.Helpers
{
public static class ErrorDialog
{
public static void Show(XamlRoot root, string message, string stackTrace)
{
string msg = message + Environment.NewLine + Environment.NewLine + stackTrace;
ContentDialog err = new ContentDialog()
{
XamlRoot = root,
Title = "An Error Has Occured",
Content = msg,
CloseButtonText = "Ok",
SecondaryButtonText = "Copy Error",
SecondaryButtonCommand = ???????
};
err.ShowAsync();
// Just testing copy to clipboard
DataPackage package = new DataPackage();
package.SetText(msg);
Clipboard.SetContent(package);
}
}
}
Eventually came across an ICommand example that helped. Turns out it is REALLY easy once I saw a simple, functional example.
Here is my ICommand named CopyCommand
My updated ErrorDialog class
And using it in a try catch block in a BackgroundWorker thread