【发布时间】: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。