简体   繁体   English

在WPF MVVM应用程序中的多个ViewModel之间传递图像/数据

[英]Passing Image/data between multiple ViewModels in WPF MVVM application

I need help to understand Multiple ViewModel communication in Wpf. 我需要帮助来了解Wpf中的多个ViewModel通信。

(1): I have MainWindowViewModel which loads image and display in MainWindow. (1):我有MainWindowViewModel,它可以加载图像并在MainWindow中显示。

(2): I have ChildWindowViewModel contain button to convert image to grayimage (2):我有ChildWindowViewModel包含按钮,可将图像转换为grayimage

I have to pass input image from mainviewmodel to childviewmodel to apply gray operation when event is fired in childwindow and after applying operation i have to return modified image to mainviewmodel to display in mainwindow 当在子窗口中触发事件时,我必须将输入图像从mainviewmodel传递到childviewmodel,以应用灰色操作,并且在应用操作之后,我必须将修改后的图像返回到mainviewmodel,以在mainwindow中显示

I have tried doing some codebehind thing by passing mainwindow to childviewmodel and it worked, Now i have to achieve this with pure mvvm architecture 我试图通过将mainwindow传递给childviewmodel来做一些代码隐藏的事情,并且它起作用了,现在我必须使用纯mvvm架构来实现这一点

Give me suggestion, thanks in advance :) 给我建议,在此先感谢:)

MainWindow: 主窗口:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid Grid.Row="0" >
        <StackPanel Orientation="Horizontal">
            <Button Content="Load Image" Command="{Binding OpenImg}"/>
            <Button Content="Child Window" Command="{Binding ChildWin}"/>
        </StackPanel>
    </Grid>
    <Grid Grid.Row="1" >
        <Viewbox Margin="5,5,5,5">
            <Image x:Name="image" Source="{Binding Image}"/>
        </Viewbox>
    </Grid>

</Grid>

MainWindowViewModel: MainWindowViewModel:

public class MainWindowViewModel : ViewModelBase
  {
    public ICommand OpenImg { get; set; }

    Bitmap bitmap;

    public MainWindowViewModel()
    {
        OpenImg = new RelayCommand(LoadImage);
    }

    private BitmapImage _image;
    public BitmapImage Image
    {
        get { return _image; }
        set
        {
            _image = value;
            RaisePropertyChanged("Image");
        }
    }

    private void LoadImage()
    {
        OpenFileDialog op = new OpenFileDialog();
        op.Title = "Select a Picture";
        op.Filter = "Images (*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" +
            "All files (*.*)|*.*";
        op.Multiselect = true;
        if (op.ShowDialog() == true)
        {
            Image = new BitmapImage(new Uri(op.FileName));
            bitmap = new Bitmap(op.FileName);
        }
    }

    private RelayCommand _childWin;
    public ICommand ChildWin
    {
        get
        {`enter code here`
            if (_childWin == null)
            {
                _childWin = new RelayCommand(DisplayChildWin);
            }
            return _childWin;
        }
    }

    private void DisplayChildWin()
    {
        ChildWindow childWindow = new ChildWindow();
        childWindow.ShowDialog();
    }
}

ChildWindow: ChildWindow:

<Grid>

    <Button Content="Convert To GrayImage" Command="{Binding ApplyGray}" Width="150" Height="30" />

</Grid>

ChildWindowViewModel: ChildWindowViewModel:

 public class ChildWindowViewModel : ViewModelBase
  {
    public ChildWindowViewModel()
    {
    }

    private RelayCommand _applyGray;
    public ICommand ApplyGray
    {
        get
        {
            if (_applyGray == null)
            {
                _applyGray = new RelayCommand(ApplyGrayScale);
            }
            return _applyGray;
        }
    }

    private void ApplyGrayScale()
    {

    }
}

GrayConversion Class: GrayConversion类:

   public static class GrayScale
    {
    public static Bitmap ApplyGray(Bitmap bmp)
    {
        //get image dimension
        int width = bmp.Width;
        int height = bmp.Height;

        //color of pixel
        Color p;

        //grayscale
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                //get pixel value
                p = bmp.GetPixel(x, y);

                //extract pixel component ARGB
                int a = p.A;
                int r = p.R;
                int g = p.G;
                int b = p.B;

                //find average
                int avg = (r + g + b) / 3;

                //set new pixel value
                bmp.SetPixel(x, y, Color.FromArgb(a, avg, avg, avg));
            }
        }
        return bmp;
    }
}

You could think about makeing your ViewModel Singelton 您可以考虑制作ViewModel Singelton

    private static ViewModel _createInstance = null;

    public static ViewModel CreateInstance
    {

        get
        {
            if (null == _createInstance)
            {
                _createInstance = new ViewModel();
            }

            return _createInstance;
        }
    }//END CreateInstance

and access it using the createInstance function like so: 并使用createInstance函数访问它,如下所示:

    ViewModel.CreateInstance.TheImageYouWantToAccess

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM