简体   繁体   English

如何存储加载的View,这样当我们到达同一个View时就不需要再次初始化组件

[英]How to store the loaded View, so that we don't need to Initialize the component again when we reach to the same View

Actually I am having two buttons.实际上我有两个按钮。 When I click first button then one UserControl will be visible in ContentControl, and when I click on second button then another UserControl will come and sit in ContentControl.当我单击第一个按钮时,一个 UserControl 将在 ContentControl 中可见,当我单击第二个按钮时,另一个 UserControl 将出现在 ContentControl 中。 The problem is, everytime when I click buttons it will load particular UserControl->InitializeComponent() method.问题是,每次单击按钮时,它都会加载特定的 UserControl->InitializeComponent() 方法。 I want to load the View only one time in the beginning and not everytime when I switch from one UserControl to another.我想在开始时只加载一次视图,而不是每次从一个 UserControl 切换到另一个时。 A lot of Thanks!非常感谢!

You have several options.你有几个选择。

A dictionary would be one.一本字典就可以了。

 Dictionary<Type, UserControl> MyControls

You can check if you have an instance already:您可以检查您是否已经有一个实例:

 MyControls.ContainsKey(TypeOfUserControl);

And use the one you already have or new up an instance if there's none there.如果没有,请使用您已有的实例或新建一个实例。 And of course add to the dictionary.当然也可以添加到字典中。

This dictionary could be held in a dependency injection container or be a static so you can reference it from wherever you like.该字典可以保存在依赖注入容器中,也可以是 static,因此您可以从任何您喜欢的地方引用它。 Or maybe all the usercontrols you care about are instantiated in one place.或者也许您关心的所有用户控件都在一个地方实例化。

You could alternatively use dependency injection and make all your usercontrols singletons.您也可以使用依赖注入并将所有用户控件设为单例。

The exact syntax depending on which DI container you use but for the microsoft one:确切的语法取决于您使用的 DI 容器,但对于 Microsoft 容器:

  services.AddSingleton<YourUserControl>();

If this was particularly problematic ( for reasons I can't imagine ) then you could perhaps use the Lazy instantiation pattern to make your usercontrols effectively statics.如果这特别成问题(出于我无法想象的原因),那么您也许可以使用延迟实例化模式来使您的用户控件有效地静态化。

https://csharpindepth.com/articles/singleton#lazy https://csharpindepth.com/articles/singleton#lazy

Or maybe this is literally two usercontrols in one piece of code and you could simply cache in two private variables.或者,这实际上是一段代码中的两个用户控件,您可以简单地缓存在两个私有变量中。

NOTE:笔记:

I wonder what you're doing in the initialisation.我想知道你在初始化时在做什么。

A common pattern is viewmodel first where a viewmodel instance would be presented to a view and templated into UI.一种常见的模式是 viewmodel 首先是 viewmodel 实例将呈现给视图并模板化到 UI 中。 In this case initialisation logic would be in that viewmodel.在这种情况下,初始化逻辑将在该视图模型中。 That approach perhaps just changes the type of thing you're caching in the dictionary or dependency injection container.这种方法可能只是改变了你在字典或依赖注入容器中缓存的东西的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我们不需要在HttpGet Create中返回View(new Person())? - Why don't we need to return View(new Person()) in HttpGet Create? 尽管我们没有初始化它,但 propertyChanged null 是怎么回事? - How isn't propertyChanged null although we don't initialize it? 如何在不丢失数据的情况下重定向到同一视图 - How can we redirect to same view without data loss 我们如何将javascript var初始化为c#var Asp.net MVC Razor View - how we initialize javascript var to c# var Asp.net MVC Razor View 为什么在 EF Core 中使用 Include(或 ThenInclude)时不需要指定类型? - Why don't we need to specify types when using Include ( or ThenInclude) with EF Core? 遇到异常时渲染错误视图 - Render error view when we encounter an exception 为什么在转换后不需要后缀,但在声明过程中需要后缀呢? - Why we don't need suffix after conversion but during declaration we need it? 我不明白为什么我们需要'new'关键字 - I don't understand why we need the 'new' keyword 如何检查场景是否已加载不要再次加载? - How can I check if a scene is loaded don't load it again? 在列表视图中使用组时如何检查列表视图项 - how to check the list view items when we are using groups in list view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM