Memory leak with Interaction Triggers in MVVM Light Toolkit Silverlight - memory-leaks

Visual Studio 2012 Project with memory leak
Hello!
I've found a memory leak when using Interaction Triggers in the MVVM Light Toolkit.
I use this xaml
<UserControl x:Class="MemoryLeakTest.MainPage"
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:ignore="http://www.ignore.com"
mc:Ignorable="d ignore"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:mvvm="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL5"
x:Name="control"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Grid x:Name="LayoutRoot">
<ItemsControl ItemsSource="{Binding LeakObjects}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Width="300" BorderThickness="6" BorderBrush="BlueViolet" CornerRadius="3">
<Grid Background="{Binding ColorBrush}" >
<StackPanel>
<Button Command="{Binding ElementName=control, Path=DataContext.Command}" Width="100" Height="40" Content="Tryck!">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<mvvm:EventToCommand Command="{Binding ElementName=control, Path=DataContext.Command}" CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<TextBlock Text="{Binding Text}"/>
</StackPanel>
</Grid>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
and then i rebind the list LeakObjects so that it creates new items.
The old items (xaml) like buttons and text blocks are still in memory and doesn't GC.
If I write
<Button Command="{Binding ElementName=control, Path=DataContext.Command}" Width="100" Height="40" Content="Press!"/>
and use the buttons Command parameter there is no memory leak but if I use the
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<mvvm:EventToCommand Command="{Binding ElementName=control, Path=DataContext.Command}" CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
there is a major leak.
The problem is that there are no Command parameters on grids etc.
The project in the link has this very simple project demonstrating the problem.
Is there a way to circumvent the memory leak? Maybe I'm using it wrong.
It is crucial that I find a way to fix this because this memory leak is all over our application.

I've experienced this leak as well. I've solved it by not using the EventToCommands and rather using just plain event handlers and calling the commands from these methods in page code-behind. It's not that clean, but it works and the page is Garbage Collected as expected.
Or you could use InvokeCommandAction instead, works for me.
http://www.dotnetpatterns.net/entries/21-Memory-Leak-issue-with-EventToCommand-passing-binding-to-RelayCommand

Related

CollectionView SelectionChanged method not triggering when tapping the item directly .NET MAUI

I'm creating a CollectionView in .net MAUI where i'm using Frame control inside the data template. As a result of that, when I tap directly on the item, the SelectionChanged method is not being triggered and will trigger only if I click on the frame border or outside of it. Below sample code and picture. Is this a bug in MAUI or I'm doing something wrong? I had the same setup in Xamarin and it was working with no issues.
XAML
<CollectionView x:Name="scheduleItemsCollection"
SelectionChanged="scheduleItemsCollection_SelectionChanged"
SelectionMode="Single"
>
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical"
Span="2"
/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="5" RowSpacing="0">
<Frame CornerRadius="40" BorderColor="Gray">
<StackLayout Spacing="0" BackgroundColor="White">
<Label Text="{Binding bookingDayArabic}" />
<Label Text="{Binding bookingDateD}" />
<StackLayout Orientation="Horizontal">
<Label Text="{Binding slotsAvailable}" HorizontalOptions="CenterAndExpand" />
<Label Text="عدد الطلبات:"/>
</StackLayout>
</StackLayout>
</Frame>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Yes, it is the case as you said. Thanks for your support and feedback for maui.
I have created a new issue about this problem.
You can follow it up here: https://github.com/dotnet/maui/issues/9567.
Have a nice day.
This worked for me:
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="Section_Tapped"/>
</Frame.GestureRecognizers>

The button is not visible

I have a UWP application, I have added a button but when I debug the application, I can't see the button. If I add a texblock or textBox I can see them, but not the button.
I have set IsEnabled to true and visibility to visible but I am not able to solve the problem.
Also I have tried to click in the place where it should be the button but nothing happens.
This happens when I debug in local machine and when I debug in a emulator of windows phone 10.
Thank you so much.
EDIT: the axml code
<Page
x:Class="SqliteEF7UWP.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SqliteEF7UWP"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Transparent">
<Button x:Name="GetVideos" IsEnabled="True" Visibility="Visible" Background="Aquamarine" Content="Buscar Videos" HorizontalAlignment="Left" Margin="120,98,0,510" VerticalAlignment="Stretch" Click="button_Click"/>
<TextBlock x:Name="textBlock" Foreground="BlueViolet" HorizontalAlignment="Left" Margin="101,59,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="34" Width="132"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="213,59,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top"/>
</Grid>
</Page>
The margin you set for the Button is too aggressive. The object is now "squeezed". For example set Margin to "120,98,0,200" (or simply "120,98") instead of "120,98,0,510" and it should be visible.
BTW using hardcoded margins to setup the user interface is probably not a good idea. Use for example the Grid with columns and rows instead of margins.

How to overdraw on WPF tab control

I am wondering how to draw beyond the boundaries of the standard WPF tab control? I have tried the various ClipToBounds settings and inserted my control within a canvas, which has worked for me with a grid in the past. Sample XAML:
<Window
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" mc:Ignorable="d" x:Class="MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl HorizontalAlignment="Left" Height="50" Margin="90,31,0,0" VerticalAlignment="Top" Width="100">
<Canvas Margin="10,26,-10,-26">
<Ellipse Width="25" Height="25" Fill="Red" Canvas.Left="0" Canvas.Top="0"></Ellipse>
</Canvas>
</TabControl>
</Grid>
This is an old post, but this answer solved the problem for me. You can use this template and just change the Grid's ClipsToBounds to False. https://zamjad.wordpress.com/2010/05/05/use-different-panel-in-tab-control/

Wide Windows phone? What view is this?

I fat fingered something when hitting debug and now I am stuck in this view.
Ive never seen it before, have no idea how I did it and would LOVE to know how to undo it.
Mind you this is a Windows Phone 8 app.
<phone:PhoneApplicationPage
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:UI="clr-namespace:Microsoft.Advertising.Mobile.UI;assembly=Microsoft.Advertising.Mobile.UI"
x:Class="mycuts.MainPage"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar>
<shell:ApplicationBarIconButton x:Name="add" Text="add" IconUri="/images/add.png"/>
<shell:ApplicationBarIconButton x:Name="del" Text="delete" IconUri="/images/minus.png"/>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
<Grid x:Name="LayoutRoot" Background="Transparent">
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="RT APPS" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="My Cuts" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<Grid x:Name="ContentPanel" Margin="0,161,0,0">
<StackPanel Orientation="Vertical">
<ListBox x:Name="cutsList" Height="451"/>
<!--<UI:AdControl ApplicationId="test_client" AdUnitId="Image480_80" HorizontalAlignment="Left" Height="80" VerticalAlignment="Top" Width="480"/>-->
</StackPanel>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
Sometimes the Blend or Visual Studio 2012/2013 Designer can go wacky like that. You can do one of two things:
Close and Reopen the XAML page
Shut down and reopen Visual Studio
Usually the first option fixes it for me, but the second one always does.
Happy coding!

'ToolTip' cannot have a logical or visual parent

I have the following problem. I keep getting 'ToolTip' cannot have a logical or visual parent error when i try to style tooltip for the toggle button. What went wrong? When i take out the tooltip control under ToggleButton.ToolTip it works !
<ToggleButton x:Name="toggle" OverridesDefaultStyle="True" Template="{StaticResource ExpanderToggleButton}" Margin="0,4,0,0" VerticalAlignment="Top" IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
<ToggleButton.ToolTip>
<ToolTip Style="{StaticResource tooltipstyle}">
<TextBlock Background="Transparent"/>
</ToolTip>
</ToggleButton.ToolTip>
</ToggleButton>
If you write code like this using property element syntax, you call already the constructor of the ToolTip class.
<ToggleButton.ToolTip>
<TextBlock></TextBlock>
</ToggleButton.ToolTip>
There is no need to instantiate another ToolTip inside like this...
<ToggleButton.ToolTip>
<ToolTip Style="{StaticResource tooltipstyle}">
<TextBlock Background="Transparent"/>
</ToolTip>
</ToggleButton.ToolTip>
Besides, on my system (using .NET 4.5) there is no error.
It seems that WPF can handle both versions meanwhile as intended by the developer.

Resources