Silverlight 4 用户控件未出现

发布于 2024-12-12 08:56:34 字数 1178 浏览 0 评论 0 原文

我是 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()。

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

提前致谢 安迪

I'm new to SL and must be missing something really fundamental here.

I have created a very simple user control like so:-

<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>

Then in my view I'm referencing it as follows:-

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

Then including it in the UI like this:-

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

However nothing appears when I run the app, just an empty space 100x20 pixels where the control should be. I've used Silverlight Spy and it shows the control being present, with all the correct details - type, assembly, visibility, etc.
I have put a breakpoint in the user control's constructor, and can confirm that InitializeComponent() is being called.

Any suggestions as to what is happening would be greatly appreciated, as I'm tearing my hair out over what should be a very simple thing to do!

Thanks in advance
Andy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晌融 2024-12-19 08:56:34

事实证明,这是 VS2010 中的一个已知错误,报告如下:http://connect.microsoft.com/VisualStudio/feedback/details/683175/visual-studio-failing-to-embed-g-resources-file-in-dll-with-certain-silverlight- 基本上,在某些情况

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

上面的链接告诉您需要执行哪些操作来解决问题,并涉及手动编辑 .csproj 文件并移动某些元素。

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

希望这对其他人有帮助。

安德鲁

This turned out to be a known bug in VS2010, reported here: http://connect.microsoft.com/VisualStudio/feedback/details/683175/visual-studio-failing-to-embed-g-resources-file-in-dll-with-certain-silverlight-project-files

Basically, in some situations, the order of elements in the .csproj file can change, resulting in the XAML files not being included in the assembly, and this is what was happening with me. No runtime errors. Nothing. Just an empty space where the user control should have rendered.

The above link tells you what you need to do to resolve the problem, and involves manually editing the .csproj file and moving certain elements around.

Incidentally, this same bug was also the cause of another problem I'd been having where my user control's code-behind was referencing a control (e.g. "this.txtForename"). The control reference, which is set up in InitializeComponent using FindName(), was always null.

Hope this helps someone else.

Andrew

浅忆流年 2024-12-19 08:56:34

看看这是否有帮助:

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,您可以完成它

See if this helps:

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

I never tried to create a custom usercontrol in xaml i always did it programatically, has you can see in this examples, but you can pull it off if you use one ContentControl or one ContentPresenter and then call you UserControl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文