我尝试制作使用UserControl(WPF)的Visual C#app。 默认情况下,VS Community 2015 C#在与主程序相同的命名空间中生成UserControl。它构建正确。 我希望将其与自己的命名空间分开,以便将来重用。 我更改自动生成的代码以将UserControl置于单独的命名空间中。
这是我的代码
用户控件
<UserControl x:Class="nsTabI2CMemRW.TabI2CMemRW"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:nsTabI2CMemRW"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="500">
<Grid>
<TextBox x:Name="AddrH"/>
<Label x:Name="AddrH_Label"/>
</Grid>
C#class
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;
using WpfApplication1; // This is my main app namespace
namespace nsTabI2CMemRW
{
/// <summary>
/// Interaction logic for TabI2CMemRW.xaml
/// </summary>
public partial class TabI2CMemRW : UserControl
{
public TabI2CMemRW()
{
//InitializeComponent();
((WpfApplication1.MainWindow)Application.Current.MainWindow).InitializeComponent(); //InitializeComponent();
}
}
}
主Windows XAML
<Window x:Class="WpfApplication1.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:WpfApplication1"
xmlns:UserControl="clr-namespace:nsTabI2CMemRW"
mc:Ignorable="d"
Title="MainWindow" Height="649.005" Width="620.667" ScrollViewer.CanContentScroll="True">
<Grid>
<TabControl x:Name="tabControl" Margin="0,0,0.4,0.2">
<TabControl.BindingGroup>
<BindingGroup/>
</TabControl.BindingGroup>
<TabItem x:Name="I2CMemReadWrite" Header="I2C Mem Read/Write">
<UserControl:TabI2CMemRW Margin="0,0,-0.2,278.2"/>
</TabItem>
<TabItem Header="TabItem">
<Button x:Name="button" Content="Button" Height="100" Width="75"/>
</TabItem>
</TabControl>
</Grid>
Designer显示错误&#34;对象引用未设置为对象的实例&#34;换行
<UserControl:TabI2CMemRW Margin="0,0,-0.2,278.2"/>
程序编译正常,但第一个标签(我的UserControl应该在哪里)是空白的。
答案 0 :(得分:1)
这里有很多不妥之处,无法回答所有问题。尝试冥想 ((WpfApplication1.MainWindow)Application.Current.MainWindow).InitializeComponent();
离开
的InitializeComponent();
代替你的C#类
答案 1 :(得分:0)
是的,这((WpfApplication1.MainWindow)Application.Current.MainWindow).InitializeComponent();
错了!错误&#34;对象引用未设置为对象的实例&#34;与此代码相关(使用VS调试器发现,如本文WPF, 'Object reference not set to an instance of an object' in Designer中所述。我认为它对WPF的类似案例很有用。)
评论出来后(见上文)并只留下InitializeComponent()
并重新编译代码两次 - 它有效!
我还检查了正确的代码隐藏文件绑定到XAML文件(Accidentally deleted the code behind for an Xaml file. How do I add the code behind again?)。
此行也很重要xmlns:UserControl="clr-namespace:nsTabI2CMemRW"
和<UserControl:TabI2CMemRW Margin="0,0,-0.2,278.2"/>
。我想我可以第一次错过他们。 (xmlns:
之后可以是任何名称,不仅是UserControl)