C# 按此键添加图像

C# 按此键添加图像,c#,wpf,canvas,C#,Wpf,Canvas,我试图在按下键盘按钮时将图像添加到画布: public MainWindow() { InitializeComponent(); } public void OnKeyDownHandler(object sender, KeyEventArgs e) { Image img = new Image(); img.Source = new BitmapImage(new Uri("C:\\Users\\Public\\Pictures\\Sample Pictur

我试图在按下键盘按钮时将图像添加到画布:

public MainWindow()
{
  InitializeComponent();
}

public void OnKeyDownHandler(object sender, KeyEventArgs e)
{
    Image img = new Image();
        img.Source = new BitmapImage(new Uri("C:\\Users\\Public\\Pictures\\Sample Pictures\\Koala.jpg"));

        Canvas.SetTop(img, 0);
        Canvas.SetLeft(img, 0);
        this.Content = img;
}
以下是XAML:

<Canvas KeyDown="OnKeyDownHandler" HorizontalAlignment="Left" Height="166" Margin="118,89,0,0" VerticalAlignment="Top" Width="300"/>


但当我按下一个键时,什么也没发生。如果我在MainWindow方法中输入相同的代码,它将加载图片。我错在哪里?

我认为你应该增加体重+身高,并将图像添加到画布上。孩子们

img.Width = value;
img.Height = value;
MyCanvas.Children.Add(img);

你需要给你的画布起一个名字,让画布可以聚焦,然后在构造器中关注它。下面是代码片段

<Canvas KeyDown="OnKeyDownHandler" Focusable="True" HorizontalAlignment="Left" Height="166" Margin="118,89,0,0" VerticalAlignment="Top" Width="300" Name="myCanvas"/>

代码隐藏:

    public MainWindow()
    {
        InitializeComponent();
        myCanvas.Focus(); //<-- Do this First

    }



    private void OnKeyDownHandler(object sender, KeyEventArgs e)
    {
        Image img = new Image();
        img.Source = new BitmapImage(new Uri("C:\\Users\\Public\\Pictures\\Sample Pictures\\Koala.jpg"));

        Canvas.SetTop(img, 0);
        Canvas.SetLeft(img, 0);
        this.Content = img;
    }
public主窗口()
{
初始化组件();

myCanvas.Focus();//有几点:

  • 在XAML中,您必须为画布指定一个名称,以便您可以从代码中将其关联起来。然后您可以使用
    maincavas.Children.Add(img);
  • 当您使用单词
    this
    时,您指的是类的实例—您的窗口,而不是画布。
    this.Content=
    与编写
    Content=
    相同,并且会将img放在窗口上。这将直接在构造函数中发生
  • 最后一点是焦点:xaml中定义的画布未获得焦点,并且未激发事件。如果向画布添加背景,则可以激发
    maincavas\u MouseDown
    。即使有背景,也不会激发
    KeyDown
  • 在画布上设置焦点并在xaml中添加
    Focusable=“True”
    后,“keyDown”也将启动(其他人已经回答过)
  • xaml:


    在OnKeyDownHandler中放置断点,并确定是否触发事件。我想这不是因为画布未聚焦。我已编辑了您的标题。请参阅“”,其中一致意见是“不,他们不应该”。
    <Canvas Name="MainCanvas" Focusable="True" Background="AliceBlue" HorizontalAlignment="Left" Height="160" Margin="68,39,0,0" VerticalAlignment="Top" Width="192" KeyDown="MainCanvas_KeyDown" MouseDown="MainCanvas_MouseDown_1" >
    
    public partial class MainWindow : Window
    {
        Image img = new Image { Source = new BitmapImage(new Uri("C:\\Users\\Public\\Pictures\\Koala.jpg")) };
        public MainWindow()
        {
            InitializeComponent();
            MainCanvas.Focus();
            //this.Content = img; 
            //Content = img; //same as the above. you don't need to write "this".
        }
    
        private void button_Click(object sender, RoutedEventArgs e)
        {// will work even if canvas has no background
            MainCanvas.Children.Add(img);
        }
    
        private void MainCanvas_KeyDown(object sender, KeyEventArgs e)
        {
            //event will not fire. Canvas does not get the focus
            //if you must have KeyDown trigger the event, you need MainCanvas.Focus() in the constructor, and Focusable="True" in the XAML.
            MainCanvas.Children.Add(img);
        }
    
        private void MainCanvas_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            //This event will only fire if the canvas can get the focus: e.g. if it has some background.
            MainCanvas.Children.Add(img); //canvas control has the name MainCanvas inside the xaml
            //the below will work, but place the image on the window, because "this" means the class instance, not the method or event you are in.
            //this.Content = img;
        }
    }