我的任务是编写一个我正在研究的.NET/C#项目的异常处理策略和指南文档.我很难接受它.有很多关于如何/何时抛出,捕获,包装异常的信息,但我正在寻找描述catch块内部应该进行哪些事情,而不是包装和抛出异常.
try
{
DoSomethingNotNice();
}
catch (ExceptionICanHandle ex)
{
//Looking for examples of what people are doing in catch blocks
//other than throw or wrapping the exception, and throwing.
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
我希望有人可以帮助我.我正在构建一个WPF成像应用程序,它从相机中获取实时图像,允许用户查看图像,然后突出显示该图像上的感兴趣区域(ROI).然后将关于ROI的信息(宽度,高度,相对于图像上的点的位置等)发送回相机,实际上告知/训练相机固件在哪里寻找条形码,文本,液位,转弯等内容在图像上的螺丝等).所需的功能是能够平移和缩放图像及其ROI,以及在图像缩放比观看区域大时滚动.ROI的StrokeThickness和FontSize需要保持原始比例,但ROI内形状的宽度和高度需要与图像一起缩放(这对于捕获传输到相机的精确像素位置至关重要).除了滚动和其他一些问题之外,我已经解决了大部分问题.我关注的两个方面是:
当我介绍ScrollViewer时,我没有得到任何滚动行为.据我所知,我需要引入一个LayoutTransform来获得正确的ScrollViewer行为.然而,当我这样做时,其他区域开始崩溃(例如,ROI在图像上没有保持正确的位置,或者当平移时鼠标指针开始从图像上的选定点开始蠕动,或者我的图像的左角弹回到MouseDown上的当前鼠标位置.)
我无法按照我需要的方式扩展投资回报率.我有这个工作,但它并不理想.我所拥有的并没有保留精确的笔划厚度,我没有考虑忽略文本块上的比例.希望你能看到我在代码示例中正在做的事情.
我确定我的问题与我对Transforms及其与WPF布局系统的关系缺乏了解有关.希望展示我迄今为止所取得成就的代码将有所帮助(见下文).
仅供参考,如果Adorners是建议,可能在我的场景中不起作用,因为我最终会得到比支持更多的装饰(谣言144装饰者是事情开始崩溃的时候).
首先,下面是显示带有ROI(文本和形状)的图像的屏幕截图.矩形,椭圆和文本需要按照比例和旋转方式跟随图像上的区域,但不能在厚度或字体大小上进行缩放.

这是显示上面图像的XAML,以及用于缩放的滑块(鼠标滚轮缩放将在稍后出现)
<Window x:Class="PanZoomStackOverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Title="MainWindow" Height="768" Width="1024">
<DockPanel>
<Slider x:Name="_ImageZoomSlider" DockPanel.Dock="Bottom"
Value="2"
HorizontalAlignment="Center" Margin="6,0,0,0"
Width="143" Minimum=".5" Maximum="20" SmallChange=".1"
LargeChange=".2" TickFrequency="2"
TickPlacement="BottomRight" Padding="0" Height="23"/>
<!-- This resides in a user control in my solution -->
<Grid x:Name="LayoutRoot">
<ScrollViewer Name="border" HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<Grid x:Name="_ImageDisplayGrid">
<Image x:Name="_DisplayImage" Margin="2" Stretch="None"
Source="Untitled.bmp"
RenderTransformOrigin ="0.5,0.5"
RenderOptions.BitmapScalingMode="NearestNeighbor"
MouseLeftButtonDown="ImageScrollArea_MouseLeftButtonDown"
MouseLeftButtonUp="ImageScrollArea_MouseLeftButtonUp"
MouseMove="ImageScrollArea_MouseMove">
<Image.LayoutTransform>
<TransformGroup>
<ScaleTransform />
<TranslateTransform />
</TransformGroup>
</Image.LayoutTransform>
</Image>
<AdornerDecorator> <!-- Using this Adorner Decorator for Move, …Run Code Online (Sandbox Code Playgroud) 我已经看到用于实现INotifyPropertyChanged的以下模式
private void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释var handler = PropertyChanged赋值在检查null之前的必要性与直接检查PropertyChanged == null吗?
谢谢