在MVVM Light应用程序中实现“编辑”菜单

时间:2016-03-07 19:20:21

标签: c# wpf xaml command mvvm-light

我正在使用MVVM Light构建WPF应用程序。我在窗口中添加了一个菜单,其中包含标准的文件和编辑菜单:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tp1;

/**
 *
 * @author Nelson
 */
public class Vehicle {

/** Variáveis da classe, têm como função **/
 private String registration;

private int registrationYear;

private double consumption;

private double autonomy;

private int cilinderCapacity;

/**
* Final variables. They are final because they do not suffer any kind of modification during the project.
* YEAR_OMISSION is 2016 because the currect year is 2016. 
* ENVIRONMENTAL_CHARGE_OMISSION is 0.10(10 cents), gave this value because there is nothing to mention the
especific value, hence why I gave 0.10.
* RATING_RECENT = Is a string, just has the text "RECENT" inside.
* RATING_COMTEMPORY - Another string, just with the "Comtempory" text inside.
* RATING_CLASSIC - Yet again another string, with the "Classic" text.
* AGE_RECENT - It is to help to compare if a vehicle is recent or not, it has the value 3.
* AGE_CLASSIC - It is to again help to compare, value is 20.
*/

private static final int YEAR_OMISSION = 2016;
private static final double ENVIRONMENTAL_CHARGE_OMISSION=0.10;
private static final String RATING_RECENT="Recent";
private static final String RATING_CONTEMPORY="Contempory";
private static final String RATING_CLASSIC="Classic";
private static int AGE_RECENT=1;
private static final int AGE_CLASSIC=20;




/**
* Constructor of the object, it has the Registration
     * @param registration
     * @param registrationYear - The year the vehicle was first registered.
     * @param consumption - How many liters the vehicle consumes.
     * @param autonomy - How many KMs a vehicle can go without refuelling.
     * @param cilinderCapacity - How many Cubic Inches the engine has.
*/
 public Vehicle(String registration,int registrationYear, double consumption, double autonomy, int cilinderCapacity) {
 this.registration = registration;
 this.registrationYear = registrationYear;
 this.consumption = consumption;
 this.autonomy = autonomy;
 this.cilinderCapacity = cilinderCapacity;
 }

/**
* Null Constructor, it has no values, they will be attributed in the MAIN Class.
*/

 public Vehicle() {
 this.registration = "";
 this.registrationYear = 0;
 this.consumption = 0;
 this.autonomy = 0;
 this.cilinderCapacity =0;
 this.registrationYear = YEAR_OMISSION;
}
 /**
* Copy Constructor.

 */
public Vehicle(Vehicle vehicle) {
 this.registration = vehicle.getRegistration();
 this.registrationYear = vehicle.getRegistrationYear();
 this.consumption = vehicle.getConsumption();
 this.autonomy = vehicle.getAutonomy();
 this.cilinderCapacity = vehicle.getCilinderCapacity();
 }

    public String getRegistration() {
        return registration;
    }

    public int getRegistrationYear() {
        return registrationYear;
    }

    public double getConsumption() {
        return consumption;
    }

    public double getAutonomy() {
        return autonomy;
    }

    public int getCilinderCapacity() {
        return cilinderCapacity;
    }


    public double getYearRecent() {
        return AGE_RECENT;
    }

    public double getAgeRecent(){
        return AGE_RECENT;
    }



    public void setRegistration(String registration) {
        this.registration = registration;
    }

    public void setRegistrationYear(int registrationYear) {
        this.registrationYear = registrationYear;
    }

    public void setConsumption(double consumption) {
        this.consumption = consumption;
    }

    public void setAutonomy(double autonomy) {
        this.autonomy = autonomy;
    }

    public void setCilinderCapacity(int cilinderCapacity) {
        this.cilinderCapacity = cilinderCapacity;
    }

   public void setAgeRecent() {
    Vehicle.AGE_RECENT = 3; 
}



/**
 * Calculate the age of the vehicle to compare in the vehicleRating method
 * @return The year, which is 2016 minus the year the vehicle was first registered.
 */
private int calculateAge(){
    return YEAR_OMISSION-this.registrationYear;

} 

/**
 * Calculate the Circulation Tax.
 * @return Returns the value of the Environmental Charge multiplied by the Cilinder Capacity of the vehicle.
 */
    public double calculateCirculationTax(){

  return ENVIRONMENTAL_CHARGE_OMISSION*cilinderCapacity;

        }





