0

I try to add a usercontrol in my Windows phone project witch has to display my personnal ads. The control works great with default embeded ads but when i want to add personnal ads from the Windows Phone project, i have 2 different issues.

Method 1 : PROPERTY

    public static PersonalAdsCollection Ads
    {
        get { return myAds; }
        set { myAds = value; }
    }

Method 2 : DEPENDANCY PROPERTY

    public PersonalAdsCollection Ads
    {
        get { return (PersonalAdsCollection)GetValue(AdCollectionProperty); }
        set
        {
            SetValue(AdCollectionProperty, value);
            //this.Dispatcher.BeginInvoke(() => myAds = value);
            //myAds = value;
        }
    }

    public static readonly DependencyProperty AdCollectionProperty =
        DependencyProperty.Register(
            "Ads",
            typeof(PersonalAdsCollection), 
            typeof(AdRotator),
            new PropertyMetadata(null, OnAdsSourceChanged)
        );
    private static void OnAdsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        myAds = (PersonalAdsCollection)e.NewValue;
    }
  //in the constructor :
  //SetValue(AdCollectionProperty, new PersonalAdsCollection());

WINDOWS PHONE PROJECT FOR BOTH

    <myAdRotator:AdRotator
        VerticalAlignment="Bottom" 
        >
        <myAdRotator:AdRotator.Ads>
            <myAdRotator:PersonalAd
                 LocalImageUrl="/Assets/pub480x80.jpg"
                 LinkUrl=""
                />
        </myAdRotator:AdRotator.Ads>
    </myAdRotator:AdRotator>

RESULTS METHOD 1 Visual studio shows the ad in the Windows phone project visual xaml view but the Windows phone project does not compile because it can't find a AdRotator.Ads Property

RESULT METHOD 2 Visual studio shows nothing but the app runs and display the ad embeded in the usercontrol, not the one set on the Windows phone project. The AdRotator.Ads setter and OnAdsSourceChanged are never called.

MY SEARCH I found the subjects : - Bound Property Setter not getting Called - WPF UserControl Dependency Property Setter not triggering

I can't figure how to make it works.

Thank you for your help.

EDIT: With the modified code, it compiles and calls OnAdsSourceChanged after the constructor's SetValue... But it does not care about the Xaml defined in the Windows phone app project :(

Netah
  • 245
  • 4
  • 11
  • Does `PersonalAdsCollection` inherit from IList? – Rohit Vats Jul 01 '14 at 11:59
  • From ObservableCollection – Netah Jul 01 '14 at 12:13
  • In your Method 1 the property must not be static. It should be `public PersonalAdsCollection Ads`, just like in Method 2. – Clemens Jul 01 '14 at 12:15
  • Thanks Clemens. Method 1 still doesn't compile. XamlParserError Collection property '__implicit_items' is null. [Line: 34 Position: 30]. Same error with method 2 with null instead of new PersonalAdsCollection – Netah Jul 01 '14 at 12:43
  • METHOD 2: If I recall correctly, when you set the value of a dependecy property from the XAML, the property does not get set through the setter, instead it is the value of your "AdCollectionProperty" that is set directly. Therefore, the "SetValue" is never called, and your OnAdsSourceChanged method is never called either. Can you put breakpoints in your setter, and in your "OnAdsSourceChanged" method to check that ? – Miiite Jul 01 '14 at 12:53
  • @Miiite i checked with breakpoints before asking the question. Breakpoints are still in place. I am looking at http://msdn.microsoft.com/en-us/library/cc903961(VS.95).aspx to figure out the new error when i go with Clemens' tips on method 2 – Netah Jul 01 '14 at 13:11
  • So, as @Clemens suggested, i changed new PropertyMetadata(null, OnAdsSourceChanged). Then, i added SetValue(AdCollectionProperty, new PersonalAdsCollection()); in the constructor. It runs, call OnAdsSourceChanged just after the constructor but does not care of the Xaml defined in the Windows phone app project. – Netah Jul 01 '14 at 13:49
  • I can't test this right now, but my guess is that the `PersonalAd` item from your XAML is added to the existing collection (in contrast to creating a new collection instance). This means that the `OnAdsSourceChanged` callback isn't called, because the value of the `Ads` property has not changed (there was just an element added). So you need to also add a `CollectionChanged` event handler to the `Ads` collection to get notified when elements are added or removed. – Clemens Jul 01 '14 at 14:00
  • Something like shown in [this answer](http://stackoverflow.com/a/19558932/1136211). – Clemens Jul 01 '14 at 14:04
  • Method 1 works but i can't figure out how to use the xaml to update the dependancy property, even with CollectionChanged :( – Netah Jul 01 '14 at 20:02

0 Answers0