简体   繁体   中英

Multiple binding to RelayCommand in WPF MVVM Light

I have started working with WPF MVVM Light and now I'am trying to navigate between pages.

In the MainWindow I have added a "BackButton"

<Button Command='{Binding Main.GoBack, Mode=OneWay}' />

which is binding to MainViewModel method "RelayCommand GoBack".

private RelayCommand _goBack;
    public RelayCommand GoBack
    {
        get
        {
            return _goBack
                ?? (_goBack = new RelayCommand(
                () =>
                    _navigationService.GoBack();
                }));
        }
    }

Why is this button changing view only once? If I want to click it secound time it doesn't work (nothing happend). If I change page for another by another button its starting work again and againg only for once.

Part of implementation of FrameNavigationService:

public FrameNavigationService()
    {
        _pagesByKey = new Dictionary<string, Uri>();
        _historic = new List<string>();
    }
    public void GoBack()
    {
        if (_historic.Count > 1)
        {
            _historic.RemoveAt(_historic.Count - 1);
            NavigateTo(_historic.Last(), null);
        }
    }
    public void NavigateTo(string pageKey)
    {
        NavigateTo(pageKey, null);
    }

    public virtual void NavigateTo(string pageKey, object parameter)
    {
        lock (_pagesByKey)
        {
            if (!_pagesByKey.ContainsKey(pageKey))
            {
                throw new ArgumentException(string.Format("No such page: {0} ", pageKey), "pageKey");
            }

            var frame = GetDescendantFromName(Application.Current.MainWindow, "MainFrame") as Frame;

            if (frame != null)
            {
                frame.Source = _pagesByKey[pageKey];
            }
            Parameter = parameter;
            _historic.Add(pageKey);
            CurrentPageKey = pageKey;
        }
    }

What can I do to handle this? May be I should do it tottaly differently?

You should possibly not be doing goback at all.

Unless you really want to use the journal, using a frame and pages is a bad idea. It's a rare requirement to go back to the last view in desktop apps. What with them not being a web browser.

Maybe you have that requirement though.

If you have a frame then you have it's journal and you can just call goback on the frame's navigationservice. https://docs.microsoft.com/en-us/dotnet/api/system.windows.navigation.navigationservice.goback?view=netframework-4.8

You set keepalive on pages. https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.page.keepalive?view=netframework-4.8

You wrote that code and it seems to be largely reproducing navigationservice functionality. From what you've shown us.

As it is.

Use type rather than a magic string as the key. A type is checked at compile time, a magic string is not and you can make mistakes.

Have you explored this issue at all? I think maybe this is one of those times that telling someone what they did wrong isn't really helping as much as telling them how they ought to diagnose.

Debugging is a key skill for any developer.

You have the code running in front of you.

Put break points in, step through and examine what is happening.

When you navigate, what ends up in _historic?

When you goback, what happens exactly?

When you click the goback that second time what path does it go down and what state is causing that.

确保在GalaSoft.MvvmLight.CommandWpf中使用RelayCommand,而不是在GalaSoft.MvvmLight.Command.RelayCommand中使用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM