将usercontrol添加到项目中时出现XamlParseException

时间:2018-01-08 16:06:47

标签: c# wpf xaml user-controls

我在一个项目中创建了一个用户控件,该项目只包含一个MainWindow.xaml和后面的代码。我将.dll添加到VS的工具箱中,并将其放到新项目的窗口中。这创建了以下内容:

<Window
        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:WpfApplication5"
        xmlns:ThinkGeoClean="clr-namespace:ThinkGeoClean;assembly=ThinkGeoClean" x:Class="WpfApplication5.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" >
    <Grid>
        <ThinkGeoClean:ListBoxCustom x:Name="listBoxCustom" />
    </Grid>
</Window>

ThinkGeoClean是我添加的.dll的名称,即usercontrol。 ListBoxCustom只是控件中的公共类,但不是我想要展示的。我想显示usercontrol的主窗口(不是窗口),但在键入<ThinkGeoClean:后它不显示为选项。唯一显示的是ListBoxCustom。如果我继续输入<ThinkGeoClean.MainWindow>,它会在该行上出现XamlParseException错误。

现在,如果我进入代码隐藏并执行:

ThinkGeoClean.MainWindow newWin = new ThinkGeoClean.MainWindow();
newWin.Show();

它将在新窗口中弹出用户控件,它可以正常工作。

这是后面的usercontrol代码的开头:

namespace ThinkGeoClean
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {

它的xaml只是一个包含一些按钮和一个地图控件的网格。

编辑:除了下面的答案,我的用户控件最初只是一个普通的WPF项目。我认为将输出类型更改为类库会自动将其更改为用户控件,但实际上我必须进入xaml并将其更改为。

1 个答案:

答案 0 :(得分:2)

这个XAML:

Children

... not 等同于创建窗口实例并以编程方式调用Grid方法。

相反,XAML处理器会尝试将窗口添加到UserControl的{​​{1}}集合中,这是不可能的,因为窗口不能是另一个控件的子节点。这就是你获得例外的原因。

此外,class SuperSignupScreenImpl extends React.PureComponent<SuperSignupScreenProps & {dispatch: Dispatch<{}>}, {}> { render() { return( <div> <TextField hintText = "Name" onChange = {this.handleInputChange.bind(this)} name = "name" > </TextField> <p>{this.props.name}</p> </div> ); } handleInputChange(event){ const target = event.target; const value = target.value; const name = target.name; console.log(value); this.props.dispatch(updateForm(name, value)); } } function mapStateToProps(state: {name: string[]}): SuperSignupScreenProps { return {name: state.name}; } const SuperSignupScreen = connect(mapStateToProps)(SuperSignupScreenImpl); export default SuperSignupScreen; 必须托管在窗口或页面中。它不是一个没有任何父主机就可以显示的顶级控件。