title: How to DisplayAlert in a .NET MAUI ViewModel
body: <p>I went through the &quot;<a href="https://docs.microsoft.com/en-us/learn/paths/build-apps-with-dotnet-maui/" rel="nofollow noreferrer">Build mobile and desktop apps with .NET MAUI</a>&quot; path on Microsoft Learn. Now that I have a simple working MAUI app, I'm trying to make it MVVM using <code>CommunityToolkit.MVVM</code>.</p>
  <p>The course has a click event for called <code>OnCall</code> which looks like this</p>
  <pre class="lang-cs prettyprint-override"><code>private async void OnCall(object sender, EventArgs e)
  {
     var confirmCall = DisplayAlert(
        &quot;Dial a Number&quot;,
        $&quot;Would you like to call {translatedNumber}?&quot;,
        &quot;Yes&quot;,
        &quot;No&quot;
     );
  
     if (await confirmCall)
     {
        try
        {
           PhoneDialer.Open(translatedNumber);
        }
        catch (ArgumentNullException)
        {
           await DisplayAlert(&quot;Unable to dial&quot;, &quot;Phone number was not valid.&quot;, &quot;OK&quot;);
        }
        catch (FeatureNotSupportedException)
        {
           await DisplayAlert(&quot;Unable to dial&quot;, &quot;Phone dialing not supported.&quot;, &quot;OK&quot;);
        }
        catch (Exception)
        {
           await DisplayAlert(&quot;Unable to dial&quot;, &quot;Phone dialing failed.&quot;, &quot;OK&quot;);
        }
     }
  }
  </code></pre>
  <p>So I moved that to my ViewModel and made it a command, like this</p>
  <pre class="lang-cs prettyprint-override"><code>[ICommand]
  public async void OnCall ()
  {
     var confirmCall = DisplayAlert(
        &quot;Dial a Number&quot;,
        $&quot;Would you like to call {translatedNumber}?&quot;,
        &quot;Yes&quot;,
        &quot;No&quot;
     );
  
     if (await confirmCall)
     {
        try
        {
           PhoneDialer.Open(translatedNumber);
        }
        catch (ArgumentNullException)
        {
           await DisplayAlert(&quot;Unable to dial&quot;, &quot;Phone number was not valid.&quot;, &quot;OK&quot;);
        }
        catch (FeatureNotSupportedException)
        {
           await DisplayAlert(&quot;Unable to dial&quot;, &quot;Phone dialing not supported.&quot;, &quot;OK&quot;);
        }
        catch (Exception)
        {
           await DisplayAlert(&quot;Unable to dial&quot;, &quot;Phone dialing failed.&quot;, &quot;OK&quot;);
        }
     }
  }
  </code></pre>
  <p>My problem is how do I call <code>DisplayAlert</code> from a command in the ViewModel.</p>