自定义xmlns命名空间不起作用

时间:2014-09-17 02:19:32

标签: c# wpf xaml

我正在尝试关注these instructions将标签内容分成单独的文件。这是我的文件结构
TabAttributesContext.xaml in TabContext folder with MainWindow.xaml

我正在尝试使用 1 文件加载文件 2 的内容,以便它们协同工作。这是文件2:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <UserControl x:Key="Tab1Control">
        <DataTemplate DataType="TabItem">
            <TextBlock Text="Test text"></TextBlock>
        </DataTemplate>
    </UserControl>
</ResourceDictionary>

文件1的相关部分:

<Window x:Class="MnMCharacterCreator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:tabs="TabContent/TabAttributesContent">
        ...
    <!--Creates a tabbing system, with a grid defined by each ItemsControl-->
    <TabControl Name="WindowTabs">
        <TabItem Name="WindowTab1" Header="Attributes">
            <!--This is where the UserControl from file 2 should be loaded-->

在访问these two个相关问题(与其他数十个人有关)并询问C#聊天后,我不得不认为这是不寻常的:

tabs is not supported in a WPF project error message

Intellisense没有为<tabs:显示任何内容,即使我手动键入现有名称或其他内容,也会给出错误消息,这意味着它不是设计器问题。 Here是VS2012中的完整解决方案。

具体来说,我要问的问题是如何使用其他xaml文件中的内容?如果xmlns无法使用,那么是什么?

1 个答案:

答案 0 :(得分:0)

您可能希望为标签项创建用户控件,

<UserControl x:Class="WPFSample.TabItem1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
<Grid>
    <TextBlock Height="100" Width="100" Text="Hi from tab item 1"/>
</Grid>

然后使用它将MainWindow中的命名空间添加为:

<Window x:Class="WPFSample.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"
    xmlns:local="clr-namespace:WPFSample">
<Grid>
    <TabControl>
        <TabItem Header="XYZ">
            <local:TabItem1/>
        </TabItem>
    </TabControl>
</Grid>

您已准备好使用。

希望您已将用户控件xaml.cs从XYZ:Window更改为XYZ:UserControl并构建解决方案一次。

相关问题