EventToCommand cannot be added to TriggerActionCollection - c#

I'm working on a WPF project using MVVM Light and trying to use an EventToCommand, but I get an error message saying:
A value of type 'EventToCommand' cannot be added to a collection or dictionary of type 'TriggerActionCollection'."
I'm using .NET Framework 4.7.2 and the NuGet library Microsoft.Xaml.Behaviors.Wpf.
I've seen most places talking about http://schemas.microsoft.com/expression/2010/interactivity,
but from what I've understood this is now deprecated, and http://schemas.microsoft.com/xaml/behaviors should be used instead.
On mvvmlight.net's changelog I found this:
EventToCommand -
BL0004.005, The type 'EventToCommand' from assembly 'GalaSoft.MvvmLight.Extras.WPF4' is built with an older version of the Blend SDK, and is not supported in a Windows Presentation Framework 4 project.
I've tried changing the projects target framework version, but it didn't seem to make a difference.
Any help would be appreciated.
<UserControl x:Class="DialogueTree.View.DialogueControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:local="clr-namespace:DialogueTree.View"
xmlns:command="http://www.galasoft.ch/mvvmlight"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="200"
MinHeight="200" MinWidth="200">
<TextBox Text="{Binding Headline}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="LostFocus">
<command:EventToCommand Command="{Binding Source={StaticResource Locator}, Path=DialogueControlVM.EndEditHeadline}"
CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>

The EventToCommand action from MvvmLight is based on the legacy Blend SDK assemblies.
XML namespace http://schemas.microsoft.com/expression/2010/interactivity
CLR namespace System.Windows.Interactivity
These are in fact deprecated and replaced by the open source XAML behaviors that you use. The issue is that these assemblies are incompatible, so you cannot use the actions provided by MvvmLight with the XAML behaviors and vice-versa. I guess MvvmLight will be updated to use these behaviors in the future, too. Here is an open issue on this topic.
However, you do not need EventToCommand, as the XAML behaviors package already contains a command that does the same: InvokeCommandAction. The property names might be different, though.
<b:EventTrigger EventName="LostFocus">
<b:InvokeCommandAction Command="{Binding Source={StaticResource Locator}, Path=DialogueControlVM.EndEditHeadline}"
CommandParameter="{Binding}"/>
</b:EventTrigger>

Related

WPF/XAML compiles but errors show in XAML

With a colleague, we try to make a POC of a WPF application. We created a new project with a page and a resx file to test internationalization (we are French). The application work well, no error on the compile and the resx file give good data.
but in the XAML file, we have a lot of errors.
Here is the XAML header:
<Window x:Class="MAPPrintProcessEditor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:res="clr-namespace:MAPPrintProcessEditor.Properties"
mc:Ignorable="d"
Title="MAP Print Process Editor"
WindowStartupLocation="CenterScreen"
Height="{Binding SystemParameters.PrimaryScreenHeight}" Width="{Binding SystemParameters.PrimaryScreenWidth}">
Here, is a line marked in error:
<MenuItem Header="{x:Static res:Resources.MenuItemHeaderClose}">
<MenuItem.Icon>
<Image Source="pack://application:,,,/Resources/Close-16.png"/>
</MenuItem.Icon>
</MenuItem>
Visual Studio underline only this part:
Header="{x:Static
And finally, here is the error message:
The name "Resources" does not exist in the namespace "clr-namespace:PrintProcessEditor.Properties"
What is wrong in this code?
We use VS2017 and .Net 4.6.1.
If the you can build correctly than just ignore that message and use BLEND to design your app.

Prism 6 and WPF4

I'm build simple application with PRISM 6 and WPF. I want to pass EventArgs to my ViewModel, so far I have this code:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseWheel">
<prism:InvokeCommandAction Command="{Binding RotaryTableCommand}" TriggerParameterPath="MouseWheel"/>
</i:EventTrigger>
</i:Interaction.Triggers>
The problem I have is in prism:InvokeCommandAction and I got this message: "The type 'invokeCommandAction' from assembly 'Prism.Wpf' is built with an older verision of Blend SDK, and is not supported in WPF4 project.
I realy don't know what should I do.

