使用Pedros例如,我在我的應用程序之前已經實施的其他服務我總結了以下解決方案在vb.net:
創建一個IFocus接口,該接口可以通過焦點服務或模擬實現
Public Interface IFocusInterface
Sub Focus()
End Interface
創建一個IFocusable接口。這將由ViewModel實現並接受實現IFocusInterface的對象。
Public Interface IFocusable
Property FocusService As IFocusInterface
End Interface
與Singleton模式
Imports Microsoft.Phone.Controls
Public NotInheritable Class FocusService
Implements IFocusInterface
Private Sub New()
End Sub
Private Shared ReadOnly m_instance As New FocusService
Public Shared ReadOnly Property Instance() As FocusService
Get
Return m_instance
End Get
End Property
Public Sub Focus() Implements IFocusInterface.Focus
Dim rootFrame = TryCast(Application.Current.RootVisual, PhoneApplicationFrame)
If Not rootFrame Is Nothing Then
Dim page = TryCast(rootFrame.Content, PhoneApplicationPage)
If Not page Is Nothing Then
page.Focus()
Else
Throw New Exception("Unable to Cast the Root Frame Content into an Application Page")
End If
Else
Throw New Exception("Unable to Cast the RootVisual into a PhoneApplicationFrame")
End If
End Sub
End Class
在您的視圖模型實現的焦點接口實現IFocusable,並確保您在福克斯服務辛格爾頓傳遞到視圖模型視圖模型建成後。
Public Class MyViewModel
Implements INotifyPropertyChanged
Implements IFocusable
' Property for the Focus Service
<Xml.Serialization.XmlIgnore()> Public Property FocusService As IFocusInterface Implements IFocusable.FocusService
Public Sub Focus()
If Not FocusService Is Nothing Then
FocusService.Focus()
Else
Throw New Exception("ViewModel hasn't been passed a Focus Service")
End If
End Sub
End Class
Dim tMyViewModel as New MyViewModel
tMyViewModel.FocusService = Vacation_Calc_Model.FocusService.Instance
關於使用行爲的好處是能夠將它附加到其他控件上,以便通過簡單的綁定實際設置焦點,並且不依賴於只關注於某個服務的服務這一頁。 – 2012-04-27 20:03:45