【发布时间】:2014-09-17 08:53:06
【问题描述】:
我遇到了一个有趣的 UI 问题。我有一个来自 Telerik 的 RadBusyIndicator 包裹在 UserControl 中(如果 Telerik 仍然存在内存泄漏,则可以轻松切换到 Windows 繁忙指示器)。当我将内容放入控件时,如果它在包装器控件的开始和结束标记之间有一个 ContentControl 以外的任何内容,则具有 x:Name 属性的所有内容在后面的代码中为空,并在页面时导致异常已加载。
这是代码的相似之处,已删除名称以保护无辜者。
xaml...
<UserControl>
<Grid x:Name="Indicator">
<telerik:RadBusyIndicator x:Name="BusyIndicator" IsBusy="{Binding Path=IsStatusBusy, Mode=TwoWay}" BusyContent="{Binding Path=WaitingContent, Mode=TwoWay}" Content="{Binding Path=UserContent, Mode=TwoWay}"/>
</Grid>
</UserControl>
还有后面的代码……
[ContentProperty("UserContent")]
public partial class CustomBusyIndicator : UserControl
{
public CustomBusyIndicator()
{
InitializeComponent();
Indicator.DataContext = this;
}
public UIElement UserContent
{
get { return (UIElement)GetValue(UserContentProperty); }
set { SetValue(UserContentProperty, value); }
}
private static readonly DependencyProperty UserContentProperty = DependencyProperty.Register("PageContent",
typeof(UIElement), typeof(CustomBusyIndicator), new PropertyMetadata(null));
private static readonly DependencyProperty WaitingContentProperty = DependencyProperty.Register("WaitingContent",
typeof (object), typeof (CustomBusyIndicator), new PropertyMetadata(null, OnWaitingContentChanged));
private static void OnWaitingContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{}
private static readonly DependencyProperty IsStatusBusyProperty = DependencyProperty.Register("IsStatusBusy",
typeof (bool), typeof (CustomBusyIndicator), new PropertyMetadata(false, OnIsStatusBusyChanged));
private static void OnIsStatusBusyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{}
public bool IsStatusBusy
{
get { return (bool) GetValue(IsStatusBusyProperty); }
set { SetValue(IsStatusBusyProperty, value); }
}
public object WaitingContent
{
get { return GetValue(WaitingContentProperty); }
set { SetValue(WaitingContentProperty, value); }
}
}
我就是这样用的.....
<CustomBusyIndicator IsStatus={Binding IsBusy}>
<CustomBusyIndicator.WaitingContent>
<TextBlock Text="Loading..." Foreground="Black" />
</CustomBusyIndicator.WaitingContent>
<Grid>
.
.
.
.
</Grid>
</CustomBusyIndicator>
有什么想法吗?提前感谢您的帮助!
编辑
我现在已经确定似乎是 x:Name 导致了这些问题。在调用InitializeComponent() 后,它们在后面的代码中为空。
【问题讨论】:
-
据我了解,这与
x:Name无关。等一下,我会在答案部分写一个正确的答案。
标签: c# silverlight xaml telerik