Why cannot add instance to a collection of type 'Microsoft.Xaml.Interactivity.BehaviorCollection' (UWP compatibility)?

I'm migrating my old project to UWP. And I have some compatibility problem in my xaml code about interaction behaviors. Now the following code won't work
<interactivity:Interaction.Behaviors>
<!--//TODO - UWP Compatibility break-->
<behaviors:StatusBarBehavior
BackgroundOpacity="1"
BackgroundColor="{StaticResource XX.Color.WG}"
ForegroundColor="{StaticResource XX.Color.TW}"
IsProgressVisible="{Binding SyncStatusManager.DataTransferInProgress}"
ProgressText="{Binding LocalizedResources.SyncInProgress, Source={StaticResource LocalizedStrings}}" />
</interactivity:Interaction.Behaviors>
The error information is: Cannot add instance of type 'xxx.App.Behaviors.StatusBarBehavior' to a collection of type 'Microsoft.Xaml.Interactivity.BehaviorCollection' any idea why this will happen?

WP8 MvvmLight namespace missing and EventToCommand doesn't exist

I am using MVVM Light libraries only (from Nuget package) in my Windows Phone 8 project and I want to use EventToCommand in ToggleSwitch. I have these lines of codes:
<toolkit:ToggleSwitch x:Name="LockSwitch"
IsChecked="{Binding IsLock, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Toggled">
<Command:EventToCommand
Command="{Binding DataContext.NavigateToArticleCommand, ElementName=LayoutRoot}"
CommandParameter="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</toolkit:ToggleSwitch>
The problem is that VS shows errors:
Error 1 The name "EventToCommand" does not exist in the namespace
"clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8".
Error 2 The type 'Command:EventToCommand' was not found. Verify that
you are not missing an assembly reference and that all referenced
assemblies have been built.
Error 3 The tag 'EventToCommand' does not exist in XML namespace
'clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8'.
I have lines above in file Styles.xaml which is a ResourceDictionary and ToggleSwitch is part of a DataTemplate. I am including MvvmLight library using this line:
xmlns:Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8"
What's wrong? Why I get that error? I was trying to use google but I couldn't find a solution.
The reference that you use to include the command is wrong. The correct reference is
xmlns:Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
There's a trick to obtain this reference without writing a single line of code.
After you have downloaded the MvvmLight nuget package, compile your project and then open your xaml file in Expression Blend.
Then click the Assets icon on the left toolbar (the bottom one) and start typing "eventtocommand" (see picture below).
Once you see EventToCommand appear in the Assets panel, drag and drop it on top of your ToggleSwitch. That's it! The reference will be added into your xaml automatically as well as the actual command code.
Why not use Microsoft.Behaviors SDK ? (references, add reference, extensions, behavior sdk) Not sure but I think EventTrigger and mvvm light EventToCommand is deprecated now (because of behaviors sdk).
Code sample with Behaviors.SDK:
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
<toolkit:ToggleSwitch x:Name="LockSwitch"
IsChecked="{Binding IsLock, Mode=TwoWay}">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Toggled">
<core:InvokeCommandAction Command="{Binding command}" CommandParameter="{Binding param}"/>
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</toolkit:ToggleSwitch>

Binding Tapped event

I'm writing a Windows Phone 8.1 app and I would like to get binding Tapped event (e.g. for Grid) capability. I read many articles and answers, but all solutions don't work in my situation. Does it exist any alternative to triggers (System.Windows.Interactivity) or Command for WP 8.1?
Thanks for interest, but I found solution. I probably was too tired and I forgot add reference to Behaviors SDK. It works great now.
<Page
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core" ...>
And simple Command usage:
<SomeControl ...>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<core:InvokeCommandAction Command="{Binding MyCommand}" />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</SomeControl>

Categories

Resources