我需要帮助来了解Wpf中的多个ViewModel通信。
(1):我有MainWindowViewModel,可以加载图像并在MainWindow中显示。
(2):我有ChildWindowViewModel包含将图像转换为grayimage的按钮
当在子窗口中触发事件时,我必须将输入图像从mainviewmodel传递到childviewmodel,以应用灰色操作,并且在应用操作之后,我必须将修改后的图像返回到mainviewmodel,以在mainwindow中显示
我尝试通过将mainwindow传递给childviewmodel来做一些代码隐藏的事情,并且它起作用了, 现在,我必须使用纯mvvm架构来实现这一目标
请给我建议,谢谢:)
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:
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:
<Grid>
<Button Content="Convert To GrayImage" Command="{Binding ApplyGray}" Width="150" Height="30" />
</Grid>
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类:
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;
}
}
答案 0 :(得分:0)
您可以考虑制作ViewModel Singelton
private static ViewModel _createInstance = null;
public static ViewModel CreateInstance
{
get
{
if (null == _createInstance)
{
_createInstance = new ViewModel();
}
return _createInstance;
}
}//END CreateInstance
并使用createInstance函数访问它,如下所示:
ViewModel.CreateInstance.TheImageYouWantToAccess