- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在开发一个 WPF MVVM 应用程序,使用 MVVM Light 作为 MVVM 框架,Entity Framework 作为 ORM,MS 同步框架作为将 Local Sql Compact DB 与在线 SQL 数据库同步的手段。该应用程序的范围也相当复杂,因为它旨在管理 Assets ,计算该 Assets 在其生命周期内的使用磨损。谢天谢地,我对所有这些技术都是新手,所以无知是幸福 :) 我找到了很多关于创建工作单元模式和存储库模式的教程和信息。但是,我正在使用新的 DbContext,我已经阅读过它已经使用了这两种模式。
我当前的问题与在 Entity Framework 中使用新的 DbContext 有关。我在 VS 中使用过 DbContext Generator 模板,所以我有一个 MyDbModelContainer。我直接从我的 View 模型中使用了它,它为每个 VM 创建了一个上下文,我很确定这是一个坏主意。这是我的父/子类型数据输入场景的构造函数,我在此处构造容器,它们使用 MVVM Light 将选定项目的消息传递给子 VM。
Private FatigueDbContext As FatigueModelContainer
Public Sub New()
If IsInDesignMode = False Then
FatigueDbContext = New FatigueModelContainer
FatigueDbContext.CtMaterials.Load()
CtMaterialsCollection = FatigueDbContext.CtMaterials.Local
FatigueDbContext.CtManufactures.Load()
CtManufactures = FatigueDbContext.CtManufactures.Local
End If
End Sub
Public Class ShellViewModel
Inherits ViewModelBase
#Region "Fields"
Private _changePageCommand As ICommand
Private _currentPageViewModel As IPageViewModel
Private _pageViewModels As List(Of IPageViewModel)
#End Region
Public Sub New()
Dim DialogService As New ModalDialogService
' Add available pages
PageViewModels.Add(New ImportJobDataViewModel(DialogService))
PageViewModels.Add(New TestViewModel())
PageViewModels.Add(New ReverseBendUtilityViewModel(DialogService))
' Set starting page
CurrentPageViewModel = PageViewModels(0)
End Sub
#Region "Properties / Commands"
Public ReadOnly Property ChangePageCommand() As ICommand
Get
If _changePageCommand Is Nothing Then
_changePageCommand = New RelayCommand(Of IPageViewModel)(Sub(param) ChangeViewModel(param))
End If
Return _changePageCommand
End Get
End Property
Private Function IsViewPageModel(viewPageModel As IPageViewModel) As Boolean
If TypeOf (viewPageModel) Is IPageViewModel Then
Return True
Else
Return False
End If
End Function
Public ReadOnly Property PageViewModels() As List(Of IPageViewModel)
Get
If _pageViewModels Is Nothing Then
_pageViewModels = New List(Of IPageViewModel)()
End If
Return _pageViewModels
End Get
End Property
Public Property CurrentPageViewModel() As IPageViewModel
Get
Return _currentPageViewModel
End Get
Set(value As IPageViewModel)
If _currentPageViewModel IsNot value Then
_currentPageViewModel = value
RaisePropertyChanged(Function() CurrentPageViewModel)
End If
End Set
End Property
#End Region
#Region "Methods"
Private Sub ChangeViewModel(viewModel As IPageViewModel)
If Not PageViewModels.Contains(viewModel) Then
PageViewModels.Add(viewModel)
End If
CurrentPageViewModel = PageViewModels.FirstOrDefault(Function(vm) vm Is viewModel)
End Sub
#End Region
End Class
最佳答案
您可以在不真正触及数据库上下文的情况下解决此问题。这个想法是,即使 VM 留在内存中,您也只需要在 VM 的 View 处于事件状态时初始化模型实例。
Caliburn.Micro: Screens and Conductors 中有一个很好的解决方法,但它实际上并不特定于 mvvm 框架。
在最基本的情况下,正如 Rachel 在上面的评论中提到的,你扩展你的 IPageViewModel
包括处理 VM 的激活和停用逻辑的函数:激活逻辑在 VM 被激活时调用,停用逻辑在 VM 被停用时调用。
激活/停用的实际调用将在 IPageViewModels
的容器中进行。 , 例如:
Public Property CurrentPageViewModel() As IPageViewModel
Get
Return _currentPageViewModel
End Get
Set(value As IPageViewModel)
If _currentPageViewModel Is value Then
Return
End If
If _currentPageViewModel IsNot Nothing Then
_currentPageViewModel.Deactivate()
End If
_currentPageViewModel = value
If value IsNot Nothing Then
value.Activate()
End If
RaisePropertyChanged(Function() CurrentPageViewModel)
End Set
End Property
关于wpf - 将生成的 ADO.Net DbContext 放入 View-Models 的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11819598/
我试图在 Eclipse v3.7.2 中将 loopj .jar 库添加到我的项目中 首先,我将 .jar 添加到“lib”目录中,右键单击它并选择“添加到构建路径”。它编译得很好,但在执行时出现错
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Passing two-dimensional array via pointer int table[20
我在 Grafana 中的图表每隔几秒钟就会自动更新一次。随着数据的进入,右侧的最后一个数据点会暂时下降。最终会显示正确的值,但在几次更新时该值较低。这是正常的吗?可以修复吗? 最佳答案 也许,这会有
我不明白为什么我会收到臭名昭著的“IllegalStateException”以及以下代码: private void mergeQueryStrings(String url, Map parame
您好,我正在通过 .php 文件中的 JSON 回显将测试 Android 应用程序链接到 MySQL 数据库。 我能够用整个数据填充 ArrayList,但现在我想将数据分离到变量中,但我无法真正找
我想仅将对象的数据成员的值写入文件,因此这里我不能使用序列化,因为它会写入很多内容其他我不需要的信息。这是我通过两种方式实现的。一种使用字节缓冲区,另一种则不使用它。 不使用 ByteBuffer:第
可能是个简单的问题,但我似乎找不到答案。我正在动态创建一个页面,我可以在其中共享 Twitter 链接。 var twitter = document.createElement('a'); tw
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
尝试获取我在末尾为 $_SESSION 设置的值作为查询中的 user_id,而不是 $username。我似乎无法修改查询。我确信这对于这里的一些专家来说是非常简单的。 if(isset($_POS
有没有人可以帮助我,我有 mysql 查询,我已经在 phpmyadmin 中测试了它: select items.name, items.category, items.supplier_id, i
我正在尝试 push_back()一个„ std::vector 的符号. 我一直收到错误: character too large for enclosing character literal t
我有一个存储在 char * 中的压缩图像,我想将它放回 AVPacket,以便我可以将它放入 ffmpeg 解码器。有人可以展示如何做到这一点吗?任何示例或教程将不胜感激。 提前致谢 最佳答案 我向
password = str() while password != "changeme": password = input("Password: ") print("Thou Shall
所以我有一个 Map,其中有一些值被传递到一个方法中: public String doThis(Map context){ ..... } 我正在尝试向该 map 插入附加属性 String abc
我遇到了一些我无法弄清楚的问题...我正在编写一个带有接受拖放的 JList 的 Swing Java 应用程序。我想在将文件或文件夹从我的系统拖到 Java 应用程序上时更改光标。 最佳答案 我自己
我正在尝试确定一些关于如何编写异常消息的指南。 例如,让我们假设一个假设的函数必须接收恒定数量的字节(作为 bytes 对象),我们用 [1, 2, 3]。以下是所有可能的异常(exception)情
使用 JSONObject 发送到网络服务当我们将 double(整数)放入零时,该点将被删除 代码 double d = 123.00; JSONObject json = new JSONObje
在 WPF 中,如何将 DataGrid 放在 ComboBox 中以显示多列?像下面这样的东西似乎没有做任何事情:
我正在尝试使用自定义 QStandardItem 在两个 QListViews 之间进行拖放。 除了this document,我在网上找不到我需要的信息这有点帮助,但现在我被困住了。 从一个 QLi
如何将 PDF 放入 NSData 中?我在应用程序的文档目录中以字符串形式找到了 PDF 的位置。当我尝试通过电子邮件发送时,我在电子邮件正文中看到 PDF(而不是看到附件图标。我不知道这是否正常)
我是一名优秀的程序员,十分优秀!