【问题标题】:Disappearing UserControl after build构建后消失的用户控件
【发布时间】:2012-12-07 21:26:13
【问题描述】:

问题:我有一个新制作的 UserControl,在父 Grid 中有几个 Telerik 控件,以便能够在整个解决方案中使用它。听起来很简单吧?我创建了 UserControl,让我们调用 My.Project.Controls.Tool 类,然后我尝试使用命名空间 xmlns:Controls="clr-namespace:My.Project.Controls;assembly=My.Project.GlobalUserControlDump" 调用另一个视图,然后通过从方便的花花公子智能感知中轻松选择将其设置在视图中。

这正如预期的那样,我的 UserControl 出现在设计器的单独视图中就好了。所以我采取下一个正常步骤并构建它....一旦构建完成(它做得很好,没有按预期报告错误),小虫子消失了! xaml 当然仍然在视图中,但它从设计器中消失了,并且它没有出现在构建的解决方案中?

我很困惑,我回去了,对 UserControl 进行快速更改,它又出现在设计器中。好的,我认为这一定是侥幸,所以我再次构建它......它再次从设计师和构建的解决方案中消失了?

现在我可以继续可靠地重现这个场景。对 UserControl 稍作改动,它会重新出现在设计器中……然后构建它,它又消失了!

有人能解释一下这个难题吗?使用 Caliburn Micro 在 SL(浏览器内外,但内置浏览器)中运行。任何对这个谜团的洞察力当然都非常感谢,希望另一双眼睛能抓住我的愚蠢。干杯!

为了澄清,这是用户控件中与a previous question 直接相关的内容。

<UserControl
    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:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    mc:Ignorable="d"
    x:Class="My.Project.Controls.DatePicker">

    <Grid Width="90">
           <telerik:RadDateTimePicker 
                                      InputMode="DatePicker" 
                                      SelectedDate="{Binding SelectedDate, Mode=TwoWay}"/>
           <telerik:RadMaskedDateTimeInput
                                           IsClearButtonVisible="False"
                                           FormatString="{}{0:M/d/yy}"
                                           SelectionOnFocus="SelectAll"
                                           Value="{Binding SelectedDate, Mode=TwoWay}"/>
    </Grid>
</UserControl>

然后我会直接在视图上调用它(它将位于 GlobalUserControlDump 项目中,如名称空间所示),一旦将名称空间添加到视图中,它就会在智能感知中按预期显示;

<UserControl 
    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:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro" 
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    x:Class="My.Project.Views.RandomView" 
    xmlns:Controls="clr-namespace:My.Project.Controls;assembly=My.Project.GlobalUserControlDump"
    mc:Ignorable="d">

    <Grid>
            <Controls:DatePicker />
    </Grid>
 </UserControl>

然后我通过暴露我需要的属性;

namespace My
{
    public partial class DatePicker : UserControl
    {
        public static readonly DependencyProperty SelectedDateProperty =
            DependencyProperty.Register("SelectedDate", typeof(DateTime), typeof(DatePicker), new PropertyMetadata(null));

        public DatePicker()
        {
            // Required to initialize variables
            DataContext = this;
        }

        public DateTime SelectedDate
        {
            get { return (DateTime)GetValue(SelectedDateProperty); }
            set { SetValue(SelectedDateProperty, value); }
        }
    }
}

感谢您的关注,我目前是否仍然感到困惑。

【问题讨论】:

  • 以前从未见过 - 你可以发布你的 XAML 来排除类似的情况吗?我也有 Telerik 控件套件
  • 你搞清楚了吗?
  • 任何机会您都可以发布 UC 的完整 XAML 和一些 XAML 以获得 UC 所在的视图(您是使用 ContentControl 和约定绑定它还是只是直接删除 UC到视图上?)
  • 编辑了显示的代码,希望能提供完整的图片。尽管我仍然很好奇为什么会发生这种行为,但我将在这个问题上悬赏。
  • 可能与您的问题无关,但是您的解决方案中是否有更多项目?

标签: c# silverlight xaml user-controls caliburn.micro


【解决方案1】:

您在 UserControl 的构造函数中缺少对 InitializeComponent() 的调用。

【讨论】:

  • 不敢相信我在我的一个构造函数中忘记了 InitializeComponent() - 感谢您的快速修复。
【解决方案2】:

我认为您发布的代码中的命名空间不正确。

通过new PropertyMetadata(null)); 未注册属性更改回调。没有它,我相信绑定将不起作用。您要做的是绑定到用户控件上的属性,当绑定值更改时,您希望设置控件中包含的RadDateTimePicker 上的值。

xaml:

    <Grid Width="90">
      <telerik:RadDateTimePicker x:Name="MyRadDateTimePicker" InputMode="DatePicker" />
    <telerik:RadMaskedDateTimeInput x:Name="MyRadMaskedDateTimeInput"
                                    IsClearButtonVisible="False"
                                    FormatString="{}{0:M/d/yy}"
                                    SelectionOnFocus="SelectAll" />
    </Grid>
</UserControl>

后面的代码:

 using System;
using System.Windows;

namespace SO
{
    public partial class MyDatePicker
    {
        public MyDatePicker()
        {
            InitializeComponent();
        }

        public const string SelectedDatePropertyName = "SelectedDate";

        public DateTime SelectedDate
        {
            get { return (DateTime)GetValue(SelectedDateProperty); }
            set { SetValue(SelectedDateProperty, value); }
        }

        public static readonly DependencyProperty SelectedDateProperty = DependencyProperty.Register(
            SelectedDatePropertyName,
            typeof(DateTime),
            typeof(MyDatePicker),
            new PropertyMetadata(DateTime.Now, OnSelectedDatePropertyChanged));

        private static void OnSelectedDatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((MyDatePicker)d).MyRadDateTimePicker.SelectedDate = (DateTime) e.NewValue;
            ((MyDatePicker)d).MyRadMaskedDateTimeInput.Value = (DateTime)e.NewValue;
        }
    }
}

【讨论】:

  • 该死的德里克,scrum 见。 ;)
  • 为什么绑定失败会导致控件不可见?此外,我不相信设置 null 元数据会破坏所有与依赖属性的绑定。通过删除所有内容并使用您的代码解决了另一个问题。关于是否调用了 InitializeComponent() 或者这只是 c&p 中的错误,我没有得到答复。
  • 将默认绑定值的 DateTime 类型设置为 null 似乎导致了不可见的问题。使用 OP 提供的代码,我不确定数据绑定如何正常工作。
  • 您说的都是对的,但是 Derek 继续进一步揭露了一个我也会在之后立即遇到的问题,包括代码中显示的初始化
猜你喜欢
  • 2018-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-11
  • 2018-09-28
  • 1970-01-01
  • 2017-04-20
相关资源
最近更新 更多