在父母之后WPF MenuItem.Click中断

时间:2011-04-08 14:17:53

标签: wpf events click menuitem

我正在尝试使用各种MenuItems(都是在代码中创建)动态填充Menu控件(它驻留在ControlTemplate中),但我遇到了一些奇怪的问题。如果MenuItems都是作为根项创建的,则它们会成功触发Click事件。但是,只要任何MenuItem成为另一个MenuItem的子节点,父节点和子节点的Click事件就会停止触发。

XAML:

<ControlTemplate>
    <Menu x:Name="MyMenu"/>
</ControlTemplate>

C#:

// This is in the control that the above ControlTemplate is created for
public override void OnApplyTemplate()
{
    base.OnApplyTemplate();

    MenuItem L1 = new MenuItem() { Header = "L1" };
    MyMenu.Items.Add(L1); // Add as root
    L1.Click += new RoutedEventHandler(delegate { MessageBox.Show("L1 Click"); });

    MenuItem L2 = new MenuItem() { Header = "L2" };
    L1.Items.Add(L2); // Add as child of L1.
    // Note: If this is a child of MyMenu, both MenuItems work as expected
    L2.Click += new RoutedEventHandler(delegate { MessageBox.Show("L2 Click"); });
}

菜单显示正确,但子项上的任何点击事件都不会触发。如果我在XAML中预先定义菜单并在XAML中设置所有Click事件,它可以完美地工作 - 但它必须在代码中,所以这不是一个选项。此外,如果我让'L2'成为'MyMenu'的孩子,即改为使其成为根项目,则L1和L2再次开始工作 - 但我也不能将所有内容都作为根项目。 有什么东西我不见了吗?

谢谢!

编辑1:

我尝试过一个小实验,看看是否所有内容都已正确初始化,然后再将L1转换为L2并添加其Click事件。仍然没有运气。这是我试过的:

XAML:

<ControlTemplate>
    <Grid>
        <Menu x:Name="MyMenu"/>
        <Button x:Name="MyButton"/>
    </Grid>
</ControlTemplate>

C#:

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();

    MenuItem L1 = new MenuItem() { Header = "L1" };
    MyMenu.Items.Add(L1);

    MenuItem L2 = new MenuItem() { Header = "L2" };
    MyMenu.Items.Add(L2); // Add this to the menu to make sure it gets initialised

    // I created a button so I can make sure that the parenting
    // and events are only added after the MenuItems are loaded
    MyButton.Click += new RoutedEventHandler(MyButton_Clicked);
}

void MyButton_Clicked(object sender, RoutedEventArgs e)
{
    // Note: If these two lines are removed, the events work fine
    MyMenu.Items.Remove(L2);
    L1.Items.Add(L2);

    L1.Click += new RoutedEventHandler(delegate { MessageBox.Show("L1 Click"); });
    L2.Click += new RoutedEventHandler(delegate { MessageBox.Show("L2 Click"); });
}

当窗口加载并显示所有内容时,我单击按钮以便发生父项并添加事件。我看到L2作为L1的孩子被移动,但是当我点击它们时,它们不响应Click事件。如果我阻止父母表格发生,他们会回复Click事件。我很困惑为什么会发生这种情况!

编辑2:

我在一个干净的项目中复制了原始帖子中的所有内容,而且一切都很完美。因此,菜单或MenuItems或它们的使用方式不是问题。这个问题的原因仍然未知......

编辑3:

根据要求,我使用添加到Window的以下代码重新测试了这个:

PreviewMouseLeftButtonDown += new MouseButtonEventHandler(delegate
{
    // Use Ctrl key to enable MessageBox so focus is not lost when opening menu
    if (Keyboard.Modifiers == ModifierKeys.Control)
        MessageBox.Show("Window PreviewMouseLeftButtonDown");
});

单击“死”MenuItems时,总是会触发PreviewMouseLeftButtonDown。他们的Click事件在成为父母之后继续停止工作。仍然没有解决方案也没有问题的迹象......

编辑4:

我做了以下测试,为L1和L2添加了以下代码:

L1.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(delegate
{
    MessageBox.Show("L1 PreviewMouseLeftButtonDown"); 
});
// Same for L2

单击时,MenuItem L1和L2都响应PreviewMouseLeftButtonDown,但在作为父级后继续停止响应。

2 个答案:

答案 0 :(得分:2)

