Skip to content

Commit

Permalink
add sample to check memory
Browse files Browse the repository at this point in the history
  • Loading branch information
whistyun committed Jan 5, 2024
1 parent bf5cba0 commit 951ec88
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 1 deletion.
9 changes: 8 additions & 1 deletion MdXaml.sln
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MdXaml.Plugins", "MdXaml.Pl
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MdXaml.AnimatedGif", "MdXaml.AnimatedGif\MdXaml.AnimatedGif.csproj", "{A7666AD6-CB44-4399-AC61-05DE80D65F9D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MdXaml.Full", "MdXaml.Full\MdXaml.Full.csproj", "{0ABF174B-B7EA-4225-BB56-5A6E8C011CFB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MdXaml.Full", "MdXaml.Full\MdXaml.Full.csproj", "{0ABF174B-B7EA-4225-BB56-5A6E8C011CFB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CheckMemoryLeak", "samples\CheckMemoryLeak\CheckMemoryLeak.csproj", "{CC0EBCF6-0EE9-4873-BB6F-6A9F4CF84B79}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -90,6 +92,10 @@ Global
{0ABF174B-B7EA-4225-BB56-5A6E8C011CFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0ABF174B-B7EA-4225-BB56-5A6E8C011CFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0ABF174B-B7EA-4225-BB56-5A6E8C011CFB}.Release|Any CPU.Build.0 = Release|Any CPU
{CC0EBCF6-0EE9-4873-BB6F-6A9F4CF84B79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CC0EBCF6-0EE9-4873-BB6F-6A9F4CF84B79}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC0EBCF6-0EE9-4873-BB6F-6A9F4CF84B79}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC0EBCF6-0EE9-4873-BB6F-6A9F4CF84B79}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -101,6 +107,7 @@ Global
{C539C7B4-BBF2-4AC2-A732-0C47E125CCD2} = {435867FA-EE25-4708-9BBA-8509ABC7E389}
{C45A1400-B4E9-4638-BF1F-4BB38CEC5E93} = {09BEAB2A-F47E-4D2B-AE81-8DC1BBB52638}
{CEF2B49F-90AF-4B65-AA0C-A12AB1C0A18F} = {09BEAB2A-F47E-4D2B-AE81-8DC1BBB52638}
{CC0EBCF6-0EE9-4873-BB6F-6A9F4CF84B79} = {435867FA-EE25-4708-9BBA-8509ABC7E389}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {23DF7019-3B25-4B82-8955-25F1DDD72D84}
Expand Down
9 changes: 9 additions & 0 deletions samples/CheckMemoryLeak/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="CheckMemoryLeak.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CheckMemoryLeak"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
14 changes: 14 additions & 0 deletions samples/CheckMemoryLeak/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Configuration;
using System.Data;
using System.Windows;

namespace CheckMemoryLeak
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}

}
10 changes: 10 additions & 0 deletions samples/CheckMemoryLeak/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
16 changes: 16 additions & 0 deletions samples/CheckMemoryLeak/CheckMemoryLeak.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFrameworks>$(SampleTargetFrameworks)</TargetFrameworks>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<IsPackable>False</IsPackable>
<LangVersion>9</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\MdXaml.Full\MdXaml.Full.csproj" />
</ItemGroup>

</Project>
52 changes: 52 additions & 0 deletions samples/CheckMemoryLeak/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<Window x:Class="CheckMemoryLeak.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:local="clr-namespace:CheckMemoryLeak"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">

<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>

<DockPanel LastChildFill="True">
<DockPanel LastChildFill="True">
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Button Content="Add Text" Command="{Binding AddTextCommand}"/>
<Button Content="Add Resource" Margin="10,0,0,0" Command="{Binding AddResourceCommand}"/>
</StackPanel>

<Button DockPanel.Dock="Bottom" Content="Run" Click="Button_Click"/>

<ListBox ItemsSource="{Binding Contents}">
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel Orientation="Horizontal">
<Label Width="90">
<Label.Style>
<Style TargetType="Label">
<Style.Triggers>
<DataTrigger Binding="{Binding ContentType}" Value="Resource">
<Setter Property="Content" Value="Resource"/>
</DataTrigger>
<DataTrigger Binding="{Binding ContentType}" Value="Text">
<Setter Property="Content" Value="Text"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
<TextBox Text="{Binding Text}" HorizontalAlignment="Stretch" Width="200"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>

<DockPanel LastChildFill="True" x:Name="ViewPanel">

</DockPanel>
</DockPanel>
</Window>
51 changes: 51 additions & 0 deletions samples/CheckMemoryLeak/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using MdXaml.Full;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace CheckMemoryLeak
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private async void Button_Click(object sender, RoutedEventArgs e)
{
var vm = (MainWindowViewModel)DataContext;
foreach (var content in vm.Contents)
{
var viewer = new MarkdownScrollViewer();

if (content.ContentType == ContentType.Resource)
{
viewer.Source = new Uri(content.Text);
}
if (content.ContentType == ContentType.Text)
{
viewer.Markdown = content.Text;
}

ViewPanel.Children.Add(viewer);

while (!viewer.IsLoaded)
await Task.Delay(1000);

await Task.Delay(1000);

ViewPanel.Children.Clear();
}

foreach (var i in Enumerable.Range(0, 10)) {
GC.Collect();
await Task.Delay(1000);
}
}
}
}
75 changes: 75 additions & 0 deletions samples/CheckMemoryLeak/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Net.Mime;
using System.Text;
using System.Windows.Input;

namespace CheckMemoryLeak
{
internal class MainWindowViewModel
{
public ICommand AddTextCommand { get; }
public ICommand AddResourceCommand { get; }

public ObservableCollection<Content> Contents { get; }
public MainWindowViewModel()
{
Contents = new();
AddTextCommand = new DelegateCommand(AddText);
AddResourceCommand = new DelegateCommand(AddResource);

Contents.Add(new Content(ContentType.Resource) { Text = "https://raw.githubusercontent.com/whistyun/MdXaml/master/samples/MdXaml.Demo/MainWindow.md#note" });
Contents.Add(new Content(ContentType.Resource) { Text = @"file:///D:\MdXaml\samples\MdXaml.Demo\MainWindow.md#note" });
Contents.Add(new Content(ContentType.Text) { Text = @"[text](file:///D:\MdXaml\samples\MdXaml.Demo\MainWindow.md#note)" });
}

public void AddText()
{
Contents.Add(new Content(ContentType.Text));
}
public void AddResource()
{
Contents.Add(new Content(ContentType.Resource));
}
}

public class DelegateCommand : ICommand
{
private Action _action;
public event EventHandler? CanExecuteChanged;

public DelegateCommand(Action act)
{
_action = act;
}

public bool CanExecute(object? parameter)
{
return true;
}

public void Execute(object? parameter)
{
_action();
}
}

public class Content
{
public ContentType ContentType { get; }
public string Text { get; set; }

public Content(ContentType type)
{
ContentType = type;
}
}

public enum ContentType
{
Resource,
Text
}
}

0 comments on commit 951ec88

Please sign in to comment.