We are writing a C# application in Visual Studio using WPF
and MVVM
.
In our View
, we write interfaces in XAML
, and then bind the GUI properties to functions in the ViewModel
.
Here is a relevant snippet of the View
code:
<StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Width="124" HorizontalAlignment="Left" Content="New Exercise" Command="{Binding OnNewExercise }" IsEnabled="{Binding IsLoadEnabled }" />
<Button Width="124" HorizontalAlignment="Left" Content="Load Exercise" Command="{Binding OnLoadExercise }" IsEnabled="{Binding IsLoadEnabled }" />
<Button Width="124" HorizontalAlignment="Left" Content="Save Exercise" Command="{Binding OnSaveExercise }" IsEnabled="{Binding IsSaveEnabled }" />
<Button Width="124" HorizontalAlignment="Left" Content="Checkpoint" Command="{Binding OnMakeCheckpoint }" IsEnabled="{Binding IsCheckpointEnabled }" />
</StackPanel>
The Bindings
shown above are all public functions in the ViewModel
.
Here is that snippet:
//Button bindings. These functions are called on button clicks.
public ICommand OnNewExercise { get { return _dashboardButtons.Exercise.NewCommand; } }
public ICommand OnLoadExercise { get { return _dashboardButtons.Exercise.LoadCommand; } }
public ICommand OnSaveExercise { get { return _dashboardButtons.Exercise.SaveCommand; } }
public ICommand OnMakeCheckpoint { get { return _dashboardButtons.Checkpoint.MakeCommand; } }
public ICommand OnLoadCheckpoint { get { return _dashboardButtons.Checkpoint.LoadCommand; } }
Problem:
When someone renames a function in the ViewModel
,
the View
still builds, and it causes our interfaces to stop working.
How can I guarantee at compile time that the bindings in the View
match the existing functions in the corresponding ViewModel
? It would be great if the View.xaml
was able to cause a compilation failure.