微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – WPF Caliburn.Micro和TabControl与UserControls问题

我很确定这已经在某个地方得到回答,但我似乎找不到我的生活.

我试图使用TabControl在UserControls之间切换(每个选项卡是不同的,所以不使用Items)

以下是故障:
我有我的主视图和3个用户控件. Mainview有一个选项卡控件 – 每个选项卡应显示不同的用户控件.

我可以很容易地将tabcontrol contect设置为用户控件使用

但是它不是绑定到viewmodel,只是视图.

所以我在我的VM和ActivateItem中使用Conductor.这是开始变得奇怪/令人沮丧的地方.应用程序从Tab0选择开始,但是Tab2(最后一个选项卡)的内容.单击任何其他选项卡,为该选项卡加载正确的viewmodel.点击回到Tab0,加载正确的内容.

如何让它停止?另外,如果切换选项卡不会再次重新初始化视图模型,清除已经输入的字段,我真的很喜欢它.

无论如何,这里是我的一些资料来源,我打算把这个放在这里,然后再打破我的鼠标,再尝试其他的东西.

视图:

<TabControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row ="1">
        <TabItem Header="PC information">
            <Grid>
                <ContentControl x:Name="LoadRemoteInfo" cal:View.Model="{Binding ActiveItem}"/>
            </Grid>
        </TabItem>
        <TabItem Header="Remote Tools">
            <Grid>
                <ContentControl x:Name="LoadRemotetools" cal:View.Model="{Binding ActiveItem}"/>
            </Grid>
        </TabItem>
        <TabItem Header="CHRemote">
            <Grid>
                <ContentControl x:Name="LoadCHRemote" cal:View.Model="{Binding ActiveItem}"/>
            </Grid>
        </TabItem>

    </TabControl>

viewmodel:

class Mainviewmodel : Conductor<object>
{
    RemoteInfoviewmodel remoteInfo = new RemoteInfoviewmodel();
    Remotetoolsviewmodel remotetools = new Remotetoolsviewmodel();
    CHRemoteviewmodel chRemote = new CHRemoteviewmodel();

    public Mainviewmodel()
    {
        ActivateItem(remoteInfo);
    }

    public void LoadRemoteInfo()
    {
        ActivateItem(remoteInfo);
    }

    public void LoadRemotetools()
    {
        ActivateItem(remotetools);
    }

    public void LoadCHRemote()
    {
        ActivateItem(chRemote);
    }
}

解决方法

我可以建议一条不同的路线吗?

这是我在大师细节场景中成功的事情.假设你有一个子视图模型的集合.我将为所有这些项目准备一个标记界面,当然,如果有这样的方法跨越所有子视图模型,您可以添加您认为合适的属性/方法

public interface IMainScreenTabItem : IScreen
{
}

您可以确定您希望所有的小孩模型都是屏幕(或者在嵌套场景下,导体).它使它们具有可用的完整初始化/激活/停用周期.

然后,孩子视图模型:

public sealed class ChRemoteviewmodel : Screen,IMainScreenTabItem
{
    public ChRemoteviewmodel()
    {
        displayName = "CH Remote";
    }
}

public sealed class PcInfoviewmodel : Screen,IMainScreenTabItem
{
    public PcInfoviewmodel()
    {
        displayName = "PC Info";
    }
}

public sealed class Remotetoolsviewmodel : Screen,IMainScreenTabItem
{
    public Remotetoolsviewmodel()
    {
        displayName = "Remote Tools";
    }
}

displayName将显示标题文本.这是一个很好的做法,使这些类密封,因为displayName是一个虚拟属性,并且在没有密封的类的构造函数调用虚拟方法一个很大的no-no.

然后,您可以添加相应的视图并设置IoC容器的首选注册 – 您必须将所有子视图模型注册为实现IMainScreenTabItem的类,然后:

public class Mainviewmodel : Conductor<IMainScreenTabItem>.Collection.OneActive
{
    public Mainviewmodel(IEnumerable<IMainScreenTabItem> tabs)
    {
        Items.AddRange(tabs);
    }
}

MainView.xaml只是:

<TabControl Name="Items"/>

它只是工作.如果您的子视图模型具有多个依赖关系(例如数据库访问,记录器,验证机制等),那么它也是非常好的和方便的解决方案,现在您可以让IoC做所有的重大工作,而不是手动实例化.

有一件事情在这里标签将按照注册类的顺序放置.如果您想要控制订单,可以通过传递自定义IComparer< IMainScreenTabItem>在Mainviewmodel构造函数中对它们进行排序.或添加一些属性您可以OrderBy或选择到IMainScreenTabItem界面.认选择的项目将是“项目”列表中的第一个项目.

其他选项是使Mainviewmodel具有三个参数:

public Mainviewmodel(ChRemoteviewmodel chRemoteviewmodel,PcInfoviewmodel pcInfo,Remotetoolsviewmodel remotetools)
{
    // Add the view models above to the `Items` collection in any order you see fit
}

虽然你有超过2 – 3个儿童视图模型(你可以轻松获得更多),它会快速凌乱.

关于’清算’部分.由IoC创建的视图模型与常规生命周期一致:它们被初始化为最多一次(OnInitialize),然后在每次导航到OnDeactivate(bool)时被禁用,并在导航到(OnActivate)时激活. OnDeactivate中的bool参数指示视图模型是否刚刚停用或完全“关闭”(例如,当您关闭对话窗口并导航时).如果您完全关闭视图模型,则会在下一次显示时重新初始化.

这意味着在OnActivate调用之间保留任何绑定的数据,您必须在OnDeactivate中显式清除它.此外,如果您保持对子视图模型的强烈引用,则即使在调用OnDeactivate(true)之后,数据仍将在下次初始化时存在 – 这是因为IoC注入视图模型已创建一次(除非您注入工厂以Func< Yourviewmodel>形式的功能),然后根据需要初始化/激活/停用.

编辑

关于bootstrapper,我不太清楚你正在使用什么样的IoC容器.我的样品使用了SimpleInjector,但是你可以像这样简单地做同样的事情. Autofac:

public class AppBootstrapper : Bootstrapper<Mainviewmodel>
{
    private Container container;

    /// <summary>
    /// Override to configure the framework and setup your IoC container.
    /// </summary>
    protected override void Configure()
    {
        container = new Container();
        container.Register<IWindowManager,WindowManager>();
        container.Register<IEventAggregator,EventAggregator>();
        var viewmodels =
            Assembly.GetExecutingAssembly()
                .DefinedTypes.Where(x => x.GetInterface(typeof(IMainScreenTabItem).Name) != null && !x.IsAbstract && x.IsClass);
        container.Registerall(typeof(IMainScreenTabItem),viewmodels);
        container.Verify();
    }

    /// <summary>
    /// Override this to provide an IoC specific implementation.
    /// </summary>
    /// <param name="service">The service to locate.</param><param name="key">The key to locate.</param>
    /// <returns>
    /// The located service.
    /// </returns>
    protected override object GetInstance(Type service,string key)
    {
        if (service == null)
        {
            var typeName = Assembly.GetExecutingAssembly().DefinedTypes.Where(x => x.Name.Contains(key)).Select(x => x.AssemblyQualifiedname).Single();

            service = Type.GetType(typeName);
        }
        return container.GetInstance(service);
    }

    protected override IEnumerable<object> GetAllInstances(Type service)
    {
        return container.GetAllInstances(service);
    }

    protected override void BuildUp(object instance)
    {
        container.InjectProperties(instance);
    }
}

注意配置中的viewmodels注册.

原文地址:https://www.jb51.cc/csharp/94296.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