【问题标题】:Error importing namespace into XAML file将命名空间导入 XAML 文件时出错
【发布时间】:2012-10-31 06:41:15
【问题描述】:

当我尝试将特定命名空间导入到我的自定义用户控件时出现此错误。导致此错误的行是导入:'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>

【问题讨论】:

  • 您是否尝试将快捷方式c 替换为其他快捷方式? (也许它以某种方式保留)
  • 是的 - 我应该提到这一点。我尝试了过去成功使用过的快捷方式。
  • 工具栏的 Xaml 在哪里?
  • 我从这里复制并粘贴了你的代码到一个测试项目中,它对我来说很好用。无论如何,XML 无效错误通常是由于错误的 xml 格式(例如缺少 > 符号)
  • @Silvermind 我不确定您要问的是哪个 XAML。工具栏的 XAML 已发布,即用户控件。如果这就是您所指的,我可以将我正在应用的模板发布到 CollaborateButton。

标签: c# wpf xaml


【解决方案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>

【讨论】:

  • 这也是我的想法,但我实际上是在十六进制编辑器中打开文件并确认没有杂项字符。
  • 感谢您的回答!我遇到了同样的错误,在我的情况下,这是因为在结束 &lt;/UserControl&gt; 标记之后有一个额外的空行(这可能是由于有人复制和粘贴整个 xaml 文件而发生的)。删除该行消除了错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多