WPF自定义命令更改图片

时间:2017-10-31 11:14:13

标签: c# wpf c#-4.0

我试图在侧边栏菜单中点击它时更改图片,但是当我点击图片时似乎没有任何事情发生。这是在WPF和图像编辑程序中完成的。我一直在阅读自定义命令并在StackOverflow上阅读其他自定义命令帖子,但我似乎无法找到适用于此案例的任何内容。

public partial class ImageEditor : UserControl
{
    ICommand changePicture;
    public ICommand ChangePicture 
    { 
        get { return changePicture; }
        set { changePicture = value; } 
    }

    public ImageEditor()
    {
        InitializeComponent();
    }

    public void loadImages(string _imageFile)
    {
        string directory = Path.GetDirectoryName(_imageFile);
        string[] _filePaths = Directory.GetFiles(directory, "*.jpg");
        foreach (string image in _filePaths)
        {
            if (image.Length > 0)
            {
                thumbNails.Children.Add(addPictureButton(image));
            }
        }
    }

    public Button addPictureButton(string image)
    {
        Image currentImage = new Image();
        currentImage.Source = new BitmapImage(new Uri(image));
        currentImage.Stretch = Stretch.Fill;

        Grid grid = new Grid();
        grid.Height = 90;
        grid.Children.Add(currentImage);

        var newButton = new MyButton();
        newButton.Content = grid;
        newButton.Name = "button";
        newButton.Height = 100;
        newButton.Background = Brushes.Chocolate;
        newButton.ImageRef = image;
        newButton.Command = ChangePicture;
        return newButton;
    }
}

我认为我的自定义命令有问题,这是我加载所有图片的主要方法。

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    /// Get the Handle for the Forms System Menu
    IntPtr systemMenuHandle = GetSystemMenu(this.Handle, false);
    /// Create our new System Menu items just before the Close menu item
    InsertMenu(systemMenuHandle, 5, MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty); // <-- Add a menu seperator
    InsertMenu(systemMenuHandle, 6, MF_BYPOSITION, _SettingsSysMenuID, "Inställningar...");
    InsertMenu(systemMenuHandle, 7, MF_BYPOSITION, _CalibrateSysMenuID, "Kalibrerings Läge (På/Av)");
    InsertMenu(systemMenuHandle, 8, MF_BYPOSITION, _ZoomPrevSysMenuID, "Zoom Läge -1");
    InsertMenu(systemMenuHandle, 9, MF_BYPOSITION, _ZoomNextSysMenuID, "Zoom Läge +1");
    //InsertMenu(systemMenuHandle, 10, MF_BYPOSITION, _PrintSysMenuID, "Print...");

    // Attach our WndProc handler to this Window
    HwndSource source = HwndSource.FromHwnd(this.Handle);
    source.AddHook(new HwndSourceHook(WndProc));

    if (ImageFile != null && ImageFile.Length > 0)
    {
        if (_cameraManager != null)
            _cameraManager.IsPaused = true;

        Bitmap bitmap = new Bitmap(ImageFile);
        UcImageGrabbedControls.SetImageToView(bitmap);
        ImagePicker.loadImages(ImageFile);
        ImagePicker.ChangePicture = new CustomCommand(OnPictureClick);

        BtnGrab.IsToggled = true;

        UpdateShownControls();
    }
}

按钮到这里

public void OnPictureClick(object sender)
{
    var button = (UserControls.MyButton)sender;
    if (button == null)
        return;

    var bitmap = new Bitmap(button.ImageRef);
    UcImageGrabbedControls.SetImageToView(bitmap);
}

自定义命令实现

public class CustomCommand : ICommand
{
    //public delegate void ExecutedMethod();
    //private ExecutedMethod method;
    private Action<object> onPictureClick;

    public CustomCommand(/*ExecutedMethod executed*/)
    {
        //method = executed;
    }

    public CustomCommand(Action<object> onPictureClick)
    {
        this.onPictureClick = onPictureClick;
    }

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        //method();
    }
}

1 个答案:

答案 0 :(得分:0)

这个课看起来像很多人一直在修改它,所以我删除了所有不再使用的东西。

table

基本问题是,public class CustomCommand : ICommand { private Action<object> onPictureClick; public CustomCommand(Action<object> onPictureClick) { this.onPictureClick = onPictureClick; } public event EventHandler CanExecuteChanged; public bool CanExecute(object parameter) { return true; } public void Execute(object parameter) { // Nothing happening here :( } } 方法没有做任何事情 - 毫不奇怪,任何执行此命令的按钮都没有做任何事情。最简单的解决方法是通过调用传递给构造函数的方法简单地恢复此方法的功能:

Execute

由于您已经在使用该方法,我建议您将操作重命名为不太具体的内容,例如public void Execute(object parameter) { onPictureClick.Invoke(parameter); // or onPictureClick(parameter) }

如果您打算(或需要)使用_action功能和/或使用CommandParameter,建议您查看CanExecute实施。
This answer has a detailed explanation)。