我终于解开了这个谜团。我在其中发现任何点击后,向父控制窃取焦点追踪问题,尽管奇怪的是菜单保持打开并对导航作出反应,即使它没有焦点。一切似乎都适用于Focus()的调用被删除。

发生了什么的细节:

没有 MenuItem育儿:

1)我点击一个根MenuItem,它响应Click。

2)然后父母拦截OnPreviewMouseDown并调用Focus()。

由于Click成功解雇,因此在这种情况下失去焦点不会导致任何不良后果。

WITH MenuItem育儿:

1)我点击菜单然后打开。

2)然后父母拦截OnPreviewMouseDown并调用Focus()。

3)菜单保持打开状态并继续对导航作出反应(这是否意味着发生?)。

4)当我在任何MenuItem上第二次点击进行选择时,菜单终于意识到它失去了焦点并关闭 - 同时也丢弃了点击。

然后,似乎就像点击一样被MenuItem制作并丢弃......导致原帖中遇到的问题。

答案 1 :(得分:0)

我正在为我的书做一个项目 - &#39; VB.Net Libraries-Part3&#39;。我需要和你尝试的一样做。我遇到了同样的问题。但我通过创建来规避这个问题按钮单击运行时菜单项。    我将menu1作为menuStrip item.submenu1_1到submenu1_8作为菜单items.minimenu1_11到minimenu1_18作为子菜单项。我将submenu1_1只放在非点击类别中。其他所有我插入按钮点击事件(全局声明在如果你点击submneu1_1点击你得到一个输出(我在主多线文本框 - 编辑器中得到它)。单击按钮并获得菜单下拉菜单下的子菜单。同样的原则可以扩展到所有子菜单(使用RadioButtons - 激活每个子菜单下拉列表。)

<Application x:Class="Test_Window"

         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:clr="clr-namespace:System;assembly=mscorlib">
