Behaviors in UWP (Event trigger command error - c#

SCENERIO:
I have a custom control where the control template has a grid. I want to add a behavior to the grid tapped event.
WHAT I HAVE DONE:
I have installed the Nuget package for managed UWP behaviors
Install-Package Microsoft.Xaml.Behaviors.Uwp.Managed
In the custom control's resource dictionary, I have the following xml namespaces referenced along with other necessary namespaces
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
Then in the custom control, in the grid block, I have the following
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<core:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=CustomCommand}"/>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
ISSUES:
The core:InvokeCommandAction has the blue squiggly line with this error
The type EventTriggerBehavior does not
support direct content
STEPS TO REPRODUCE:
Create a custom control. Nothing special,maybe just a grid. Add the EventTriggerBehavior as shown above to the grid. Use the created control on a page in your application
This right here is where I'm stuck at. I've done a lot of Google searches and just can't find a way out of this error.
Any help and code correction will be appreciated

The core:InvokeCommandAction has the blue squiggly line with this error
The type EventTriggerBehavior does not support direct content
I made a basic demo and reproduced this problem. It is a XAML Designer issue. After building the project, this error is gone. So please make sure your codes are correct and go ahead build your project.

Related

Use certain style only when the Fall Creators Update is detected

As an example, I would like to apply the ButtonRevealStyle to my button:
<Button Style="{StaticResource ButtonRevealStyle}" Grid.Column="1" Width="38" ... />
This will work, but of course only on a device with the Fall Creators Update installed. How do I disable this for all previous versions of W10?
I know I can use .IsApiPresent() in the code-behind when I want to check for a specific Windows Api but in this case this doesn't seem to be the preferred/recommended solution and I'd like to stick to just XAML for this. Doing it in C# requires referencing every single control with that style in code-behind and manually assigning the style if it's present. I'm pretty sure this is not the best solution in this day and age, where you can set up responsive and animated layouts solely in XAML. Besides, if the button was in a ListView.ItemTemplate just accessing each control would require a few solid lines of code. Not to mention the check itself
Is it possible? Am I missing something?
Edit: Turns out it is possible, and I totally was missing something. Conditional XAML can easily be done and isn't that complex all things considered. It's just a matter of setting a custom namespace in the file (pointing to the same resource as the 'root' namespace, just with the `IsApiContractPresent" check at the end. Yes, it is possible to use that in XAML.
After setting the custom namespace you can then specify attributes that will only be aplied when the certain API is present on the End-User's device. Example:
xmlns:fcu="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,5)"
...
<Button fcu:Style="{StaticResource ButtonRevealStyle}" Grid.Column="1" Width="38" ... />
This will result in the button getting Reveal only on PCs with Fall Creators Update and the previous versions won't be throwing an error.
More info: https://learn.microsoft.com/en-us/windows/uwp/debug-test-perf/conditional-xaml
You people clearly like your downvote button a bit too much.
Turns out it is possible, and I totally was missing something. Conditional XAML can easily be done and isn't that complex all things considered. It's just a matter of setting a custom namespace in the file (pointing to the same resource as the 'root' namespace, just with the `IsApiContractPresent" check at the end. Yes, it is possible to use that in XAML.
After setting the custom namespace you can then specify attributes that will only be aplied when the certain API is present on the End-User's device. Example:
xmlns:fcu="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,5)"
...
<Button fcu:Style="{StaticResource ButtonRevealStyle}" Grid.Column="1" Width="38" ... />
This will result in the button getting Reveal only on PCs with Fall Creators Update and the previous versions won't be throwing an error.
More info: https://learn.microsoft.com/en-us/windows/uwp/debug-test-perf/conditional-xaml

StaticResource is null even though it's defined in XAML?

I've got a bit of a niche scenario which I'm fully not expecting anyone to solve, but thought I'd give it a shot:
I have a Windows Forms User Control, which contains a single ElementHostcontrol that has it's Child property set to a WPF / XAML user control.
I'm getting some strange behaviour whereby the XAML contains something that looks like this:
<UserControl>
<UserControl.Resources>
<somenamespace:myresource x:Key="foo" />
</UserControl.Resources>
<Grid DataContext="{StaticResource foo}">
...
</Grid>
</UserControl>
I get a XamlParseException at runtime, asking me to Provide a value, even though it's clearly defined above.
I can see InitializeComponent() is being called, but that's as far as I can step.
In my code behind, in the WPF / XAML user control, if before InitializeComponent() is called I add:
this.Resources["foo"] = new myresource() everything works great again.
Just to confuse matters even further, this only happens when I use Visual Studio 2015 to compile the code - compiling using VS2013 (on the same machine/no solution changes whatsoever) works perfectly.
Has anyone got any ideas, or even helps on how to debug this?

How to get an icon in stackPanel in a XAML file

I am new to XAML and C#
I have an icon created already in a project and and I have to use this icon whenever I select one of the option from the dropdown menu.
I made a stackpanel in XAML file
<StackPanel Name="stackPanelforIcon">
</StackPanel>
In the code behind file I have different cases for the dropdown menu.
case IconOnSelect:
?????? = IconList.NewIcon;
This NewIcon is the one already created and I am using the source also for this
using IconProject.Iconlists;
On writing IconList.NewIcon I am not getting any error, it is referenced correctly.
What should I write at ?????? to reference it. Is there any other way apart from using stackPanel to include an icon
A StackPanel cannot show an icon on it's own. You need a control for it, for example an Image.
<StackPanel Name="stackPanelforIcon">
<Image x:Name=theImage" />
</StackPanel>
Then you can use your Icon in your code behind like this:
this.theImage.Source = IconList.NewIcon;
You may need to convert your value, you never said what type it actually is.
Please note that using code-behind is not the preferred way with WPF. Using MVVM is way easier and more natural working with WPF, using code-behind you will fight WPF all the way. Using MVVM, this could be:
<StackPanel Name="stackPanelforIcon">
<Image Source="{Binding CurrentImage}" />
</StackPanel>
with your ViewModel having a property called CurrentImage that you would set when you want to change it. Don't forget to implement INotifyPropertyChanged for the changes to take effect though.

Can't use WrapPanel in SilverLight Application

I'm building my first Silverlight application and I'm attempting to use a WrapPanel in one of my views. However I am getting the following error.
Error 1 The name "WrapPanel" does not exist in the namespace
"http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit".
My code:
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
...
<toolkit:WrapPanel Height="657" Width="657" />
Do I need to install a package or something? If so, how?
Kindly refer to this link ::
WrapPanel
Actually the WrapPanel control is not a part of Silverlight rather it is a part of Silverlight Toolkit. Before you can use a WrapPanel control, you must download the Silverlight Toolkit. After that you need to add a reference to an assembly. You will get Microsoft.Windows.Controls.dll assembly from the folder where you installed the Silverlight Toolkit. Now, you have to import the Microsoft.Windows.Controls namespace to the page. Once you type xmlns= in your page, you will see Microsoft.Windows.Controls listing in Intellisense.
<Application
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"
x:Class="Demo.App"
xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls">
<Application.Resources>
<!-- Resources scoped at the Application level should be defined here. -->
<ItemsPanelTemplate x:Key="ExamplePanal">
<controls:WrapPanel/>
</ItemsPanelTemplate>
</Application.Resources>
The above example "xmlns:controls="clr-namespace:Microsoft.Windows.Controls" after adding this dll then the WrapPanel added to the Intellisense. while typing controls: intellisense show WrapPanal in the list.
See the below code here I am adding ExamplePanal.
<Control
ItemsPanel="{StaticResource ExamplePanal}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
I think this may help you..
Thank You
Jom George

How to handle closing window when pressing close icon on the top-right in WPF/MVVM?

I am able to bind buttons and menu items with ICommand and close the windows.
It is exactly as described in the tutorial WPF Apps With The Model-View-ViewModel Design Pattern - via the Command property accessible in XAML.
But it is not described or implemented in the tutorial how to close by pressing the standard 'Close' icon on the top-right of the window. I need to perform some clean up in my application.
My question is how to bind a Command to the close event, so that it is executed when the user presses the close icon (not buttons or menu items - I know how to manage such cases).
How should this be handled to avoid violating the MVVM approach?
Thanks!
The MVVM Light Toolkit contains a behaviour called EventToCommand, which gives you an easy way to bind a command to an event.
The following XAML snippet shows an example of how to get a command called "CloseCommand" to execute when the window's Closed event is raised:
<Window x:Class="EventToCommand.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
Title="MainWindow" Height="300" Width="500">
<!-- Make sure to put this tag directly inside the Window,
and not inside a child element, since it is the Windows that has the Closed event -->
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closed">
<cmd:EventToCommand Command="{Binding CloseCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<!-- Windows contents -->
</Window>
To get access to the EventToCommand behaviour, you need to download MVVM Light Toolkit from the project downloads page, and then reference the following DLLs:
GalaSoft.MvvmLight.dll
GalaSoft.MvvmLight.Extras.dll
System.Windows.Interactivity.dll
That is all that is needed.
Further instructions of how to get started with the toolkit can be found here.
I would bind a Command to the Application's Exit event
I like using the AttachedCommand behavior found here for binding Commands to Events, although I know you can also accomplish the same thing using Blend's Interaction Triggers.

Categories