feat: fix memory leakage.

This commit is contained in:
rabbitism
2024-01-22 19:00:12 +08:00
parent f0ec32c870
commit 0a3f7dbf5f
4 changed files with 25 additions and 10 deletions

View File

@@ -17,6 +17,10 @@
<Run Text="Result: "></Run> <Run Text="Result: "></Run>
<Run Text="{Binding Result}"></Run> <Run Text="{Binding Result}"></Run>
</TextBlock> </TextBlock>
<TextBlock>
<Run Text="Date: "></Run>
<Run Text="{Binding DialogViewModel.Date}"></Run>
</TextBlock>
<Button Command="{Binding ShowGlobalDialogCommand}">Show Dialog</Button> <Button Command="{Binding ShowGlobalDialogCommand}">Show Dialog</Button>
<Button Command="{Binding ShowLocalOverlayDialogCommand}">Show Local Overlay Dialog</Button> <Button Command="{Binding ShowLocalOverlayDialogCommand}">Show Local Overlay Dialog</Button>
<Button Command="{Binding ShowGlobalOverlayDialogCommand}"> Show Global Overlay Dialog </Button> <Button Command="{Binding ShowGlobalOverlayDialogCommand}"> Show Global Overlay Dialog </Button>

View File

@@ -23,6 +23,16 @@ public class DialogDemoViewModel: ObservableObject
set => SetProperty(ref _result, value); set => SetProperty(ref _result, value);
} }
private DateTime _date;
public DateTime Date
{
get => _date;
set => SetProperty(ref _date, value);
}
public DialogWithActionViewModel DialogViewModel { get; set; } = new DialogWithActionViewModel();
public DialogDemoViewModel() public DialogDemoViewModel()
{ {
ShowLocalOverlayDialogCommand = new AsyncRelayCommand(ShowLocalOverlayDialog); ShowLocalOverlayDialogCommand = new AsyncRelayCommand(ShowLocalOverlayDialog);
@@ -43,8 +53,10 @@ public class DialogDemoViewModel: ObservableObject
private async Task ShowLocalOverlayDialog() private async Task ShowLocalOverlayDialog()
{ {
var vm = new DialogWithActionViewModel();
var result = await DialogBox.ShowOverlayAsync<DialogWithAction, DialogWithActionViewModel, bool>( var result = await DialogBox.ShowOverlayAsync<DialogWithAction, DialogWithActionViewModel, bool>(
new DialogWithActionViewModel(), "LocalHost"); DialogViewModel, "LocalHost");
Date = vm.Date;
Result = result; Result = result;
} }
} }

View File

@@ -20,10 +20,6 @@
Grid.Row="0" Grid.Row="0"
Grid.RowSpan="2" Grid.RowSpan="2"
Content="{TemplateBinding Content}" /> Content="{TemplateBinding Content}" />
<StackPanel Grid.Row="2">
<Button>OK</Button>
<Button>Cancel</Button>
</StackPanel>
</Grid> </Grid>
</Border> </Border>
</ControlTemplate> </ControlTemplate>

View File

@@ -10,8 +10,6 @@ public class DialogControl: ContentControl
{ {
public const string PART_CloseButton = "PART_CloseButton"; public const string PART_CloseButton = "PART_CloseButton";
private Button? _closeButton; private Button? _closeButton;
public event EventHandler<object?> OnClose; public event EventHandler<object?> OnClose;
@@ -38,17 +36,22 @@ public class DialogControl: ContentControl
public Task<T> ShowAsync<T>() public Task<T> ShowAsync<T>()
{ {
var tcs = new TaskCompletionSource<T>(); var tcs = new TaskCompletionSource<T>();
this.OnClose+= (sender, args) =>
void OnCloseHandler(object sender, object? args)
{ {
if (args is T result) if (args is T result)
{ {
tcs.SetResult(result); tcs.SetResult(result);
OnClose-= OnCloseHandler;
} }
else else
{ {
tcs.SetResult(default); tcs.SetResult(default(T));
OnClose-= OnCloseHandler;
} }
}; }
this.OnClose += OnCloseHandler;
return tcs.Task; return tcs.Task;
} }