0

I have an application that creates a log file while it's running. This log file can get quite large and in most cases should be deleted when the application closes.

I am using the MVVM format and would like to be able to use this format as well. When I try to bind a command to the "Closing" or "Closed" events I get a compile error that it can only be bound to type "EventHandler" but doesn't doing so break the MVVM format?

I have looked at other possible solutions on SO and other web sites, all of the proposed solution are old and none work (compile errors, reference errors, etc.). I am using VS2019 if that matters.

Using MVVM what is the best method to delete this file?

Athens66
  • 33
  • 6
  • You should show your code and then explain what you're trying to do. Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) and elaborate on the issue. Which technology are you even using? Is this WPF, Xamarin.Forms, .NET MAUI, ...? – Julian Jun 06 '23 at 18:41
  • If it's WPF, using `override void OnExit(ExitEventArgs e)` to trigger your file delete in the App.xaml code-behind is the route I would go. – bigcrazyal Jun 06 '23 at 19:13
  • Julian, The problem is I have no code to provide. I don't know what to write. I am writing this in C# using MVVM – Athens66 Jun 06 '23 at 19:26
  • @bigcrazyal - putting it in App.xaml, violates the MVVM format. At some point the GUI will have a major overhaul, so it's important the MVVM format is maintained. – Athens66 Jun 06 '23 at 19:28
  • @Athens66, are you able to implement `Dispose()` for your viewmodels so that you can handle something like this when you get rid of your VM? – bigcrazyal Jun 06 '23 at 19:50
  • You can still follow the MVVM *pattern* and do things in the code-behind. Why can't you provide any code? Without a minimal reproducible example, your question is difficult to answer and will likely get closed. – Julian Jun 07 '23 at 08:27

1 Answers1

0

You are getting the error you mentioned because you're trying to bind a command to an event ("Closing" and "Closed" are both events) which, instead, expects an event handler.

There are ways, as shown in this thread, to declare an event handler that invokes a command entirely in XAML using bindings and this would be the best solution in order to adhere to the MVVM pattern.

Another solution involves handling events such as Closed or Closing directly in your code-behind and invoke from there your ViewModel's commands. This is somehow simpler but requires your View to have a tight dependency from your ViewModel so it is not the best solution in terms of MVVM compliance. You can, however, loose this dependency by using interfaces if you want to strictly adhere to MVVM.

Here's an example


// MyApp\ViewModel\MyViewModel.cs
public class MyViewModel : IClose
{
   public ICommand CloseCommand { get; }

   // ...
}

// MyApp\Abstractions\IClose.cs
public interface IClose
{
   ICommand CloseCommand { get; }
}

// MyApp\View\MyView.xaml.cs
public partial class MyView
{
   public MyView()
   {
      InitializeComponent();
   }

   protected void OnClosing(object sender, EventArgs e)
   {
      var close = this.BindingContext as IClose;

      if (close is null) return;

      close.CloseCommand.Execute(null);
   }
}

With MyApp\View\MyView.xaml being

<Window x:Class="MyApp.View.MyView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:vm="clr-namespace:MyApp.ViewModel"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Closing="OnClosing">
   <Window.DataContext>
      <vm:MyViewModel/>
   </Window.DataContext>
</Window>
canta2899
  • 394
  • 1
  • 7