eljamba
eljamba

Reputation: 385

.NET MAUI: how to show display alert in a class library

I'm trying to develop a .NET MAUI class library. During an event, if an exception raises a display alert window should be visualized. In a .NET MAUI project this works fine, but in .NET MAUI class library project there is something missing, maybe I should add a reference. Could someone provide a solution?

catch (Exception ex)
{
       await DisplayAlert("EXCEPTION RAISED", ex.ToString(), "CANCEL");
}

Upvotes: 1

Views: 1332

Answers (1)

H.A.H.
H.A.H.

Reputation: 3917

This is really bad idea. For more than one reason.

  1. Tomorrow when you try to reuse this library in a thread, that has no interface, what will you do?

  2. How will you know the consumer of your library will know what exactly went wrong? I've dealt in the past with libs that basically return true/false, and hide the exception. Go figure out.

  3. If you decide to make changes and fix it at some point, everyone that is using the old version of your library will have really bad time to edit every line he uses your library.

To answer your question. You can pass any object as parameter to any function. Or you can do this as initialization. If you still want to do it.

As alternative, I sometimes use messaging service. If the elements are de-coupled and I don't want them to be coupled. The code that does work broadcasts messages, and IF (this is important) there is interface registered to receive such messages, they are handled.

Upvotes: 1

Related Questions