WPF MenuItem.Click 在父子关系后中断

发布于 2024-10-31 14:34:23 字数 3255 浏览 1 评论 0原文

我正在尝试使用各种菜单项(全部在代码中创建)动态填充菜单控件(位于 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:

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

编辑 3:

根据要求,我使用添加到窗口中的以下代码重新测试了此问题:

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");
});

当按住 ctrl 键单击“死”菜单项时,始终会触发 PreviewMouseLeftButtonDown。他们的 Click 事件在设置父级后继续停止工作。仍然没有解决方案,也没有表明问题...

编辑 4:

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

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

单击时,MenuItem L1 和 L2 都响应 PreviewMouseLeftButtonDown,但在之后继续停止响应 Click被养育。

I am trying to dynamically populate a Menu control (which resides in a ControlTemplate) with various MenuItems (all created in code) but I am running into some weird problems. If the MenuItems are all created as root items, they fire their Click event successfully. But as soon as any MenuItem is made a child of another MenuItem, both the parent and child's Click events stop firing.

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"); });
}

The menu displays correctly, but none of the click events on the sub-items fire at all. If I pre-define the menu in XAML and set all Click events in XAML too, it works perfectly - but it has to be in code, so this is not an option. Also, if I make 'L2' to be a child of 'MyMenu', i.e. make it a root item instead, L1 and L2 start to work again - but I cannot have everything as root items either.
Is there something that I am missing?

Thank you!

EDIT 1:

I have tried a little experiment to see if everything was properly initialised before parenting L1 to L2 and adding their Click events. Still no luck. Here is what I tried:

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"); });
}

When the Window loads up and everything is displayed, I click on the Button so that the parenting happens and events are added. I see L2 being moved as a child of L1, but when I click either, they don't respond to Click events. If I prevent the parenting form happening, they do respond to Click events. I am so confused as to why this is happening!

EDIT 2:

I replicated everything from the original post in a clean project, and it all works perfectly. So it's not a problem with the Menu or MenuItems or the way they were used. The cause of this problem is still unknown...

EDIT 3:

As requested, I re-tested this with the following code added to the 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");
});

PreviewMouseLeftButtonDown is always triggered when ctrl-clicking on the 'dead' MenuItems. Their Click events continue to stop working after being parented. Still no solution nor indication of the problem...

EDIT 4:

I did the following test, adding the below code for both L1 and L2:

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

When Clicked, MenuItem L1 and L2 both respond to PreviewMouseLeftButtonDown, but continue to stop responding to Click after being parented.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落□心尘 2024-11-07 14:34:23

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

所发生事件的详细信息:

没有 MenuItem 父子关系:

1) 我单击根 MenuItem,它响应 Click。

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

由于 Click 已成功触发,因此在这种情况下不会出现因失去焦点而导致的不良结果。

WITH MenuItem 父子关系:

1) 我单击菜单,它会打开。

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

3)菜单保持打开状态并继续对导航做出反应(这是注定要发生的吗??)。

4)当我第二次单击任何菜单项进行选择时,菜单最终意识到它失去了焦点并关闭 - 也放弃了单击。

然后,似乎就像单击了 MenuItem 并丢弃了...导致原始帖子中遇到的问题。

I've finally solved the mystery. I tracked down the problem to the parent Control stealing focus after any clicks are made within it, although it is weird that the menu stayed open and reacted to navigation even though it did not have focus. Everything seems to work with the call to Focus() being removed.

The details of what was happening:

WITHOUT MenuItem parenting:

1) I click on a root MenuItem and it responds to Click.

2) OnPreviewMouseDown is then intercepted by the parent and a call to Focus() is made.

There is no adverse results caused by the loss of focus in this situation since Click was successfully fired.

WITH MenuItem parenting:

1) I click on the menu and it opens.

2) OnPreviewMouseDown is then intercepted by the parent and a call to Focus() is made.

3) The menu stays open and continues to react to navigation (is this meant to happen??).

4) When I click the second time on any MenuItem to make a selection, the menu finally realises that it lost focus and closes - also discarding the click.

It then seems like a click was made and discarded by the MenuItem... leading to the problem experienced in the original post.

陌上芳菲 2024-11-07 14:34:23

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

<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:\Users\YourName\Desktop\Test\bin\Debug\Test_Window.exe

测试:-单击 T.Menu 1st。您会看到“Hellow World!”在编辑器中。
现在单击 Dim1_1 按钮。您将看到完整的下拉迷你菜单。
问候

文卡特拉曼

I was doing a project for my book-'VB.Net Libraries-Part3'.I needed to do the same as what you attempted.I had the same problems.But i circumvented the problem by creating runtime menuitems by a button click.
I had menu1 as menuStrip item.submenu1_1 to submenu1_8 as menu items.minimenu1_11 to minimenu1_18 as submenu items.I put the submenu1_1 only in the non-click category.All others i inserted into a button click event(the global declarations were done in the main).Now if you click the submneu1_1 click you get one output(i got it in the main multiline textbox-Editor).Click the button and get the minimenus with submenus on dropdown of menu1.The same principle can be extended to all submenus(use RadioButtons-to activate each submenu dropdown).

<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>

The corresponding .vbproj file is:-

**

<?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>

**

Compilation call from cmd is:-

msbuild.exe C:\Users\YourName\Desktop\Test\Test_Window.vbproj /t:ReBuild
C:\Users\YourName\Desktop\Test\bin\Debug\Test_Window.exe

Testing:-Click the T.Menu 1st.You get 'Hellow World!' in the Editor.
Now click the Dim1_1 button.You will see the full drop down minimenus.
regards

Venkatraman

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文