【问题标题】:Silverlight 4 user control not appearingSilverlight 4 用户控件未出现
【发布时间】:2011-10-26 08:33:31
【问题描述】:

我是 SL 的新手,这里肯定遗漏了一些真正重要的东西。

我创建了一个非常简单的用户控件,如下所示:-

<UserControl x:Class="Company.UI.Common.Controls.TestControl"
    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"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="Yellow" Width="100" Height="20">
        <TextBlock Text="foo"></TextBlock>
    </Grid>
</UserControl>

那么在我看来,我是这样引用它的:-

xmlns:medControls="clr-namespace:Company.UI.Common.Controls;assembly=Company.UI.Common"

然后像这样在 UI 中包含它:-

<medControls:TestControl Width="100" Height="20" Visibility="Visible" />

但是,当我运行该应用程序时,什么都没有出现,只有一个 100x20 像素的空白空间,该空间应该是控件所在的位置。我使用了 Silverlight Spy,它显示了存在的控件,以及所有正确的细节——类型、组件、可见性等。 我已经在用户控件的构造函数中设置了一个断点,并且可以确认 InitializeComponent() 正在被调用。

任何关于正在发生的事情的建议将不胜感激,因为我正在为应该做的一件非常简单的事情而烦恼!

提前致谢 安迪

【问题讨论】:

  • 您的用户控件很好,我认为托管它的视图有问题,您需要显示完整代码

标签: xaml silverlight-4.0


【解决方案1】:

这原来是 VS2010 中的一个已知错误,在此报告:http://connect.microsoft.com/VisualStudio/feedback/details/683175/visual-studio-failing-to-embed-g-resources-file-in-dll-with-certain-silverlight-project-files

基本上,在某些情况下,.csproj 文件中元素的顺序可能会发生变化,导致 XAML 文件不包含在程序集中,这就是发生在我身上的事情。没有运行时错误。没有什么。只是应该呈现用户控件的空白空间。

上面的链接告诉您解决问题需要做什么,包括手动编辑 .csproj 文件和移动某些元素。

顺便说一句,同样的错误也是我在用户控件的代码隐藏引用控件(例如“this.txtForename”)时遇到的另一个问题的原因。使用 FindName() 在 InitializeComponent 中设置的控件引用始终为空。

希望这对其他人有所帮助。

安德鲁

【讨论】:

    【解决方案2】:

    看看这是否有帮助:

    http://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol%28v=vs.95%29.aspx

    http://10rem.net/blog/2010/02/05/creating-customized-usercontrols-deriving-from-contentcontrol-in-wpf-4

    我从未尝试在 xaml 中创建自定义用户控件,我总是以编程方式进行,您是否可以在此示例中看到,但如果您使用一个 ContentControl 或一个 ContentPresenter 然后称你为 UserControl

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多