Merge pull request #774 from yangjieshao/fix_issues_680

修复 ImageViewer.Source 设置为null会报错的问题
This commit is contained in:
Dong Bin
2025-09-17 12:08:09 +08:00
committed by GitHub
3 changed files with 29 additions and 2 deletions

View File

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

View File

@@ -8,4 +8,19 @@ public partial class ImageViewerDemo : UserControl
{ {
InitializeComponent(); 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;
}
}
} }

View File

@@ -149,6 +149,10 @@ public class ImageViewer: TemplatedControl
{ {
if(!IsLoaded) return; if(!IsLoaded) return;
IImage image = args.GetNewValue<IImage>(); IImage image = args.GetNewValue<IImage>();
if(image is null)
{
return;
}
Size size = image.Size; Size size = image.Size;
double width = this.Bounds.Width; double width = this.Bounds.Width;
double height = this.Bounds.Height; double height = this.Bounds.Height;