Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Background: This is code lifted from an application i made for a company to automate a bunch of calculations and device selections based on a bunch of data regarding a building. I'll be using the surface area of the building as an example.
- I've tried to implement the MVVM model as best i could (explanations on it are kinda vague) so we have:
- A XAML page with say, a textfield and a button: (Note, this is from a page called Offertetool.XAML, see below)
- <TextBox Text="{Binding surface, UpdateSourceTrigger=PropertyChanged}"/>
- <Button Content="Calculate" Command="{Binding CalculateClick}"/>
- A class that uses the data:
- public class Building
- {
- public double surface; //m^2
- public double twiceSurface;
- public void CalculateDoubleSurfaceArea()
- {
- twiceSurface = surface*2;
- }
- }
- A class that does nothing but receive input from the UI and do preparations for processing, in this case ProjectHandler:
- internal class ProjectHandler : ObservableObject
- {
- Building thisBuilding = new Building();
- public string surface
- {
- get { return thisBuilding.surface.ToString(); }
- set
- {
- if (Double.TryParse(value, out double surfaceParse))
- {
- thisBuilding.surface = surfaceParse;
- OnPropertyChanged("oppervlakte");
- }
- }
- }
- public ICommand CalculateClick
- {
- get { return new DelegateCommand(CalculateStuff); }
- }
- public void CalculateStuff{
- thisBuilding.CalculateDoubleSurfaceArea();
- }
- }
- The ProjectHandler class implements ObservableObject, this is where my copy and pasting came in. I've lifted this class straight from an MVVM tutorial, it basically sets a class up to be able to use the events called by the XAML.
- public abstract class ObservableObject : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- protected void OnPropertyChanged(string propertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- }
- The button clicker event uses something called DelegateCommand, again, this is copy pasted:
- class DelegateCommand : ICommand
- {
- private readonly Action action;
- public DelegateCommand(Action _action)
- {
- action = _action;
- }
- public void Execute(object parameter)
- {
- action();
- }
- public bool CanExecute(object parameter) //Dit mogelijk nog aanpassen want nu is geen enkele knop ooit greyed out.
- {
- return true;
- }
- //#pragma warning disable 67
- public event EventHandler CanExecuteChanged;
- //#pragma warning restore 67
- }
- In order for the above to work, we need to point Offertetool.XAML.cs to the right datacontext, so:
- public partial class Offertetool : Page
- {
- public Offertetool()
- {
- InitializeComponent();
- App thisApp = (App)Application.Current;
- this.DataContext = thisApp.projectHandler; //<-- in App.XAML.cs the instance of ProjectHandler is created.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement