Jun 28, 2011 at 10:42 PM
Edited Jun 28, 2011 at 10:46 PM
|
I am not sure if this is a memory leak or it's a problem with how I am using the framework. I am using Silverlight 4 and the latest version of Caliburn.Micro, also got the same result using RTW 1.0.
I set up a simple project to see if the problem I was seeing in my main application could be recreated. I have one ViewModel with one method ShowWindow:
[Export(typeof(ToolbarViewModel)), PartCreationPolicy(CreationPolicy.NonShared)]
public class ToolbarViewModel : Screen
{
public static IWindowManager DialogManager { get; set; }
[ImportingConstructor]
public ToolbarViewModel(IWindowManager dialogManager)
{
DialogManager = dialogManager;
}
public void ShowWindow()
{
var vm = new DialogAViewModel();
DialogManager.ShowDialog(vm);
}
}
DialogAViewModel:
public class DialogAViewModel : Screen
{
public DialogAViewModel() {}
public void OK() { TryClose(); }
}
DialogAView:
<controls:ChildWindow x:Class="CaliburnTestEnvironment.Views.DialogAView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
Width="400"
Height="300"
Title="DialogAView">
<Grid x:Name="LayoutRoot"
Margin="2">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock x:Name="Message"
Text="Hello"
Loaded="Message_Loaded"
LayoutUpdated="Message_LayoutUpdated"
Width="75"
Height="23"/>
<Button x:Name="OK"
Content="OK"
Width="75"
Height="23"
HorizontalAlignment="Right"
Margin="0,12,79,0"
Grid.Row="1" />
</Grid>
</controls:ChildWindow>
Codebehind for DialogAView:
public partial class DialogAView : ChildWindow
{
public DialogAView()
{
InitializeComponent();
this.Closed += new EventHandler(DialogAView_Closed);
LayoutUpdated += new EventHandler(DialogAView_LayoutUpdated);
}
void DialogAView_LayoutUpdated(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("**********\tView layout updated...\t\t{0}", GetHashCode());
}
void DialogAView_Closed(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("**********\tView closed...\t{0}", GetHashCode());
}
private void Message_Loaded(object sender, RoutedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("----------\tTextblock Loaded...\t\t\t{0}", Message.GetHashCode());
}
private void Message_LayoutUpdated(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("----------\tTextblock layout updated...\t{0}", Message.GetHashCode());
}
}
The result from the debug window after clicking ShowWindow three times and closing the ChildWindow using the OK button:
INFO: Activating CaliburnTestEnvironment.ViewModels.DialogAViewModel.
---------- Textblock layout updated... 36936550
********** View layout updated... 64479624
---------- Textblock layout updated... 63646052
********** View layout updated... 27440617
---------- Textblock layout updated... 51921052
********** View layout updated... 63658128
---------- Textblock Loaded... 51921052
INFO: Action: OK availability update.
INFO: Action: OK availability update.
INFO: Invoking Action: OK.
********** View closed... 63658128
INFO: Deactivating CaliburnTestEnvironment.ViewModels.DialogAViewModel.
INFO: Closed CaliburnTestEnvironment.ViewModels.DialogAViewModel.
---------- Textblock layout updated... 36936550
********** View layout updated... 64479624
---------- Textblock layout updated... 63646052
********** View layout updated... 27440617
---------- Textblock layout updated... 51921052
********** View layout updated... 63658128
As you can see the previous "closed" views are still in memory and somehow in the visual tree even though the Close method was called in the ChildWindow. I am not sure what I am doing wrong.
|