将名称空间导入XAML文件时出错

时间:2012-10-30 21:48:59

标签: c# wpf xaml

当我尝试将特定命名空间导入自定义用户控件时,我收到此错误。导致此错误的行是导入:'xmlns:c =“clr-namespace:Wifi.Toolbar'。如果我删除此行,则错误消失。

WifiCollaborateToolbarView.xaml(1,1):错误MC3000:'根级别的数据无效。第1行,第1行。' XML无效。

Windows 7
Visual Studio 2010 SP1
.NET 4.0

这是XAML文件:

<UserControl x:Class="Wifi.Toolbar.WifiCollaborateToolbarView"
             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:c="clr-namespace:Wifi.Toolbar">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="CollaborateStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid x:Name="GridTopLevel" SnapsToDevicePixels="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <c:CollaborateButton x:Name="ButtonName"
                             Grid.Column="0" 
                             Style="{StaticResource MpCollaborateButtonStyle}"/>


    </Grid>
</UserControl>

以下是我要导入此文件的类。它与XAML文件存在于同一个程序集中。

using System;
using System.Windows;
using System.Windows.Controls;

namespace Wifi.Toolbar
{
    [TemplatePart(Name="PART_Icon", Type=typeof(Image))]
    [TemplatePart(Name="PART_Caption", Type=typeof(TextBlock))]
    public class CollaborateButton : Button
    {
        private Image part_icon;
        private TextBlock part_caption;

        public CollaborateButton()
        {

        }

        public override void OnApplyTemplate()
        {
            ...
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            ...
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            ...            
        }
    }   
}

按钮模板

<Style x:Key="MpCollaborateButtonStyle" TargetType="{x:Type c:CollaborateButton}">
        <Setter Property="Height" Value="24" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="BorderBrush" Value="{StaticResource MpCollaborateButtonNormalStrokeBrush}" />
        <Setter Property="Background" Value="{StaticResource MpButtonNormalFillBrush}" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="Bd"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                            MinWidth="{TemplateBinding MinWidth}"
                            Height="{TemplateBinding Height}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            Background="{TemplateBinding Background}">

                        <Grid Margin="6,0,12,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                            <Image x:Name="PART_Icon"
                                   Grid.Column="0"
                                   Margin="0,0,7,0" 
                                   Width="16" 
                                   Height="16" 
                                   Source="Images/Collaboration_MeetingName_16x16.png" />

                            <TextBlock x:Name="PART_Caption"
                                       Grid.Column="2"
                                       Text="This is some really long text that is only for testing purposes. Isn't this fun?"
                                       Style="{StaticResource MpCollaborateTextBlock}"/>

                        </Grid>
                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="{StaticResource MpButtonDownFillBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

1 个答案:

答案 0 :(得分:1)

您必须在根级别UserControl声明之前有一些垃圾。也许它被标记在你没看到的地方。例如,这会导致错误...

                                                                                                      Junk
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

    </Grid>
</Window>

与此相同:

Junk
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

    </Grid>
</Window>