【问题标题】:Custom xmlns namespaces not working自定义 xmlns 命名空间不起作用
【发布时间】:2014-09-17 12:36:28
【问题描述】:

我正在尝试关注these instructions 将标签内容分成单独的文件。这是我的文件结构

我正在尝试使用文件 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-->

在访问thesetwo 相关问题(与其他数十人)并在 C# 聊天中提问后,我觉得这很不寻常:

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

具体来说,我想问的问题是如何使用来自另一个 xaml 文件的内容?如果不可能使用 xmlns,那是什么?

【问题讨论】:

  • 用户控件中的数据模板?我不确定您要达到的目标。但同时2 是一个资源字典,因此您可能看不到任何带有tabs: 的内容,但您可以通过{StaticResource Tab1Control}&lt;StaticResourceExtension ResourceKey="Tab1Control" /&gt; 等标记访问。
  • @pushpraj 但是当我试图加载 XAML 内容时,除非有我不知道的 content= 属性,否则我不能像资源一样使用它。
  • 你真正想要实现什么?
  • @pushpraj 我需要使用来自另一个 xaml 文件的内容,正如问题所问的那样,并且在拆分 xaml 文件的上下文中,以便每个选项卡项都有一个单独的 xaml 文件,如在最佳。任何允许我在运行时将每个选项卡项的内容放在适当的位置,同时在设计时将它们分开到不同文件中的任何东西都是我所要求的。
  • 值得注意的是,相当多的用户对我将 xaml 文件分离的想法回应为“为什么要这样做?”好吧,想象一个文件中有 10,000 行 XAML。这不是大型应用程序如何做到这一点,我需要配置此应用程序以实现灵活性。我只是提前说这些。

标签: c# wpf xaml


【解决方案1】:

您可能希望为选项卡项创建一个用户控件,

<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 并构建解决方案一次。

【讨论】:

    猜你喜欢
    • 2016-09-14
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2014-10-25
    • 2011-08-08
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多