【发布时间】:2014-02-03 01:49:57
【问题描述】:
我正在使用 MVVM 模式(没有框架,只有原始 MVVM)开发一个 Windows 应用商店应用程序。
我有一个用户控件DropboxFileplanUserControl.xaml,它有一个关联的视图模型DropboxFileplanViewModel.cs。 DropboxFileplanUserControl 嵌入在 MainPage.xaml 中,MainPage.xaml 具有关联的视图模型 MainPageViewModel.cs。
我的问题是如何在DropboxFileplanViewModel.cs 中定义和引发事件并在MainPageViewModel.cs 中处理它?假设要引发的事件称为ImageLoaded。
编辑:我添加了以下具体代码sn-ps...
DropboxFileplanUserControl.xaml
<UserControl
x:Class="PhotoBox.Controls.DropboxFileplanUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PhotoBox.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="using:PhotoBox.ViewModels"
xmlns:triggers="using:WinRT.Triggers"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="200">
<UserControl.DataContext>
<viewModels:DropboxFileplanViewModel />
</UserControl.DataContext>
<Grid>
<ListBox>
<!--
...
...
Here I define a ListBox and use interaction triggers to bind the SelectionChanged event to FileplanSelectionChangedCommand on the ViewModel -->
<triggers:Interactions.Triggers>
<triggers:EventTrigger EventName="SelectionChanged">
<triggers:InvokeCommandAction Command="{Binding FileplanSelectionChangedCommand}" PassEventArgsToCommand="True" />
</triggers:EventTrigger>
</triggers:Interactions.Triggers>
</ListBox>
</Grid>
DropboxFileplanViewModel.xaml 注意:我已经从这个 sn-p 中删除了很多代码
public class DropboxFileplanViewModel : ViewModel
{
public DelegateCommand FileplanSelectionChangedCommand { get; set; }
public DropboxFileplanViewModel()
{
FileplanSelectionChangedCommand = new DelegateCommand(FileplanSelectionChanged);
}
private void FileplanSelectionChanged(object parameter)
{
var args = (SelectionChangedEventArgs) parameter;
// Some other stuff is done here but ultimately,
// GetImageFile is called
}
private async void GetImageFile(MetaData file)
{
// Stuff is done here to get the image
// ******************************
// Here I want to raise the event
// ******************************
}
}
DropboxFileplanUserControl嵌入MainPage.xaml如下...
MainPage.xaml
<controls:DropboxFileplanUserControl
Grid.Row="0"
DataContext="{Binding FileplanControl}"
Visibility="{Binding IsOpen, Converter={StaticResource BooleanToVisibilityConverter}}"
IsEnabled="{Binding IsOpen}"
<!-- *** Here I need to access the ImageLoaded event and bind it to a command in MainPageViewModel.cs *** -->
/>
所以,总而言之,我需要在DropboxFileplanViewModel.cs 中声明并引发一个事件,并在MainPage.xaml 中访问此事件,以便我可以在MainPageViewModel.cs 中处理它。我知道如何将MainPage.xaml 中的事件绑定到MainPageViewModel 中的命令,我只需要知道如何做第一个,即在DropboxFileplanViewModel.cs 中声明和引发事件并在MainPage.xaml 中访问它。
【问题讨论】:
-
您是否使用基类来存储您的视图模型,例如 ViewModelLocator (mvvm light)?您可以为您的 xaml 和视图模型添加一些代码吗?
-
我的视图模型的继承层次是
ViewModel : BindableBase : INotifyPropertyChanged。关于类名,我尽量让我的原始问题保持一般性,但我将使用实际代码 sn-ps 编辑帖子。 -
感谢您的代码。一个简单的方法是在 mvvmlight 中使用 Messenger 类,但我认为我会在给出答案之前先检查一下,因为你没有使用框架。
标签: c# xaml mvvm windows-runtime windows-store-apps