Why doesn't this work (NavigationService on Singleton)?
public class Navigator : PhoneApplicationPage
{
private static Navigator _instance;
private static object _lock = new object();
public static Navigator Instance
{
get
{
lock (_lock)
{
if (_instance == null)
{
_instance = new Navigator();
}
return _instance;
}
}
private set
{
lock (_lock)
{
_instance = value;
}
}
}
private Navigator(){}
public bool NavigateTo(string uri)
{
lock (_lock)
{
return NavigationService.Navigate(new Uri(uri, UriKind.Relative));
}
}
}
It is called in a ViewModel class:
Navigator.Instance.NavigateTo("/NotePage.xaml");
So I've got this, and NavigationService.Navigate(..) throws a NullReferenceException.
How can I fix this / what is an alternative? I want to use the NavigationService from a ViewModel class.
I'd prefer a solution without the need for i开发者_Go百科nstalling more componets (MVVM light). If that is absolutely not possible I'll check out the Messenger / Message class.
EDIT I pretty much gave up. I solved my problem by creating a style for hyperlink button which can wrap around everything.
your singleton is not thread safe. look at this implementation:
http://www.yoda.arachsys.com/csharp/singleton.html
精彩评论