<x:Code>
        <![CDATA[
'''''''''''''''''''''''''''Test_Window_Project
Dim s2 As Window = New Window
Dim Editor As TextBox = New TextBox:Dim btn1 As Button = New Button:Dim tbk1 As TextBlock = New TextBlock
Dim canvas1 As Canvas = New Canvas:Dim ListBox1 As ListBox = New ListBox:Dim ListBox2 As ListBox = New ListBox
Dim DllNumber As TextBox = New TextBox:Dim DllMethodsNumber As TextBox = New TextBox:Dim LoopBox As TextBox = New TextBox
Dim DllName1  As TextBox = New TextBox:Dim btn2 As Button = New Button:Dim tbk2 As TextBlock = New TextBlock
Dim btn3 As Button = New Button:Dim btn4 As Button = New Button:Dim btn5 As Button = New Button
Dim tbk5 As TextBlock = New TextBlock:Dim tbk3 As TextBlock = New TextBlock:Dim tbk4 As TextBlock = New TextBlock
Dim menuStrip As Menu= New Menu
Dim menu1 As MenuItem= New MenuItem
Dim submenu1_1 As MenuItem= New MenuItem:Dim submenu1_2 As MenuItem= New MenuItem
Dim submenu1_3 As MenuItem= New MenuItem:Dim submenu1_4 As MenuItem= New MenuItem
'''''''''''''''''''''''''''''''''''menu1 Additions
Dim minimenu1_11 As MenuItem = New MenuItem:Dim minimenu1_12 As MenuItem= New MenuItem
Dim minimenu1_13 As MenuItem = New MenuItem:Dim minimenu1_14 As MenuItem= New MenuItem
Dim minimenu1_15 As MenuItem = New MenuItem:Dim minimenu1_16 As MenuItem= New MenuItem
Dim minimenu1_17 As MenuItem = New MenuItem:Dim minimenu1_18 As MenuItem= New MenuItem
Dim minimenu1_21 As MenuItem = New MenuItem:Dim minimenu1_22 As MenuItem= New MenuItem
Dim minimenu1_23 As MenuItem = New MenuItem:Dim minimenu1_24 As MenuItem= New MenuItem
Dim minimenu1_25 As MenuItem = New MenuItem:Dim minimenu1_26 As MenuItem= New MenuItem
Dim minimenu1_27 As MenuItem = New MenuItem:Dim minimenu1_28 As MenuItem= New MenuItem
Dim minimenu1_31 As MenuItem = New MenuItem:Dim minimenu1_32 As MenuItem= New MenuItem
Dim minimenu1_33 As MenuItem = New MenuItem:Dim minimenu1_34 As MenuItem= New MenuItem
Dim minimenu1_35 As MenuItem = New MenuItem:Dim minimenu1_36 As MenuItem= New MenuItem
Dim minimenu1_37 As MenuItem = New MenuItem:Dim minimenu1_38 As MenuItem= New MenuItem
Dim minimenu1_41 As MenuItem = New MenuItem:Dim minimenu1_42 As MenuItem= New MenuItem
Dim minimenu1_43 As MenuItem = New MenuItem:Dim minimenu1_44 As MenuItem= New MenuItem
Dim minimenu1_45 As MenuItem = New MenuItem:Dim minimenu1_46 As MenuItem= New MenuItem
Dim minimenu1_47 As MenuItem = New MenuItem:Dim minimenu1_48 As MenuItem= New MenuItem
Sub New()
s2.Width=1020:s2.Height=840:s2.Background=Brushes.Yellow:s2.Content=canvas1:s2.Left=0:s2.Top=0
s2.Title="Goodluck_Dll_Analyser":Dim uriSource = New Uri("D:\DllMaster\Goodluck.ico", UriKind.Relative)
s2.Icon = New BitmapImage(uriSource):s2.Name="Goodluck_Dll_Analyser"
''''''''''''''''''''''''''''''''''''''''menu1 Properties
menuStrip.Items.Add(menu1):menu1.Name = "menu1":menu1.Margin=New Thickness(0,0,0,0)
menu1.Header= "T.Menu":menu1.FontWeight=FontWeights.UltraBold:menu1.FontSize="14"
menu1.Foreground=Brushes.White:menu1.Width=80:menu1.Background=Brushes.Brown
''''''''''''''''''''''''''''''''''''''
Editor.TextWrapping=TextWrapping.Wrap:Editor.AcceptsReturn = True:Editor.Width=1000:Editor.Height=723
Editor.Margin=New Thickness(0,74,0,0)
Editor.Background=New SolidColorBrush(Colors.White):Editor.Foreground=New SolidColorBrush(Colors.Navy)
Editor.FontSize=16:Editor.FontWeight=FontWeights.UltraBold:Editor.HorizontalScrollBarVisibility=ScrollBarVisibility.Visible
Editor.VerticalScrollBarVisibility=ScrollBarVisibility.Visible
Editor.HorizontalContentAlignment=HorizontalAlignment.Left
btn1.Width=80:btn1.Height=50:btn1.Margin=New Thickness(0,22,0,0):btn1.Background=New SolidColorBrush(Colors.Navy)
btn1.Content=tbk1:btn1.FontSize="14":btn1.Foreground=New SolidColorBrush(Colors.White):tbk1.InLines.Add("Dim_1_1")
btn1.HorizontalAlignment=System.Windows.HorizontalAlignment.Left:tbk1.TextAlignment = TextAlignment.Center
tbk1.FontStretch=FontStretches.UltraExpanded:tbk1.FontStyle=FontStyles.Italic:tbk1.Typography.SlashedZero=True
tbk1.FontWeight=FontWeights.UltraBold:tbk1.FontFamily=New FontFamily("Century Gothic"):tbk1.LineHeight=Double.NaN
tbk1.TextWrapping=TextWrapping.Wrap:tbk1.Typography.NumeralStyle=FontNumeralStyle.OldStyle
tbk1.Padding = New Thickness(5, 10, 5, 10)
btn4.Width=80:btn4.Height=50:btn4.Margin=New Thickness(246,22,0,0):btn4.Background=New SolidColorBrush(Colors.Red)
btn4.Content=tbk4:btn4.FontSize="14":btn4.Foreground=New SolidColorBrush(Colors.White):tbk4.InLines.Add("Close")
btn4.HorizontalAlignment=System.Windows.HorizontalAlignment.Left:tbk4.TextAlignment = TextAlignment.Center
tbk4.FontStretch=FontStretches.UltraExpanded:tbk4.FontStyle=FontStyles.Italic:tbk4.Typography.SlashedZero=True
tbk4.FontWeight=FontWeights.UltraBold:tbk4.FontFamily=New FontFamily("Century Gothic"):tbk4.LineHeight=Double.NaN
tbk4.TextWrapping=TextWrapping.Wrap:tbk4.Typography.NumeralStyle=FontNumeralStyle.OldStyle
tbk4.Padding = New Thickness(5, 10, 5, 10)
btn5.Width=80:btn5.Height=50:btn5.Margin=New Thickness(328,22,0,0):btn5.Background=New SolidColorBrush(Colors.Navy)
btn5.Content=tbk5:btn5.FontSize="14":btn5.Foreground=New SolidColorBrush(Colors.White):tbk5.InLines.Add("AllClear")
btn5.HorizontalAlignment=System.Windows.HorizontalAlignment.Left:tbk5.TextAlignment = TextAlignment.Center
tbk5.FontStretch=FontStretches.UltraExpanded:tbk5.FontStyle=FontStyles.Italic:tbk5.Typography.SlashedZero=True
tbk5.FontWeight=FontWeights.UltraBold:tbk5.FontFamily=New FontFamily("Century Gothic"):tbk5.LineHeight=Double.NaN
tbk5.TextWrapping=TextWrapping.Wrap:tbk5.Typography.NumeralStyle=FontNumeralStyle.OldStyle
tbk5.Padding = New Thickness(5, 10, 5, 10)
canvas1.Children.Add(Editor):canvas1.Children.Add(btn1):canvas1.Children.Add(ListBox1)
canvas1.Children.Add(btn4):canvas1.Children.Add(btn5):canvas1.Children.Add(menuStrip)
menu1.Items.Add(submenu1_1):submenu1_1.Name = "submenu1_1":submenu1_1.Foreground=Brushes.Brown
submenu1_1.Width=130:submenu1_1.Header= "Action"
s2.Show()
AddHandler btn1.Click,AddressOf btn1Handler
AddHandler btn4.Click,AddressOf btn4Handler:AddHandler btn5.Click,AddressOf btn5Handler
AddHandler submenu1_1.Click,AddressOf submenu1_1Handler
AddHandler submenu1_2.Click,AddressOf submenu1_2Handler
AddHandler submenu1_3.Click,AddressOf submenu1_3Handler
AddHandler submenu1_4.Click,AddressOf submenu1_4Handler
AddHandler minimenu1_11.Click,AddressOf minimenu1_11Handler
AddHandler minimenu1_12.Click,AddressOf minimenu1_12Handler
AddHandler minimenu1_13.Click,AddressOf minimenu1_13Handler
AddHandler minimenu1_14.Click,AddressOf minimenu1_14Handler
End Sub
Private Sub submenu1_1Handler(ByVal sender As Object, ByVal e As RoutedEventArgs)  
    Editor.Text="Hello World!"
End Sub
Private Sub submenu1_2Handler(ByVal sender As Object, ByVal e As RoutedEventArgs)
End Sub
Private Sub submenu1_3Handler(ByVal sender As Object, ByVal e As RoutedEventArgs)
End Sub
Private Sub submenu1_4Handler(ByVal sender As Object, ByVal e As RoutedEventArgs)
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub minimenu1_11Handler(ByVal sender As Object, ByVal e As RoutedEventArgs)
End Sub
Private Sub minimenu1_12Handler(ByVal sender As Object, ByVal e As RoutedEventArgs)
End Sub
Private Sub minimenu1_13Handler(ByVal sender As Object, ByVal e As RoutedEventArgs)
End Sub
Private Sub minimenu1_14Handler(ByVal sender As Object, ByVal e As RoutedEventArgs)
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub btn1Handler(sender As Object,e As RoutedEventArgs)
    Editor.Text &="Hello World!"
menu1.Items.Add(submenu1_2):submenu1_2.Name = "submenu1_2":submenu1_2.Foreground=Brushes.Brown
submenu1_2.Width=130:submenu1_2.Header= "Action"
menu1.Items.Add(submenu1_3):submenu1_3.Name = "submenu1_3":submenu1_3.Foreground=Brushes.Brown
submenu1_3.Width=130:submenu1_3.Header= "Action"
menu1.Items.Add(submenu1_4):submenu1_4.Name = "submenu1_4":submenu1_4.Foreground=Brushes.Brown
submenu1_4.Width=130:submenu1_4.Header= "Action"

submenu1_1.Items.Add(minimenu1_11):minimenu1_11.Header= "New":minimenu1_11.FontSize="12"
minimenu1_11.Foreground=Brushes.White:minimenu1_11.Background=Brushes.DarkSlateBlue
submenu1_1.Items.Add(minimenu1_12):minimenu1_12.Header= "Open":minimenu1_12.FontSize="12"
minimenu1_12.Foreground=Brushes.White:minimenu1_12.Background=Brushes.DarkSlateBlue
minimenu1_12.Width=140:minimenu1_12.Width=140
submenu1_1.Items.Add(minimenu1_13):minimenu1_13.Header= "Close":minimenu1_13.FontSize="12"
minimenu1_13.Foreground=Brushes.White:minimenu1_13.Background=Brushes.DarkSlateBlue
submenu1_1.Items.Add(minimenu1_14):minimenu1_14.Header= "Save":minimenu1_14.FontSize="12"
minimenu1_14.Foreground=Brushes.White:minimenu1_14.Background=Brushes.DarkSlateBlue
minimenu1_13.Width=140:minimenu1_14.Width=140
submenu1_2.Items.Add(minimenu1_21):minimenu1_21.Header= "New":minimenu1_21.FontSize="12"
minimenu1_21.Foreground=Brushes.White:minimenu1_21.Background=Brushes.DarkSlateBlue
submenu1_2.Items.Add(minimenu1_22):minimenu1_22.Header= "Open":minimenu1_22.FontSize="12"
minimenu1_22.Foreground=Brushes.White:minimenu1_22.Background=Brushes.DarkSlateBlue
minimenu1_22.Width=140:minimenu1_22.Width=140
submenu1_2.Items.Add(minimenu1_23):minimenu1_23.Header= "Close":minimenu1_23.FontSize="12"
minimenu1_23.Foreground=Brushes.White:minimenu1_23.Background=Brushes.DarkSlateBlue
submenu1_2.Items.Add(minimenu1_24):minimenu1_24.Header= "Save":minimenu1_24.FontSize="12"
minimenu1_24.Foreground=Brushes.White:minimenu1_24.Background=Brushes.DarkSlateBlue
minimenu1_23.Width=140:minimenu1_24.Width=140
submenu1_2.Items.Add(minimenu1_25):minimenu1_25.Header= "SaveAs":minimenu1_25.FontSize="12"
minimenu1_25.Foreground=Brushes.White:minimenu1_25.Background=Brushes.DarkSlateBlue
submenu1_2.Items.Add(minimenu1_26):minimenu1_26.Header= "Print"
minimenu1_26.Foreground=Brushes.White:minimenu1_26.Background=Brushes.DarkSlateBlue
minimenu1_25.Width=140:minimenu1_26.Width=140
submenu1_2.Items.Add(minimenu1_27):minimenu1_27.Header= "Recent"
minimenu1_27.Foreground=Brushes.White:minimenu1_27.Background=Brushes.DarkSlateBlue
submenu1_2.Items.Add(minimenu1_28):minimenu1_28.Header= "Exit"
minimenu1_28.Foreground=Brushes.White:minimenu1_28.Background=Brushes.DarkSlateBlue
minimenu1_27.Width=140:minimenu1_28.Width=140

submenu1_3.Items.Add(minimenu1_31):minimenu1_31.Header= "New":minimenu1_31.FontSize="12"
minimenu1_31.Foreground=Brushes.White:minimenu1_31.Background=Brushes.DarkSlateBlue
submenu1_3.Items.Add(minimenu1_32):minimenu1_32.Header= "Open":minimenu1_32.FontSize="12"
minimenu1_32.Foreground=Brushes.White:minimenu1_32.Background=Brushes.DarkSlateBlue
minimenu1_32.Width=140:minimenu1_32.Width=140
submenu1_3.Items.Add(minimenu1_33):minimenu1_33.Header= "Close":minimenu1_33.FontSize="12"
minimenu1_33.Foreground=Brushes.White:minimenu1_33.Background=Brushes.DarkSlateBlue
submenu1_3.Items.Add(minimenu1_34):minimenu1_34.Header= "Save":minimenu1_34.FontSize="12"
minimenu1_34.Foreground=Brushes.White:minimenu1_34.Background=Brushes.DarkSlateBlue
minimenu1_33.Width=140:minimenu1_34.Width=140
submenu1_3.Items.Add(minimenu1_35):minimenu1_35.Header= "SaveAs":minimenu1_35.FontSize="12"
minimenu1_35.Foreground=Brushes.White:minimenu1_35.Background=Brushes.DarkSlateBlue
submenu1_3.Items.Add(minimenu1_36):minimenu1_36.Header= "Print"
minimenu1_36.Foreground=Brushes.White:minimenu1_36.Background=Brushes.DarkSlateBlue
minimenu1_35.Width=140:minimenu1_36.Width=140
submenu1_3.Items.Add(minimenu1_37):minimenu1_37.Header= "Recent"
minimenu1_37.Foreground=Brushes.White:minimenu1_37.Background=Brushes.DarkSlateBlue
submenu1_3.Items.Add(minimenu1_38):minimenu1_38.Header= "Exit"
minimenu1_38.Foreground=Brushes.White:minimenu1_38.Background=Brushes.DarkSlateBlue
minimenu1_37.Width=140:minimenu1_38.Width=140

submenu1_4.Items.Add(minimenu1_41):minimenu1_41.Header= "New":minimenu1_41.FontSize="12"
minimenu1_41.Foreground=Brushes.White:minimenu1_41.Background=Brushes.DarkSlateBlue
submenu1_4.Items.Add(minimenu1_42):minimenu1_42.Header= "Open":minimenu1_42.FontSize="12"
minimenu1_42.Foreground=Brushes.White:minimenu1_42.Background=Brushes.DarkSlateBlue
minimenu1_42.Width=140:minimenu1_42.Width=140
submenu1_4.Items.Add(minimenu1_43):minimenu1_43.Header= "Close":minimenu1_43.FontSize="12"
minimenu1_43.Foreground=Brushes.White:minimenu1_43.Background=Brushes.DarkSlateBlue
submenu1_4.Items.Add(minimenu1_44):minimenu1_44.Header= "Save":minimenu1_44.FontSize="12"
minimenu1_44.Foreground=Brushes.White:minimenu1_44.Background=Brushes.DarkSlateBlue
minimenu1_43.Width=140:minimenu1_44.Width=140
submenu1_4.Items.Add(minimenu1_45):minimenu1_45.Header= "SaveAs":minimenu1_45.FontSize="12"
minimenu1_45.Foreground=Brushes.White:minimenu1_45.Background=Brushes.DarkSlateBlue
submenu1_4.Items.Add(minimenu1_46):minimenu1_46.Header= "Print"
minimenu1_46.Foreground=Brushes.White:minimenu1_46.Background=Brushes.DarkSlateBlue
minimenu1_45.Width=140:minimenu1_46.Width=140
submenu1_4.Items.Add(minimenu1_47):minimenu1_47.Header= "Recent"
minimenu1_47.Foreground=Brushes.White:minimenu1_47.Background=Brushes.DarkSlateBlue
submenu1_4.Items.Add(minimenu1_48):minimenu1_48.Header= "Exit"
minimenu1_48.Foreground=Brushes.White:minimenu1_48.Background=Brushes.DarkSlateBlue
minimenu1_47.Width=140:minimenu1_48.Width=140
End Sub
Public Sub btn4Handler(sender As Object,e As RoutedEventArgs)
    Cockpit_Close()
End Sub
Public Sub btn5Handler(sender As Object,e As RoutedEventArgs)
    Editor.Text=""
End Sub
'''''''''''''''''''''''Closing Cockpit_Window from its own Exit_Out Button
Sub Cockpit_Close()
Dim myProcesses() As Process = Process.GetProcesses
    For Each p As Process In myProcesses
        If p.MainWindowTitle.Contains("Goodluck") Then            
            Threading.Thread.Sleep(200)
            p.CloseMainWindow()
            p.Kill()
        End If
    Next
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''
        ]]>
