开发者

Why doesn't this work (NavigationService on Singleton)?

开发者 https://www.devze.com 2023-04-09 22:26 出处:网络
public class Navigator : PhoneApplicationPage { private static Navigator _instance; private static object _lock = new object();
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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号