wpf错误打开另一个表单

时间:2017-09-29 20:10:47

标签: c# wpf xaml

当我尝试通过按钮实例化新表单时,我收到错误:

  

'主窗口'不包含' NewWindow'的定义和   没有扩展方法' NewWindow'接受第一个论点   键入' MainWindow'可以找到(你错过了使用指令或   汇编参考?)

我的表单是 MainWindow NewWindow ,我在MainWindow中有一个按钮,可以打开NewWindow。

见下文:

MainWindow.xaml

<Window x:Class="MyNamespace.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:MyNamespace"
    mc:Ignorable="d"
    Title="Main Window"
    Height="400"
    Width="640"
>
<Grid>
    <Button
            VerticalAlignment="Top"
            HorizontalAlignment="Right"
            Content="Open New Window"
            Width="200"
            Click="OpenNewWindow"
            Margin="0,10,10,0"
            x:Name="btOpenNewWindow"
        />
</Grid>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyNamespace
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OpenNewWindow(object sender, RoutedEventArgs args)
        {
            NewWindow nw = new NewWindow();
            nw.Show();
        }
    }
}

NewWindow.xaml

<Window x:Class="MyNamespace.NewWindow"
    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:MyNamespace"
    mc:Ignorable="d"
    Title="New Window"
    Height="400"
    Width="640"
    >
<Grid>
    <Label Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" Margin="43,3,0,0"/>
</Grid>

NewWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MyNamespace
{
    public partial class NewWindow : Window
    {
        public NewWindow()
        {
            InitializeComponent();
        }
    }
}

我不明白出了什么问题。

0 个答案:

没有答案