gpt4 book ai didi

c# - 带有使用依赖注入(inject)创建的 View 模型的 Prism PopupWindowAction

转载 作者:行者123 更新时间:2023-11-30 12:25:55 27 4
gpt4 key购买 nike

我创建了一个“弹出”窗口,根据 Prism 文档使用 PopupWindowAction 显示该窗口。 View 加载得很好,但 ViewModel 不是。我能找到的所有示例都在 View 的后台代码中创建了一个简单的 ViewModel。我的 ViewModel 需要通过统一构建,以便可以注入(inject)依赖项,但这被绕过了,因为 View 是在 xaml 中声明的:

<prism:InteractionRequestTrigger SourceObject="{Binding CustomViewRequest, Mode=OneWay}">
<prism:PopupWindowAction>
<prism:PopupWindowAction.WindowContent>
<views:CustomView />
</prism:PopupWindowAction.WindowContent>
</prism:PopupWindowAction>
</prism:InteractionRequestTrigger>

我有一个部分解决方法,即在 PopupWindowAction.WindowContent 中嵌入一个 ContentControl(带有一个区域)。这是因为当我将 View 加载到该区域时,会为我创建 ViewModel。但是,每次出现窗口时,它的大小都与所有显示器上的总桌面空间相同。

我想我可以实现一些代码来设置弹出窗口的起始位置和尺寸,但我无权访问窗口,因为它是在 PopupWindowAction 中为我创建的。我不想限制底层 ContentControl 或 View 的大小,否则用户将无法调整窗口大小。另外,这感觉像是一种解决方法!

那么如何使用依赖注入(inject)让 PopupWindowAction 加载 ViewModel 呢?或者,如果这不简单,如何访问窗口维度并将它们绑定(bind)到与 ContentControl 中的 View 关联的 View 模型?

最佳答案

我遇到了同样的需求,并且能够让它与自定义派生的 PopupWindowAction 一起工作,以允许组合其 WindowContent。根据 Prism 文档 Unity doesn't support the TryResolve extension method ,但如果您更熟悉 Unity,则可能有另一种方法来执行适用于 Unity 的 TryResolve 部分。

所以我定义了一个 ComposablePopupWindowAction,它添加了一个 WindowContentType 依赖属性。然后,我覆盖了 Invoke 方法,以使用服务定位器获取 WindowContentType 的实例(如果已定义)。

// a popupWindowAction that allows the DI to compose its view
public class ComposablePopupWindowAction : PopupWindowAction
{
public static readonly DependencyProperty WindowContentTypeProperty =
DependencyProperty.Register(
"WindowContentType",
typeof(Type),
typeof(ComposablePopupWindowAction),
new PropertyMetadata(null),
v => {
Type type = v as Type;
Type frameworkElementType = typeof(FrameworkElement);
// either this is not specified, or if it is then it needs to be a FrameworkElement
return (v == null) || type.IsSubclassOf(frameworkElementType) || (type == frameworkElementType);
}
);

public Type WindowContentType
{
get { return (Type)GetValue(WindowContentTypeProperty); }
set { SetValue(WindowContentTypeProperty, value); }
}

protected override void Invoke(object parameter)
{
ConfigureWindowContent();

base.Invoke(parameter);
}

protected void ConfigureWindowContent()
{
// configure the windowContent if not specified, but a type was
if ((this.WindowContentType != null) && (this.WindowContent == null))
{
// this doesn't appear to be supported in Unity so might need slightly different logic here?
var view = ServiceLocator.Current.TryResolve(this.WindowContentType);
// if can't get thedesired type then base will use the notification
if ((view != null) && (view.GetType() == this.WindowContentType))
{
this.WindowContent = view as FrameworkElement;
}
}
}
}

然后照常执行 interactionRequestTrigger,并为 WindowContentType 属性指定 View 类型:

<prism:InteractionRequestTrigger SourceObject="{Binding ConfigurationPopupRequest, Mode=OneWay}">
<inf:ComposablePopupWindowAction IsModal="True" CenterOverAssociatedObject="True" WindowContentType="{x:Type analysis:ConfigurationPopupView}" />
</prism:InteractionRequestTrigger>

关于c# - 带有使用依赖注入(inject)创建的 View 模型的 Prism PopupWindowAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30373027/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com