Reputation: 3
I'm new to WPF
and Prism
. How do I use an if else statement when using CommandCanExecute()
, CommandExecute()
/Delegate commands?
I have a code to load and get the instance of livechart. However, if the file for the chart does not exist in the users desktop, my application will crash. I want to implement a if else statement to say if you cannot find the graph, show up a message box notifying that there is an error, instead of crashing the program.
I tried searching up on RaiseCanExecuteChanged but unsure how to implement.
private bool BEYieldCommandCanExecute()
{
return true;
}
private void BEYieldCommandExecute()
{
if (true)
{
_eventAggregator.GetEvent<GraphPubSubEvent>().Publish(_viewName);
}
else
{//Check
MessageBox.Show("Error loading. Please ensure Excel file/Server file exist in Desktop/Server to generate Chart.", "Invalid Request", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}
Thanks alot!!
Upvotes: 0
Views: 936
Reputation: 486
You shouldn't do this in the command it self. When you bind the command to a button, the button shall not be enabled when you can't perform the task.
bool CanExecute()
{
return File.Exists("some/path/to/file.ext");
}
Additionally, you should add a message to the UI (e.g. next to the button, or as a tool-tip) which is only visible if the button is disabled.
Upvotes: 0
Reputation: 167
You should be more precise. I dont know exacly what you mean. If you have this chart in some file do it like that:
Add this reference on top:
using System.IO;
then in your code check if file exist:
if (!File.Exists("someFile.txt"))
{
CreateFile("someFile.txt");
}
else
{
DoSomeActionWithFile("someFile.txt")
}
Another way to do that is block try-catch
try
{
OpenFile("someFile.txt");
}
catch(Exception ex)
{
LogError(ex.toString());
DoSomethingWithInErrorCase();
}
Upvotes: 0
Reputation: 352
In XAML: <Button Command="{Binding OpenDialogCommand }" />
public class ViewModel
{
public DelegateCommand OpenDialogCommand { get; set; }
public ViewModel()
{
OpenDialogCommand = new DelegateCommand(BrowseFile);
}
private void BrowseFile()
{
var openDialog = new OpenFileDialog()
{
Title = "Choose File",
// TODO: etc v.v
};
openDialog.ShowDialog();
if (File.Exists(openDialog.FileName))
{
// TODO: Code logic is here
}
else
{
// TODO: Code logic is here
}
}
}
`
Upvotes: 0
Reputation: 1648
ICommand
implementations are meant to be built as bindable commands
DelegateCommand.RaiseCanExecuteChange() is to refresh if the button is enabled in the UI.
It is not intended to be used in logic behind the commands.
For Example:
XAML Save Button Binding <Button Command="{Binding SaveCommand}" />
ViewModel Command where the button is only enabled if the Record needs to be saved
private DelegateCommand _saveCommand;
public DelegateCommand SaveCommand =>
_saveCommand ?? (_saveCommand = new DelegateCommand(ExecuteSaveCommand, CanExecuteSaveCommand));
void ExecuteSaveCommand()
{
// Save logic goes here
}
private bool CanExecuteSaveCommand()
{
var isDirty = CurrentRecord.GetIsDirty();
return isDirty;
}
When the UI is bound the SaveCommand CanExecuteSaveCommand() method is run and let's assume the record is NOT dirty when loaded.
The trick is to wire up an event so when MyRecord is updated the UI button is enabled by calling _saveCommand.RaiseCanExecuteChanged
public MainWindowViewModel(IRecordDataService dataService)
{
CurrentRecord = dataService.GetRecord();
CurrentRecord.Updated += (sender, args) => _saveCommand.RaiseCanExecuteChanged();
}
Upvotes: 0