修复 ImageViewer.Source 设置为null会报错的问题

修复 https://github.com/irihitech/Ursa.Avalonia/issues/680
This commit is contained in:
杨劼
2025-09-16 18:02:37 +08:00
parent 7f118d7b1c
commit be08931b47
3 changed files with 29 additions and 2 deletions

View File

@@ -9,7 +9,10 @@
d:DesignWidth="800"
mc:Ignorable="d">
<UserControl.Resources>
<SolidColorBrush x:Key="MaskBackground" Opacity="0.2" Color="Red" />
<SolidColorBrush
x:Key="MaskBackground"
Opacity="0.2"
Color="Red" />
<SolidColorBrush x:Key="MaskBorder" Color="Red" />
</UserControl.Resources>
<StackPanel>
@@ -20,7 +23,7 @@
<u:ImageViewer
Name="viewer"
Width="600"
Height="300"
Height="300"
MinScale="0.5"
Source="../Assets/3x.png">
<u:ImageViewer.Overlayer>
@@ -117,6 +120,11 @@
Grid.Row="3"
Grid.Column="1"
Theme="{DynamicResource SimpleToggleSwitch}" />
<Button
Grid.Row="3"
Grid.Column="2" Click="Button_Click"
Content="测试设置为 null" />
</Grid>
</StackPanel>
</UserControl>

View File

@@ -8,4 +8,19 @@ public partial class ImageViewerDemo : UserControl
{
InitializeComponent();
}
Avalonia.Media.IImage? oldImg;
private void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
if(viewer.Source!=null)
{
oldImg = viewer.Source;
viewer.Source = null;
}
else
{
viewer.Source = oldImg;
}
}
}