在我的windows phone 8.1的Silverlight项目中,我遇到了问题。我创建了我的自定义控件PullToRefreshPanel,并且在添加了GridView后,Xaml设计器显示错误:
Delegate to an instance method cannot have null 'this'
如何捕获此错误?由于代表错误,我怎么知道?
public class PullToRefreshPanel : ContentControl
{
bool _isPullRefresh;
public event EventHandler PullToRefresh;
public PullToRefreshPanel()
{
DefaultStyleKey = typeof(PullToRefreshPanel);
}
public object RefreshContent
{
get { return GetValue(RefreshContentProperty); }
set { SetValue(RefreshContentProperty, value); }
}
public static readonly DependencyProperty RefreshContentProperty =
DependencyProperty.Register("RefreshContent",
typeof(object),
typeof(PullToRefreshPanel),
new PropertyMetadata("RefreshContent"));
public object PullContent
{
get { return GetValue(PullContentProperty); }
set { SetValue(PullContentProperty, value); }
}
public static readonly DependencyProperty PullContentProperty =
DependencyProperty.Register("PullContent",
typeof(object),
typeof(PullToRefreshPanel),
new PropertyMetadata("PullContent"));
public double PullRange
{
get { return (double)GetValue(PullRangeProperty); }
set { SetValue(PullRangeProperty, value); }
}
public static readonly DependencyProperty PullRangeProperty =
DependencyProperty.Register("PullRange",
typeof(double),
typeof(PullToRefreshPanel),
new PropertyMetadata(40.0, PullRangeChangeEvent));
private static void PullRangeChangeEvent(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var range = (double)e.NewValue;
d.SetValue(InvisiblePullRangeProperty, range * 5);
}
public double InvisiblePullRange
{
get { return (double)GetValue(InvisiblePullRangeProperty); }
protected set { SetValue(InvisiblePullRangeProperty, value); }
}
public static readonly DependencyProperty InvisiblePullRangeProperty =
DependencyProperty.Register("InvisiblePullRange",
typeof(double),
typeof(PullToRefreshPanel),
new PropertyMetadata(200.0));
void UpdateView()
{
var grid = (Grid)GetTemplateChild("PullGrid");
ScrollViewer.ChangeView(null, grid.ActualHeight, null);
var contentgrid = (Grid)GetTemplateChild("ContentGrid");
contentgrid.SetValue(HeightProperty, ScrollViewer.ActualHeight);
contentgrid.SetValue(WidthProperty, ScrollViewer.ActualWidth);
UpdateTransform();
}
void UpdateStates(bool useTransitions)
{
VisualStateManager.GoToState(this, ScrollViewer.VerticalOffset == 0.0 ? "Refresh" : "Pull", useTransitions);
}
void UpdateTransform()
{
var element = (UIElement)GetTemplateChild("StackPanel");
var transform = element.RenderTransform as CompositeTransform ?? new CompositeTransform();
transform.TranslateY = (ScrollViewer.VerticalOffset - InvisiblePullRange) * 0.8;
element.RenderTransform = transform;
}
protected virtual void OnPullToRefresh(EventArgs e)
{
if (PullToRefresh != null) PullToRefresh(this, e);
}
protected override void OnApplyTemplate()
{
ScrollViewer = (ScrollViewer)GetTemplateChild("ScrollViewer");
ScrollViewer.SizeChanged += (s, e) => UpdateView();
ScrollViewer.ViewChanged += ScrollViewer_ViewChanged;
Loaded += (s, e) => UpdateView();
base.OnApplyTemplate();
}
void ScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
UpdateStates(true);
UpdateTransform();
Debug.WriteLine(ScrollViewer.VerticalOffset);
if (ScrollViewer.VerticalOffset != 0.0)
_isPullRefresh = true;
if (e.IsIntermediate)
return;
if (ScrollViewer.VerticalOffset == 0.0 && _isPullRefresh)
{
OnPullToRefresh(new EventArgs());
}
_isPullRefresh = false;
var grid = (Grid)GetTemplateChild("PullGrid");
ScrollViewer.ChangeView(null, grid.ActualHeight, null);
}
private ScrollViewer ScrollViewer { get; set; }
}
和控制风格:
<Style TargetType="controls:PullToRefreshPanel">
<Setter Property="FontSize" Value="24" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:PullToRefreshPanel">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="VisualStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Pull">
<Storyboard>
<FadeInThemeAnimation TargetName="PullContent" />
<FadeOutThemeAnimation TargetName="RefreshContent" />
</Storyboard>
</VisualState>
<VisualState x:Name="Refresh">
<Storyboard>
<FadeInThemeAnimation TargetName="RefreshContent" />
<FadeOutThemeAnimation TargetName="PullContent" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ScrollViewer x:Name="ScrollViewer"
HorizontalScrollMode="Disabled"
VerticalScrollBarVisibility="Hidden"
ZoomMode="Disabled">
<StackPanel x:Name="StackPanel">
<Grid Name="PullGrid" Height="{TemplateBinding InvisiblePullRange}">
<Grid Height="{TemplateBinding PullRange}" VerticalAlignment="Bottom">
<ContentControl x:Name="RefreshContent"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{TemplateBinding RefreshContent}" />
<ContentControl x:Name="PullContent"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{TemplateBinding PullContent}" />
</Grid>
</Grid>
<Grid x:Name="ContentGrid">
<ContentPresenter />
</Grid>
</StackPanel>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我使用Visual Studio 2015和Windows Phone 8.1 XAML项目。 有什么想法吗?
更新: 也许这是一个bug Xaml Designer?该应用程序工作正常。仅在Xaml Designer中出错。 我从控制代码中删除了所有引用EventHandler PullToRefresh并且错误仍然存在。 并且不断出现这个错误: