0

我在处理事件时遇到问题。基本上该程序生成图像但具有相同的事件。

 private Image MakeImage(string filename)
 {
     Uri uri = new Uri("ms-appx:///CategoryData/" + filename+".png");
     BitmapImage bitmap = new BitmapImage(uri);
     Image image = new Image()
     {
         Height = 100,
         Width = 100,
         Stretch = Stretch.Uniform,
         MaxHeight = 250,
         MaxWidth  = 250,
         HorizontalAlignment = HorizontalAlignment.Left,
         VerticalAlignment = VerticalAlignment.Top,
         Name = filename+"Img",
         ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY,
     };
     image.Source = bitmap;
     image.ManipulationDelta += image_ManipulationDelta;
     return image;
 }

 void image_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
 {
     Image image = sender as Image;
     CompositeTransform ct = image.RenderTransform as CompositeTransform;
     ct.TranslateX += e.Delta.Translation.X;
     ct.TranslateY += e.Delta.Translation.Y;
     //throw new NotImplementedException();
 }

并且在“”上发生异常ct.TranslateX += e.Delta.Translation.X;。你调用的对象是空的。

4

1 回答 1

1

检查你的演员CompositeTransform表,因为它可能是错误的类型——导致ct存在null,因此进入你的异常。

编辑:添加了一个例子。

CompositeTransform ct = image.RenderTransform as CompositeTransform;
if (ct == null) image.RenderTransform = ct = new CompositeTransform();
ct.TranslateX += e.Delta.Translation.X;
ct.TranslateY += e.Delta.Translation.Y;

TranslateTransform此外,如果您只需要翻译图像,为什么不使用 a呢?

于 2012-12-17T02:53:47.267 回答