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.
Related
I wanted to handle MainWindow's Closing event in one of my ViewModel. This ViewModel belongs to a View which is displayed on top of the MainWindow. I wanted to save values of some properties present in that ViewModel while/before closing of the application.
I am using MvvmCross to implement MVVM pattern. So I tried overriding ViewDisappearing, ViewDisappeared and ViewDestroy methods, but they are only getting called when View get switched, but not when application/window closes.
While searching for this requirement, I could only find this answer: https://social.msdn.microsoft.com/Forums/vstudio/en-US/477e7e74-ccbf-4498-8ab9-ca2f3b836597/how-to-know-when-a-wpf-usercontrol-is-closing?forum=wpf , which is close to my requirement, but needs to be implemented in code-behind.
Can anyone please help me in achieving this in MVVM/MvvmCross in WPF?
I had some experience to implement events by MVVM maybe it can help you.
As I know there is a package on Microsoft.Xaml.Behaviors.Wpf that will help to get the events and execute a command
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<i:InvokeCommandAction Command="{Binding ClosingWindow}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
and if you want to execute a command in other view models you can use a static command
xmlns:local="clr-namespace:App.Views.Windows"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<i:InvokeCommandAction Command="{x:static local:ViewModel.ClosingWindow}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
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>
I am using Caliburn Micro for a Windows Phone app. I have a hyperlink control for which I want to bind the click event to my View Model. Below is the sample code
XAML, MyPage.xaml
<TextBlock>
<Run>Got to</Run>
<Hyperlink micro:Message.Attach="[Event Click] = [Action OpenAnotherPage]">
My Page</Hyperlink><Run Text="."></Run></TextBlock>
ViewModel MyPageViewModel.cs
public void OpenAnotherPage()
{
// some code
}
When I tap on the link, I get an exception
System.Exception: No target found for method
What could be the problem?
Update 1: Tried setting micro:Action.TargetWithoutContext="{Binding ElementName=MyPage, Path=DataContext}" on the Hyperlink control, but it didn't work
Give the TextBlock a name and use that as the ElementName since you have the Hyperlink nested into that control. Then Update1 should work.
Try following XAML (untested):
<Hyperlink>My Page
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ec:CallMethodAction TargetObject="{Binding}" MethodName="OpenAnotherPage" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Hyperlink>
The namespaces are following (you'll need to reference those 2 assemblies, they're by Microsoft and shipped with MS Expression Blend)
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ec="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
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>
Does exist a similar EventToCommand (behavior's) on Android?
Example Silverlight/WPF:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<Commanding:EventToCommand Command="{Binding DataContext.EditEventTypeCommand,
RelativeSource={RelativeSource AncestorType=telerik:RadGridView}}"
CommandParameter="{Binding}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
If you are using mvvmcross, then bindings exist for all events which have EventHandler signatures - so you can just use bindings like:
'Click':{'Path':'MyViewModelCommand'}
There are plenty of examples of this in the samples, plus there are several questions oh here - search for MvvmCross and click.
If you need to bind to an event which has an EventHandler<T> signature then you can add this yourself - see mvvmcross touch command binding in android
I'm not sure I have understood your question (I don't know Silverlight) but you can do as follows:
In your XML:
<Button ...
android:onClick="onMyButtonClicked".../>
In your java file
public void onMyButtonClicked(View sender) { ... }
There are no other ways to specify event handlers in Android XML, I think. You can specify a lot of properties though.