</x:Code>
</Application>

相应的.vbproj文件是: -

**

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{BE9B3350-5541-4EB5-BD4D-F6938B472E5E}</ProjectGuid>
    <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{F184B08F-C81C-45F6-A57F-5ABD9991F28F}</ProjectTypeGuids>
    <OutputType>WinExe</OutputType>
    <RootNamespace>Test_Window</RootNamespace>
    <AssemblyName>Test_Window</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <MyType>Custom</MyType>
    <PublishUrl>publish\</PublishUrl>
    <Install>true</Install>
    <InstallFrom>Disk</InstallFrom>
    <UpdateEnabled>false</UpdateEnabled>
    <UpdateMode>Foreground</UpdateMode>
    <UpdateInterval>7</UpdateInterval>
    <UpdateIntervalUnits>Days</UpdateIntervalUnits>
    <UpdatePeriodically>false</UpdatePeriodically>
    <UpdateRequired>false</UpdateRequired>
    <MapFileExtensions>true</MapFileExtensions>
    <ApplicationRevision>0</ApplicationRevision>
    <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
    <IsWebBootstrapper>false</IsWebBootstrapper>
    <UseApplicationTrust>false</UseApplicationTrust>
    <BootstrapperEnabled>true</BootstrapperEnabled>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <DefineDebug>true</DefineDebug>
    <DefineTrace>true</DefineTrace>
    <IncrementalBuild>true</IncrementalBuild>
    <OutputPath>bin\Debug\</OutputPath>
    <DocumentationFile>Test_Window.xml</DocumentationFile>
    <NoWarn>41999,42016,42017,42018,42019,42020,42021,42022,42032,42036,42314</NoWarn>
  </PropertyGroup>
  <ItemGroup>    
    <Reference Include="System.Core" />
    <Reference Include="System.Drawing" />
    <Reference Include="system.windows.controls.ribbon" />
    <Reference Include="System.Windows.Forms" />
    <Reference Include="System.Windows" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
    <Reference Include="System.Xaml">
      <RequiredTargetFramework>4.0</RequiredTargetFramework>
    </Reference>
    <Reference Include="UIAutomationProvider" />
    <Reference Include="UIAutomationTypes" />
    <Reference Include="WindowsBase" />
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
  </ItemGroup>
  <ItemGroup>
    <ApplicationDefinition Include="Test_Window.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </ApplicationDefinition>
  </ItemGroup>
  <ItemGroup>    
    <Imports Include="System.Drawing.Bitmap" />
    <Imports Include="System.MarshalByRefObject" />
    <Imports Include="System.Drawing.Icon" />
    <Import Include="System.Windows.Shapes" />
    <Import Include="System.Attribute" />
    <Import Include="System.Collections.Generic" />
    <Import Include="System.Collections" />
    <Import Include="System.ComponentModel" />
    <Import Include="System.Data" />
    <Import Include="System.Diagnostics" />
    <Import Include="System.Drawing" />
    <Import Include="System.Linq" />           
    <Import Include="Microsoft.VisualBasic" />
    <Import Include="Microsoft.Win32" />
    <Import Include="System.Net" />
    <Import Include="System.Net.WebClient" />
    <Import Include="System 'STAThread" />
    <Import Include="System.Windows.Application" />
    <Import Include="System.Object" />
    <Import Include="System.Reflection" />
    <Import Include="System.Runtime.InteropServices" />
    <Import Include="System.IO" />
    <Import Include="System" />
    <Import Include="System.Web" />
    <Import Include="System.Windows" />
    <Import Include="System.Windows.Forms" />
    <Import Include="System.Windows.Automation.Peers" />
    <Import Include="System.Windows.Controls" />
    <Import Include="System.Windows.Data" />
    <Import Include="System.Windows.Documents" />
    <Import Include="System.Windows.Input" />
    <Import Include="System.Windows.Media" />
    <Import Include="System.Windows.Media.Imaging" />
    <Import Include="System.Windows.Navigation" />
    <Import Include="System.Windows.Shapes" />
    <Import Include="System.Windows.Threading" />
    <Import Include="System.Windows.Threading.DispatcherTimer" />
    <Import Include="System.Text" />
    <Import Include="System.Threading" />
    <Import Include="System.Threading.Tasks" />
    <Import Include="System.Xml" />
    <Import Include="System.Xml.Linq" />
  </ItemGroup>  
  <ItemGroup>
    <BootstrapperPackage Include=".NETFramework,Version=v4.5">
      <Visible>False</Visible>
      <ProductName>Microsoft .NET Framework 4.5 %28x86 and x64%29</ProductName>
      <Install>true</Install>
    </BootstrapperPackage>
    <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
      <Visible>False</Visible>
      <ProductName>.NET Framework 3.5 SP1</ProductName>
      <Install>false</Install>
    </BootstrapperPackage>
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
</Project>

**

来自cmd的编译调用是: -

msbuild.exe C:\ Users \ YourName \ Desktop \ Test \ Test_Window.vbproj / t:ReBuild C:\用户\ YOURNAME \桌面\测试\ BIN \调试\ Test_Window.exe

测试: - 点击T.Menu 1st.You get&#39; Hellow World!&#39;在编辑。 现在点击Dim1_1按钮。你会看到完整的下拉minimenus。 问候

Venkatraman