0

In my not silverlight project for windows phone 8.1 i have problem.I create my custom control PullToRefreshPanel, and after the addition of a GridView, Xaml designer shows an error:

Delegate to an instance method cannot have null 'this'

How to catch this error? How do I know because of Delegates mistake?

enter image description here enter image description here

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

and style for control:

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

I use Visual Studio 2015 and Windows Phone 8.1 XAML project. Any thoughts?

Update: Maybe it is a bug Xaml Designer? The application works fine. Error only in Xaml Designer. I removed from the control code, all references EventHandler PullToRefresh and the error remained. And that's constantly coming out this error: enter image description here

arsenium
  • 571
  • 1
  • 7
  • 20
  • You have to instantiate PullToRefresh eventhandler. It is getting null reference exception. Check this answer http://stackoverflow.com/questions/3752824/delegate-to-an-instance-method-cannot-have-null-this – asitis Jan 28 '16 at 10:58
  • How to do it correctly? The method is assigned through Xaml. Or something I did not understand? In all the examples that I have looked at creating EventHandler uses the same code as I have. I think this error is due to Xaml? – arsenium Jan 28 '16 at 11:40
  • Maybe it is a bug Xaml Designer? The application works fine. Error only in Xaml Designer. – arsenium Jan 30 '16 at 09:14

0 Answers0