我很确定这已经在某个地方得到了解答,但我似乎无法在我的生活中找到它.
我正在尝试使用TabControl在UserControls之间切换(每个选项卡都不同,因此不使用Items)
这是细分:我有我的主视图和3个用户控件.Mainview有一个选项卡控件 - 每个选项卡应显示不同的用户控件.
我可以很容易地将tabcontrol对象设置为usercontrol但是它没有绑定到viewmodel,只绑定了视图.
所以我在我的VM中使用Conductor,以及ActivateItem.这是它开始变得奇怪/令人沮丧的地方.应用程序从选中Tab0开始,但Tab2(最后一个选项卡)内容.单击任何其他选项卡,为该选项卡加载正确的ViewModel.单击返回Tab0,同时加载正确的内容.
我如何让它停下来?另外,如果切换标签没有再次重新初始化视图模型,清除已经输入的字段,我真的很喜欢它.
无论如何,这是我的一些消息来源,我打算将它放在这里并在我打破鼠标之前处理其他事情.
视图:
和ViewModel:
class MainViewModel : Conductor
Patryk Ćwiek.. 30
我可以建议一条不同的路线吗?
这是我在主要细节场景中成功完成的事情.假设您有一组子视图模型.我将为所有这些项目准备一个标记界面,当然,如果有这样的方法跨越所有子视图模型,您可以添加您认为合适的属性/方法:
public interface IMainScreenTabItem : IScreen { }
您可以确定您希望所有子模型都是Screen
s(或者,在嵌套方案的情况下,Conductor
s).它使它们具有完整的初始化/激活/停用循环.
然后,子视图模型:
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
是一个虚拟属性,并且在一个没有密封的类的构造函数中调用虚方法是一个很大的禁忌.
然后,您可以添加相应的视图并设置您选择注册的IoC容器 - 您必须将所有子视图模型注册为实现该类的类IMainScreenTabItem
,然后:
public class MainViewModel : Conductor.Collection.OneActive { public MainViewModel(IEnumerable tabs) { Items.AddRange(tabs); } }
凡MainView.xaml
仅仅是:
它只是有效.如果您的子视图模型具有多个依赖关系(例如数据库访问,记录器,验证机制等),那么它也是非常好的和方便的解决方案,现在您可以让IoC完成所有繁重工作,而不是手动实例化它们.
但有一件事:选项卡将按照注入类的顺序放置.如果要控制排序,可以MainViewModel
通过传递自定义IComparer
或添加某些属性OrderBy
或选择IMainScreenTabItem
接口来在构造函数中对它们进行排序.默认选定的项目将是Items
列表中的第一个.
其他选择是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
)时激活.该bool
参数OnDeactivate
表示视图模型是否只是停用或完全"封闭"(例如,当您关闭对话窗口,并导航离开).如果完全关闭视图模型,它将在下次显示时重新初始化.
这意味着任何绑定数据都将在OnActivate
调用之间保留,您必须明确清除它OnDeactivate
.更重要的是,如果你保留对子视图模型的强引用,那么即使在你调用之后OnDeactivate(true)
,数据仍将在下一次初始化时出现 - 这是因为IoC注入视图模型被创建一次(除非你以一种形式注入工厂函数) of Func
),然后按需初始化/激活/停用.
关于bootstrapper,我不太清楚你正在使用什么样的IoC容器.我的示例使用SimpleInjector,但您可以使用例如Autofac轻松完成相同的操作:
public class AppBootstrapper : Bootstrapper{ private Container container; /// /// Override to configure the framework and setup your IoC container. /// protected override void Configure() { container = new Container(); container.Register(); container.Register (); var viewModels = Assembly.GetExecutingAssembly() .DefinedTypes.Where(x => x.GetInterface(typeof(IMainScreenTabItem).Name) != null && !x.IsAbstract && x.IsClass); container.RegisterAll(typeof(IMainScreenTabItem), viewModels); container.Verify(); } /// /// Override this to provide an IoC specific implementation. /// /// The service to locate.The key to locate. ////// The located service. /// 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
注意viewModels
注册Configure
.
我可以建议一条不同的路线吗?
这是我在主要细节场景中成功完成的事情.假设您有一组子视图模型.我将为所有这些项目准备一个标记界面,当然,如果有这样的方法跨越所有子视图模型,您可以添加您认为合适的属性/方法:
public interface IMainScreenTabItem : IScreen { }
您可以确定您希望所有子模型都是Screen
s(或者,在嵌套方案的情况下,Conductor
s).它使它们具有完整的初始化/激活/停用循环.
然后,子视图模型:
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
是一个虚拟属性,并且在一个没有密封的类的构造函数中调用虚方法是一个很大的禁忌.
然后,您可以添加相应的视图并设置您选择注册的IoC容器 - 您必须将所有子视图模型注册为实现该类的类IMainScreenTabItem
,然后:
public class MainViewModel : Conductor<IMainScreenTabItem>.Collection.OneActive { public MainViewModel(IEnumerable<IMainScreenTabItem> tabs) { Items.AddRange(tabs); } }
凡MainView.xaml
仅仅是:
<TabControl Name="Items"/>
它只是有效.如果您的子视图模型具有多个依赖关系(例如数据库访问,记录器,验证机制等),那么它也是非常好的和方便的解决方案,现在您可以让IoC完成所有繁重工作,而不是手动实例化它们.
但有一件事:选项卡将按照注入类的顺序放置.如果要控制排序,可以MainViewModel
通过传递自定义IComparer<IMainScreenTabItem>
或添加某些属性OrderBy
或选择IMainScreenTabItem
接口来在构造函数中对它们进行排序.默认选定的项目将是Items
列表中的第一个.
其他选择是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
)时激活.该bool
参数OnDeactivate
表示视图模型是否只是停用或完全"封闭"(例如,当您关闭对话窗口,并导航离开).如果完全关闭视图模型,它将在下次显示时重新初始化.
这意味着任何绑定数据都将在OnActivate
调用之间保留,您必须明确清除它OnDeactivate
.更重要的是,如果你保留对子视图模型的强引用,那么即使在你调用之后OnDeactivate(true)
,数据仍将在下一次初始化时出现 - 这是因为IoC注入视图模型被创建一次(除非你以一种形式注入工厂函数) of 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
注册Configure
.