I amd trying implement Relay Commands using Microsoft MVVM Community Toolkit. If I place the command code in the ViewModel it works. If I try to put the Command code in its own folder the command code cannot be found. I have created a very simple example below soowing the command code in the ViewModel (commented out) and my attempt to place the command ccode in its own folder. I suspect my implementation of the code in the folder is all wrong. I have found no examples of the command code in its own folder using Relay commands. I can place command code in its own folder just using ICommand & a CommandBase.
App.xaml.cs
using RelayCommandLocation.Views;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace RelayCommandLocation
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
RelayCommandView relayCommandView = new RelayCommandView();
relayCommandView.DataContext = new RelayCommandViewModel();
relayCommandView.Show();
}
}
}
ViewModel in Folder ViewModels
using Microsoft.Toolkit.Mvvm.Input;
using RelayCommandLocation.Commands;
using System.Windows.Input;
namespace RelayCommandLocation.ViewModels
{
public class RelayCommandViewModel : ObservableObject
{
public ICommand PushMeClick { get; set; }
// ################################################################################
// ############# This fails to compile = cannot find PushMeCommand code
public RelayCommandViewModel()
{
PushMeClick = new RelayCommand(PushMeCommandX.PushMeCommand);
}
// ********************************************************************************
// *********** This works. The RelayCommand code is in the ViewModel. *************
//public RelayCommandViewModel()
//{
// PushMeClick = new RelayCommand(PushMeCommand);
//}
//private void PushMeCommand()
//{
// MessageBox.Show($"Button pushed");
//}
}
}
Command Code in folder Commands
using System;
using System.Windows;
using System.Windows.Input;
namespace RelayCommandLocation.Commands
{
public class PushMeCommandX
{
public void PushMeCommand()
{
MessageBox.Show($"Button pushed");
}
}
}
XAML in folder Views
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RelayCommandLocation.Views"
mc:Ignorable="d"
Title="RelayCommandView" Height="450" Width="800">
<Grid>
<Button Content="Push Me" Command="{Binding PushMeClick}" Height="50" Width="75" />
</Grid>
</Window>
Is it possible to place the Relay Command code in its own folder?
CodePudding user response:
From the comment of @Klaus Gutter, you need to create an instance of the PushMeCommandX
in your RelayCommandViewModel
using Microsoft.Toolkit.Mvvm.Input;
using RelayCommandLocation.Commands;
using System.Windows.Input;
namespace RelayCommandLocation.ViewModels
{
public class RelayCommandViewModel : ObservableObject
{
public ICommand PushMeClick { get; set; }
// Create property or a field for your PushMeCommandX class.
public PushMeCommandX PushMe {get; set;}
public RelayCommandViewModel()
{
// Create an instance of the PushMeCommandX class to get access to it's methods.
PushMe = new PushMeCommandX();
PushMeClick = new RelayCommand(PushMe.PushMeCommand);
}
}
}
I will side with @Sir Rufo to read some Tutorial about C# classes.