【问题标题】:Design-time failure: WPF, Usercontrols and Namespaces设计时失败:WPF、用户控件和命名空间
【发布时间】:2010-05-08 16:19:14
【问题描述】:

我有一个非常简单的 WPF 项目,包括一个窗口和用户控件。我非常处于学习阶段。当我运行它时它工作正常。但是,我无法在设计时看到表单。我相信这个问题与命名空间有关,但我不明白在哪里。这很可能是一个简单的错误

主窗口 XML

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:views="clr-namespace:UserLogin"
    x:Class="UserLogin.MainView"
    x:Name="MainViewWindow"
    mc:Ignorable="d" 
    Title="Login" 
    Height="141" 
    Width="347"
    >

    <Grid>        
        <views:LoginView />
    </Grid>

</Window>

主窗口代码隐藏

Imports Microsoft.VisualBasic
Imports System
Imports System.Windows
Imports UserLogin

Namespace UserLogin

    Partial Public Class MainView
        Inherits System.Windows.Window

        Public Sub New()
            InitializeComponent()
        End Sub

    End Class

End Namespace

用户控件 XAML

<UserControl 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:d="http://schemas.microsoft.com/expression/blend/2008" 
             x:Class="UserLogin.LoginView"
             x:Name="LoginViewControl"
             mc:Ignorable="d" d:DesignHeight="96" d:DesignWidth="298">
    <Grid  Height="96" Width="298">
        <Button Command="{Binding OKCommand}" Height="21" Margin="0,0,90,16" Name="btnOK" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="76">OK</Button>
        <Button Command="{Binding CancelCommand}" Height="21" HorizontalAlignment="Right" Margin="0,0,9,16" Name="btnCancel" VerticalAlignment="Bottom" Width="75">Cancel</Button>
        <Label Height="23" HorizontalAlignment="Left" Margin="10,5,0,0" Name="Label1" VerticalAlignment="Top" Width="85">Name:</Label>
        <Label HorizontalAlignment="Left" Margin="10,32,0,0" Name="Label2" Width="85" Height="29" VerticalAlignment="Top">Password:</Label>
        <TextBox Margin="0,31,6,0" Name="txtPassword" Height="22" VerticalAlignment="Top" HorizontalAlignment="Right" Width="182" />
        <ComboBox Height="22" Margin="110,6,6,0" Name="cboNames" VerticalAlignment="Top" />
    </Grid>
</UserControl>

用户控制代码隐藏

Imports Microsoft.VisualBasic
Imports System
Imports UserLogin

Namespace UserLogin
    Partial Public Class LoginView
        Inherits System.Windows.Controls.UserControl

        Public Sub New()
            InitializeComponent()
        End Sub

    End Class
End Namespace

我想我在这个命名空间中遗漏了一些东西

xmlns:views="clr-namespace:UserLogin"

因为智能感知没有给我在 XAML 设计器中声明的用户控件,而是报告错误“无法加载程序集的元数据......等等”

感谢任何建议

西蒙

【问题讨论】:

  • 我建议,如果您找到解决问题的答案,那么您应该接受该答案。

标签: wpf user-controls namespaces


【解决方案1】:

我遇到了这个问题并解决了。我在设计器中遇到了错误,但是在运行程序时它起作用了。在这种情况下,源代码位于网络驱动器上,并且该驱动器没有完全信任权限。我将代码移动到本地,它开始工作。如果您希望将代码保留在网络驱动器上,也可以添加完全信任权限。

【讨论】:

【解决方案2】:

这可能是一个愚蠢的问题,但是您是否在创建控件后重新构建了项目? Visual Studio WPF 编辑器对查找程序集非常挑剔,但“找不到元数据”几乎总是意味着您引用的程序集不存在。
每当您遇到奇怪的 WPF 设计器问题时,请始终重建并可能重新启动 VS。

【讨论】:

  • 谢谢。我已经多次重建/清理/删除 bin 和 obj 等。所以尝试关闭并重新打开VS2010。在设计时让用户控件出现在窗口中仍然没有乐趣。大概因为它在运行时看起来没问题,你认为假设它是设计者是安全的吗?
  • 好吧,你明白了,我从未见过如此顽固的错误。在我看来,设计师对你的控制有些过敏,但它是如此简单,我不知道为什么会发生这种情况。尝试注释掉部分,看看它最终是否会重新出现?
  • 我已经尽我所能做到了,并将其缩小到引用主窗口中命名空间的行。我可以将用户控件中的所有 xaml(引用控件)放入主窗口,就可以了。所以我从 codeplex (BubbleBurst) 下载了一个 wpf 组件,发现我有类似的问题。我开始怀疑我是否没有正确安装某些东西。
  • 我在使用 VS2008 时遇到了同样的错误。西蒙,你接受了这个答案,但评论说它不起作用。你还记得你做了什么来解决这个问题吗?
  • @bufferz,见下面的 Steve0212
【解决方案3】:

MainView 和 LoginView 控件是否在同一个程序集中?如果没有,则需要在名称空间 defn 中包含程序集信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    相关资源
    最近更新 更多