caliburn.micro 如何为视图模型运行时加载和绑定视图

发布于 2024-12-09 00:20:40 字数 407 浏览 1 评论 0原文

我正在构建一个需要主题支持的应用程序。所以我想提供视图文件夹运行时。

public class AppBootstrapper : Bootstrapper<IShell>
{
    CompositionContainer _container;

    /// <summary>
    /// By default, we are configure to use MEF
    /// </summary>
    protected override void Configure()
    {
         //view locator code get views from file and and binding it to viewmodel run time.
    }
}

I am building an application that requires theme support. So I want to supply views folder run time.

public class AppBootstrapper : Bootstrapper<IShell>
{
    CompositionContainer _container;

    /// <summary>
    /// By default, we are configure to use MEF
    /// </summary>
    protected override void Configure()
    {
         //view locator code get views from file and and binding it to viewmodel run time.
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水染的天色ゝ 2024-12-16 00:20:40

更好的调整是使用这种方式(在 Caliburn 中实现,但不在 Micro 中实现)。 http://caliburnmicro.codeplex.com/discussions/265502

首先,您需要定义一个用于存储用于发现视图的相关数据的属性:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public class ViewAttribute : Attribute
{
    public object Context { get; set; }

    public Type ViewType { get; private set; }

    public ViewAttribute(Type viewType)
    {
        ViewType = viewType;
    }
}

将其附加到您的视图模型。

[View(typeof(MyView))]
public class MyViewModel : Screen

然后,您需要将引导程序中的 LocateTypeForModelType 更改为如下所示:

void Initialize()
{
    var baseLocate = ViewLocator.LocateTypeForModelType;

    ViewLocator.LocateTypeForModelType = (modelType, displayLocation, context) =>
    {
        var attribute = modelType.GetCustomAttributes(typeof(ViewAttribute), false).OfType<ViewAttribute>().Where(x => x.Context == context).FirstOrDefault();
        return attribute != null ? attribute.ViewType : baseLocate(modelType, displayLocation, context);
    };
}

A better tweak would be to use this way (implemented in Caliburn but not the Micro one). http://caliburnmicro.codeplex.com/discussions/265502

First of all you need to define an attribute used to store the relevant data used to discover the view:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public class ViewAttribute : Attribute
{
    public object Context { get; set; }

    public Type ViewType { get; private set; }

    public ViewAttribute(Type viewType)
    {
        ViewType = viewType;
    }
}

Attach it to your View Model.

[View(typeof(MyView))]
public class MyViewModel : Screen

Then, you need to change the LocateTypeForModelType in your bootstrapper to something like this:

void Initialize()
{
    var baseLocate = ViewLocator.LocateTypeForModelType;

    ViewLocator.LocateTypeForModelType = (modelType, displayLocation, context) =>
    {
        var attribute = modelType.GetCustomAttributes(typeof(ViewAttribute), false).OfType<ViewAttribute>().Where(x => x.Context == context).FirstOrDefault();
        return attribute != null ? attribute.ViewType : baseLocate(modelType, displayLocation, context);
    };
}
风吹过旳痕迹 2024-12-16 00:20:40

在 Caliburn 中,您可以创建自定义的 IConventionManager 或调整实现 (DefaultConventionManager) 以更改框架在运行时查找 View 文件夹的方式。

事实上,视图不一定位于 Views 文件夹中,您可以修改此默认行为,因为这只是默认约定。实现此接口的最佳方法是检查默认实现。

In Caliburn, you can create a customized IConventionManager or tweek the implementation (DefaultConventionManager) to change the way the framework finds the View folder at runtime.

In fact views should not necessarily be in Views folder and you can modify this default behavior as this is just the default Convention. The best way to implement this interface is to check the default implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文