C# 从弹出窗口关闭选项卡项
这里我有一个使用Prism 6工具包的功能性CloseTabAction,如下所示:C# 从弹出窗口关闭选项卡项,c#,mvvm,prism,C#,Mvvm,Prism,这里我有一个使用Prism 6工具包的功能性CloseTabAction,如下所示: class CloseTabAction : TriggerAction<Button> { protected override void Invoke(object parameter) { var args = parameter as RoutedEventArgs; if (args == null) return; var
class CloseTabAction : TriggerAction<Button>
{
protected override void Invoke(object parameter)
{
var args = parameter as RoutedEventArgs;
if (args == null) return;
var tabItem = FindParent<TabItem>(args.OriginalSource as DependencyObject);
if (tabItem == null) return;
var tabControl = FindParent<TabControl>(tabItem);
if (tabControl == null) return;
var region = RegionManager.GetObservableRegion(tabControl).Value;
if (region == null) return;
if (region.Views.Contains(tabItem.Content))
{
region.Remove(tabItem.Content);
}
}
static T FindParent<T>(DependencyObject child) where T : DependencyObject
{
while (true)
{
var parentObject = VisualTreeHelper.GetParent(child);
if (parentObject == null) return null;
var parent = parentObject as T;
if (parent != null) return parent;
child = parentObject;
}
}
}
我看到你看了我的Pluralsight课程,关于使用Prism控制TabControl:) 为什么不在调用“删除”之前显示一个对话框呢。如果对话框的结果有效,则继续删除,如果无效,则不删除。然后,您只需要决定如何通知ViewModel操作。这可以通过多种方式实现;事件聚合器,在CloseTabAction触发器上公开属性,在触发器上公开命令。你可以决定怎么做
编辑:实际上我想到了一个更好的方法。只需使用ViewModel之外的界面,检查是否可以关闭选项卡项。可以使用IConfirmNavigationRequest接口或创建自己的接口。然后在移除he区域之前检查。这更容易。在invoke方法中,我添加了这段代码。它检查是否实现了自定义的“IConfrimCloseRequest”接口,然后根据该结果调用“ConfrimCloseRequest”方法close或cancel。如果它没有实现接口,就关闭它
var context = new NavigationContext(region.NavigationService, null);
if (IfImplements<IConfirmCloseRequest>(tabItem.Content,
i => i.ConfirmCloseRequest(context, canClose =>
{
if (!canClose) return;
if (region.Views.Contains(tabItem.Content))
region.Remove(tabItem.Content);
}))) return;
if (region.Views.Contains(tabItem.Content))
region.Remove(tabItem.Content);
private static T Implementor<T>(object content) where T : class
{
T implementor = content as T;
if (implementor != null) return implementor;
var element = content as FrameworkElement;
if (element != null) implementor = element.DataContext as T;
return implementor;
}
private static bool IfImplements<T>(object content, Action<T> action) where T : class
{
T target = Implementor<T>(content);
if (target == null) return false;
action(target);
return true;
}
var-context=new-NavigationContext(region.NavigationService,null);
如果(i)实现(tabItem.Content,
i=>i.ConfirmCloseRequest(上下文,canClose=>
{
如果(!canClose)返回;
if(region.Views.Contains(tabItem.Content))
region.Remove(tabItem.Content);
})))返回;
if(region.Views.Contains(tabItem.Content))
region.Remove(tabItem.Content);
私有静态T实现器(对象内容),其中T:class
{
T implementor=内容为T;
if(implementor!=null)返回implementor;
var元素=作为框架元素的内容;
if(element!=null)implementor=element.DataContext作为T;
返回实现者;
}
私有静态bool i实现(对象内容、操作),其中T:class
{
T目标=实施者(内容);
if(target==null)返回false;
行动(目标);
返回true;
}
在我的ViewModel中,我使用“ConfirmCloseRequest”方法实现了我的自定义界面,关闭选项卡时将调用该方法
public void ConfirmCloseRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
ClosePopupRequest.Raise(new Confirmation { Title = "Confrim", Content = "Are you sure you want to close this view?" }, r => { continuationCallback(r.Confirmed); });
}
public void ConfirmCloseRequest(NavigationContext NavigationContext,Action continuationCallback)
{
closepopurequest.Raise(新确认{Title=“Confrim”,Content=“确实要关闭此视图吗?”),r=>{continuationCallback(r.confirm);});
}
@BrainLagunas是的,我喜欢你的课程!我希望看到更多,即使Pluralsight认为它们非常具体。不管怎样,我把它修好了,并让它与你的第二个建议一起工作。请看下面的解决方案。
public void ConfirmCloseRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
ClosePopupRequest.Raise(new Confirmation { Title = "Confrim", Content = "Are you sure you want to close this view?" }, r => { continuationCallback(r.Confirmed); });
}