   /**
    * Classify the vehicle based on the age.
    * If the result given by the calculateAge method is minor than the AGE_RECENT variable(3), then it will
    return "Recent"
    * If the result is between Age_RECENT and AGE_CLASSIC(20), then it will say "Contemporary"
    * If none of the IFs apply, it will return "Classic".
   **/
public static String vehicleRating(Vehicle vehicle) {
if(vehicle.calculateAge() < Vehicle.AGE_RECENT) { 
    return  Vehicle.RATING_RECENT; }
else if ((vehicle.calculateAge()>=Vehicle.AGE_RECENT)&&(vehicle.calculateAge()<=Vehicle.AGE_CLASSIC)){
    return Vehicle.RATING_CONTEMPORY;}
else 
 return Vehicle.RATING_CLASSIC;

}

    @Override
    public String toString() {
        return "Vehicle{" + "registration=" + registration + ", registrationYear=" + registrationYear + ", consumption=" + consumption + ", autonomy=" + autonomy + ", cilinderCapacity=" + cilinderCapacity + '}';
    }


}

现在,如果我运行应用程序并在其中一个<Window x:Class="ParserEditor.MainWindow" 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:ParserEditor" xmlns:ignore="http://www.galasoft.ch/ignore" xmlns:i="clr namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4" mc:Ignorable="d ignore" Closing="Window_Closing" DataContext="{Binding Main, Source={StaticResource Locator}}" Height="{Binding Height, Mode=TwoWay}" MaxHeight="{Binding MaxHeight, Mode=TwoWay}" MinHeight="{Binding MinHeight, Mode=TwoWay}" MinWidth="450" Width="450" Title="{Binding WindowTitle}"> <Window.InputBindings> <KeyBinding Command="{Binding NewParserCommand}" Gesture="CTRL+N" /> <KeyBinding Command="{Binding OpenParserCommand}" Gesture="CTRL+O" /> <KeyBinding Command="{Binding SaveParserCommand}" Gesture="CTRL+S" /> <KeyBinding Command="{Binding SaveParserAsCommand}" Gesture="ALT+A" /> <KeyBinding Command="{Binding ExitCommand}" Gesture="ALT+F4" /> <KeyBinding Command="{Binding ApplicationCommands.Undo}" Gesture="CTRL+Z" /> <KeyBinding Command="{Binding ApplicationCommands.Redo}" Gesture="CTRL+Y" /> <KeyBinding Command="{Binding ApplicationCommands.Cut}" Gesture="CTRL+X" /> <KeyBinding Command="{Binding ApplicationCommands.Copy}" Gesture="CTRL+C" /> <KeyBinding Command="{Binding ApplicationCommands.Paste}" Gesture="CTRL+V" /> <KeyBinding Command="{Binding ApplicationCommands.Delete}" Gesture="DEL" /> <KeyBinding Command="{Binding ApplicationCommands.SelectAll}" Gesture="CTRL+A" /> </Window.InputBindings> <DockPanel Name="Dock"> <Menu IsMainMenu="true" DockPanel.Dock="Top"> <MenuItem Header="_File"> <MenuItem Header="_New..." InputGestureText="Ctrl-N" Command="{Binding NewParserCommand}" /> <MenuItem Header="_Open..." InputGestureText="Ctrl-O" Command="{Binding OpenParserCommand}" /> <MenuItem Header="_Save..." InputGestureText="Ctrl-S" Command="{Binding SaveParserCommand}" /> <MenuItem Header="Save _As..." InputGestureText="Alt-A" Command="{Binding SaveParserAsCommand}" /> <Separator /> <MenuItem Header="E_xit" InputGestureText="Alt-F4" Command="{Binding ExitCommand}" /> </MenuItem> <MenuItem Header="_Edit"> <MenuItem Header="Undo" InputGestureText="Ctrl-Z" /> <MenuItem Header="Redo" InputGestureText="Ctrl-Y" /> <Separator/> <MenuItem Header="Cut" InputGestureText="Ctrl-X" /> <MenuItem Header="Copy" InputGestureText="Ctrl-C" /> <MenuItem Header="Paste" InputGestureText="Ctrl-V" /> <MenuItem Header="Delete" InputGestureText="Del" /> <MenuItem Header="Select All" InputGestureText="Ctrl-A" /> </MenuItem> </Menu> <Grid x:Name="LayoutRoot"> <!-- Some controls, including TextBlocks & TextBoxes here --> </Grid> </DockPanel> </Window> 控件中进行一些编辑,我可以使用常用的键盘快捷键来剪切,复制和放大。粘贴文本,撤消&amp;重点是重做动作是控件。但是,如果我点击我的编辑菜单并使用我的一个选项,那么没有任何反应。

如何让菜单项生效?

1 个答案:

答案 0 :(得分:0)

我想出来了。在发布问题&amp;在查看XAML时,我注意到Edit菜单项没有绑定任何命令。我将所需的Command="ApplicationCommands.xxx"语句添加到XAML&amp;现在编辑菜单工作。