简单地说,我有一个有多种视图的棱镜模块。
其中一个是带有网格的用户控件,我需要在此视图处于活动状态时重新加载其datacontext。
我知道我必须使用IActiveAware
界面,但我无法弄清楚如何......
修改
MyUserControl.xaml
<UserControl x:Class="[...]
mc:Ignorable="d">
<UserControl.Resources>
<dc:MyViewModel x:Key="MyViewModel" />
</UserControl.Resources>
<Grid DataContext="{StaticResource MyViewModel}">
<Grid.RowDefinitions>
[...]
MyUserControl.xaml.cs
public partial class MyUserControl: UserControl, IActiveAware
{
public MyUserControl(IRegionManager _regionManager)
{
regionManager = _regionManager;
InitializeComponent();
}
public event EventHandler<EventArgs> Activated;
public event EventHandler<EventArgs> Deactivated;
private bool _IsActive;
public bool IsActive
{
get
{
return _IsActive;
}
set
{
_IsActive = value;
IActiveAware vmAware = DataContext as IActiveAware;
if (vmAware != null)
vmAware.IsActive = value;
}
}
public event EventHandler IsActiveChanged = delegate { };
[...]
MyViewModel.cs
public class MyViewModel: IActiveAware
{
private bool isActive { get; set; }
public bool IsActive
{
get
{
return isActive;
}
set
{
isActive = value;
LoadData();
IsActiveChanged(this, EventArgs.Empty);
}
}
public event EventHandler IsActiveChanged = delegate { };
[...]
答案 0 :(得分:0)
只需在视图模型上实现界面即可。你也需要实现事件处理程序,但我已经离开了这个例子。您可以在Brian Noyes's blog上看到完整示例。
public class ViewModel : IActiveAware
{
private bool _isActive;
public bool IsActive
{
get { return _isActive; }
set
{
_isActive = value;
// Do Other Stuff e.g.
LoadData();
}
}
private void LoadData()
{
.....
}
}
Prism查看它正在激活的视图模型,并确定它是否实现IActiveAware
,如果是,则设置IsActive
,以便您可以创建视图模型,从而看,做点什么。