Cimbalino-Toolkit icon indicating copy to clipboard operation
Cimbalino-Toolkit copied to clipboard

Error in UWP app

Open dmitry-lt opened this issue 9 years ago • 15 comments

Windows 10 UWP app, submitted to the store, produces an error:

Cannot add instance of type 'Cimbalino.Toolkit.Behaviors.MultiBindingBehavior' to a collection of type 'Microsoft.Xaml.Interactivity.BehaviorCollection'.

On dev machine (in debug mode) everything works fine.

Lib version 2.4.1

dmitry-lt avatar Apr 03 '17 12:04 dmitry-lt

Stacktrace of the error (only happens in release mode)

System.NullReferenceException: Arg_NullReferenceException at Cimbalino.Toolkit.Behaviors.MultiBindingBehavior.Update() in D:\Users\Pedro\Dropbox\Projects\Cimbalino.Toolkit\src\cimbalino.toolkit (wp8)\behaviors\MultiBindingBehavior.cs:line 212 at Cimbalino.Toolkit.Behaviors.MultiBindingBehavior.OnAttached() in D:\Users\Pedro\Dropbox\Projects\Cimbalino.Toolkit\src\cimbalino.toolkit (wp8)\behaviors\MultiBindingBehavior.cs:line 166 at Microsoft.Xaml.Interactivity.Behavior.Attach(DependencyObject associatedObject) at Microsoft.Xaml.Interactivity.BehaviorCollection.VerifiedAttach(DependencyObject item) at Microsoft.Xaml.Interactivity.BehaviorCollection.BehaviorCollection_VectorChanged(IObservableVector1 sender, IVectorChangedEventArgs eventArgs) at Windows.Foundation.TypedEventHandler2.Invoke(TSender sender, TResult args) at __Interop.Intrinsics.HasThisCall__22[TArg0](Object __this, IntPtr pfn, Object arg0, TArg0 arg1) at __Interop.ReverseComStubs.Stub_5[TArg0,TArg1](Object __this, Void* unsafe_d, Void* unsafe_e, IntPtr __methodPtr)

dmitry-lt avatar Apr 03 '17 13:04 dmitry-lt

The error seems to disappear if I uncheck Project properties -> Build -> Optimize code

dmitry-lt avatar Apr 03 '17 13:04 dmitry-lt

What version of Visual Studio are you using? Are you using the correct UWP XAML Behaviors (the one from this NuGet)?

pedrolamas avatar Apr 03 '17 13:04 pedrolamas

Visual Studio 2017.

In my project.lock.json there is

"Cimbalino.Toolkit/2.4.1": { "type": "package", "dependencies": { "Cimbalino.Toolkit.Core": "2.4.1", "Microsoft.Xaml.Behaviors.Uwp.Managed": "1.1.0" },

Only Cimbalino.Toolkit is referenced directly in the project References

dmitry-lt avatar Apr 03 '17 14:04 dmitry-lt

Please try to manually add the Microsoft.Xaml.Behaviors.Uwp.Managed package to your project and see if that makes any difference.

pedrolamas avatar Apr 03 '17 14:04 pedrolamas

Manually adding Microsoft.Xaml.Behaviors.Uwp.Managed package does not make any difference.

If "Compile with .NET Native tool chain" and "Optimize code" are checked, I get this error. Unchecking those is a workaround.

dmitry-lt avatar Apr 03 '17 18:04 dmitry-lt

~Oh, I just noticed you're using the MultiBindingBehavior: That's not available in UWP!!~

Please ensure you add Cimbalino.Toolkit to all your projects that actually require it, it should then pick the correct assembly according to the platform!

pedrolamas avatar Apr 03 '17 19:04 pedrolamas

How come it is not available? I've been using it for quite some time. What should I use instead?

For example, this article says it's available:

http://www.damirscorner.com/blog/posts/20160221-MultibindingInUniversalWindowsApps.html

dmitry-lt avatar Apr 03 '17 21:04 dmitry-lt

Sorry, though I wrote MultiBindingBehavior I was thinking on MultiApplicationBarBehavior; it's been a long day, so please just disregard my last comment...

I now start to remember this better, and you are correct when you say that this issue goes away when disabling .NET Native: the problem is that this behavior relies heavily on reflection and as such, can have .NET Native problems.

I need to research this further, but I'd advise in using compiled bindings (x:bind) function support as an alternative!

pedrolamas avatar Apr 03 '17 21:04 pedrolamas

@dmitry-lt can you share the XAML bit you have with this MultiBindingBehavior ?

pedrolamas avatar Apr 11 '17 09:04 pedrolamas

<Image VerticalAlignment="Top" HorizontalAlignment="Center" Margin="0 5 0 5" Width="50" Height="50">
    <interactivity:Interaction.Behaviors>
        <behaviors:MultiBindingBehavior Converter="{StaticResource IconConverter}" PropertyName="Source">
            <behaviors:MultiBindingItem Value="{Binding Item}" />
            <behaviors:MultiBindingItem Value="{Binding Item.Enabled}" />
            <behaviors:MultiBindingItem Value="{Binding Item.Status}" />
            <behaviors:MultiBindingItem Value="{Binding Item.Type}" />
        </behaviors:MultiBindingBehavior>
    </interactivity:Interaction.Behaviors>
</Image>

dmitry-lt avatar Apr 11 '17 09:04 dmitry-lt

Ok, the solution will be to add a runtime exception so that we can use reflection over Image.Source property.

On the solution explorer, there's a Properties node in the UWP project for your app - expand it!

Inside you should find a Default.rd.xml file. Find where it says "" and add the following line below it:

<Type Name="Windows.​UI.​Xaml.​Controls.Image" Dynamic="Required All" Serialize="Require All" />

Rebuild the app with the "Compile with .NET Native tool chain" and "Optimize code" checked, and check again!

pedrolamas avatar Apr 11 '17 10:04 pedrolamas

Hmm. There is no Default.rd.xml in my project. Anyway, I have already changed my code to not use MultiBindingBehavior (and handle it in view model instead).

dmitry-lt avatar Apr 11 '17 10:04 dmitry-lt

Same here, similar XAML, same problem (only when compiling in release mode) cimbalino crash

pulimento avatar May 24 '17 10:05 pulimento

Installing 'Microsoft.Xaml.Behaviors.Uwp.Managed' v2.0.0 manually (instead of v1.1.0) did not help. Using the v2.5.0 version of the toolkit, on a UWP project

pulimento avatar May 24 '17 10:05 pulimento