How to add behaviors through code?

Oct 28, 2010 at 5:09 PM

<Image Source="Images/Desert.jpg" x:Name="image1">
          <interactivity:Interaction.Behaviors>
               <WP7:MultiTouchManipulationBehavior IsInertiaEnabled="True" IsRotateEnabled="True" IsTranslateEnabled="True" IsScaleEnabled="True" MinimumScaleRadius="1" MaximumScaleRadius="720"/>
          </interactivity:Interaction.Behaviors>
      </Image>

How can I do this through the code?

Oct 28, 2010 at 5:29 PM

You can use this code:

var multiTouchBehavior = new MultiTouchManipulationBehavior() { IsScaleEnabled = true };
System.Windows.Interactivity.Interaction.GetBehaviors(image1).Add(multiTouchManipulationBehavior);

multiTouchBehavior.Move(new Point(100, 250), 45, 100);
Oct 28, 2010 at 5:49 PM

Thank you for your quick response.

I tried it, but I'm getting an runtime error saying "Invalid parameter".

Oct 28, 2010 at 6:24 PM

I've got it working in the Silverlight sample and WP7 emulator too, can you send me some code for tests?

Dec 4, 2010 at 12:55 PM
Edited Dec 4, 2010 at 1:17 PM

Hi davidezordan,

I'm facing the same problem, the unhanded exception happens here :

File : MultiTouchBehavior.WP7.cs

Message : System.ArgumentException was unhandled : The parameter is incorrect.

Funtion : void TouchFrameReported(object sender, TouchFrameEventArgs e);

Line : _processor.Process(e.GetTouchPoints(AssociatedObject));

How to reproduce this :

<Image x:Name="myImage" Source="{Binding PicturePath}">
    <Custom:Interaction.Behaviors>
        <MultiTouch_Behaviors_WP7:MultiTouchBehavior x:Name="mtbInstance" MinimumScale="1" MaximumScale="10"/>
    </Custom:Interaction.Behaviors>
</Image>

And I'm using a listbox which contains list of pictures, and I bind PicturePath property on selection changed.

On first click it works with no problem, after you get the unhandled exception each time.

Thanks.

Dec 4, 2010 at 2:29 PM

Hi Devix,

please check the 70338 change set in the source code, I've inserted a working sample using data binding.

Be sure to load the solution "SilverlightWP7MultiTouch.sln"

Cheers,

Davide

Dec 4, 2010 at 3:07 PM

Hi davidezordan,

Thank you for your quick reply :) :)

I've checked the 70338 change set, It's mostly the same thing I have done....except that I tested this on WP7 Emulator, there you will get this exception !

Regards,
Devix
Dec 4, 2010 at 3:49 PM

Hi Devix,

check out the 70348 change set, works in the emulator too.

Be sure to reference the last updated behavior as done in the sample.

Thanks,

Davide

Dec 4, 2010 at 5:40 PM
Edited Dec 4, 2010 at 5:45 PM

Hi,

Thank you for this updates ! please find here an example where you will see the error that I'm talking about : download from skydrive .

By the way, you will see also in the sample that the picture goes out from it's container (StackPanel with a blue background).....

Thanks,

Devix

Dec 4, 2010 at 6:15 PM

Hi Devix,

please download your updated sample from here.

I've updated the references and added the correct behavior.

Thanks,

Davide

Dec 4, 2010 at 8:01 PM
Edited Dec 4, 2010 at 8:50 PM

Thank you very much, I really appreciate you help !

Basically, It should work if I reference only the new DLL file instead of including all the projects ?

Regards,

Devix

Dec 4, 2010 at 9:33 PM
Edited Dec 4, 2010 at 9:37 PM

Hi again Davide :)

I founded a solution ! I modified the WP7Only solution (MultiTouchBehavior.WP7.cs) like this :

void TouchFrameReported(object sender, TouchFrameEventArgs e)

{
if (!AssociatedObject.DesiredSize.Equals(new Size(0, 0)))
{
_processor.Process(e.GetTouchPoints(AssociatedObject));

#if DEBUG
HandleDebugInfoAndFingers(
e.GetTouchPoints(RootVisual),
e.GetPrimaryTouchPoint(RootVisual));

#endif
}
}

Now it works without the unhandled exception and just by adding the assembly file to your project !

Thanks,
Devix
Dec 4, 2010 at 10:00 PM

Great!

Will post your fix in the source code, thanks! :)

Davide