Justin
Justin

Reputation: 6559

Silverlight Start up life cycle and binding

This is the first time I have used Silverlight and as such, I am new to the whole xaml markup style. I am building a GIS application using a silverlight library provided by ESRI.

From my understanding, when the XAML page is parsed from top to bottom, the objects are created in the order. Is this correct? I have an esri Map object created on line 38 of my mainpage and then on line 247 of my mainpage, I create a DrawControl (a drawing control I made myself).

Part of how the objects in drawing tool works is that it is created by passing the map object to the constructor. With my Map object with name x:Name="Map", I have the following for my drawcontrol:

<local:DrawRootControl x:Name="DrawRoot" Height="152" Margin="216,10,0,0" Grid.Row="1" VerticalAlignment="Top" Visibility="Collapsed" map="{Binding ElementName=Map}"/>

Then in my control, I have this in the code behind:

public static readonly DependencyProperty mapProperty = DependencyProperty.Register
        (
             "map",
             typeof(Map),
             typeof(DrawRootControl),
             null
        );

    public Map map
    {
        get { return (Map)GetValue(mapProperty); }
        set { SetValue(mapProperty, value); }
    }

..........

public DrawRootControl()
    {
        // Required to initialize variables
        InitializeComponent();

        MyDrawObject = new Draw(map)
        {
            LineSymbol = CanvasDraw.Resources["DrawLineSymbol"] as LineSymbol,
            FillSymbol = CanvasDraw.Resources["DrawFillSymbol"] as FillSymbol
        };
        MyDrawObject.DrawComplete += MyDrawObject_DrawComplete;             
    }

When I am debugging, my map object in my constructor is null. I thought that if map is created earlier in the mainpage and then passed when I do that binding, it would not be null and would be initialized and created. Maybe I am doing binding incorrectly? I don't really understand the binding thing entirely.

Any help would be appreciated.

Upvotes: 0

Views: 337

Answers (1)

Leblanc Meneses
Leblanc Meneses

Reputation: 3091

From my understanding, when the XAML page is parsed from top to bottom, the objects are created in the order. Is this correct?

yes top to bottom like html. Example:

<Grid x:Name="LayoutRoot" Background="White">
    <Rectangle Fill="#FFE53400" Height="132"  />
    <Rectangle Fill="#FF0000E5" Height="132" Margin="0,51,0,0" />
</Grid>

Part of how the objects in drawing tool works is that it is created by passing the map object to the constructor.

If you are dependent to another UI element you will need to implement the callback to draw your control when the DependencyProperty has changed. In this example replace Title with Map

    public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }

    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("Title", typeof(string), typeof(ComparisonReport), new PropertyMetadata(null, OnTitleChanged));

    private static void OnTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var o = d as ComparisonReport;
        if (o != null && e.NewValue != null)
        {
            var n = ((ComparisonReport)d);
            n.RadChart1.DefaultView.ChartArea.AxisX.Title = String.Format("{0} Comparison", e.NewValue);
        }
    }

If you have written a custom control you can wait till OnApplyTemplate() at which point you can locate the part (esri map object) by name. GetTemplateChild you can then attach to the esri events that affect your custom drawing.

Upvotes: 1

Related Questions