GoBack()似乎是在OnNavigatedFrom中传递参数但它不应该,为什么?

时间:2016-10-30 17:00:14

标签: c# xaml uwp

我有一个UWP应用,有两页HomePageEditItemPage。在HomePage我创建了一个按钮,该按钮导航到EditItemPage传递NavigationContext bool isNewItem = true个班级EditItemPage。然后,我在Save上创建新项目,用户更改了一些信息并点击了按钮Item。该按钮将NavigationContext中创建的HomePage传递给Save。问题是,当我点击后退按钮而不点击HomePage按钮时,public sealed partial class EditItemPage : Page { public EditItemPage() { this.InitializeComponent(); } protected override void OnNavigatedFrom(NavigationEventArgs e) { base.OnNavigatedFrom(e); ViewModel.CleanNavigationBackStack.Execute(e.Uri); } protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); if (e.Parameter is NavigationContext) { var context = (NavigationContext) e.Parameter; if (context.ContextType == NavigationContextType.ShareOperation) { ViewModel.HandleNewUri.Execute(context.ShareOperation); } else if (context.ContextType == NavigationContextType.FeedItem) { if (context.IsNewItem) ViewModel.HandleNewFeedItem(); else { ViewModel.HandleExistingFeedItem(context.FeedItem); } } else throw new ArgumentOutOfRangeException(); } } private EditItemViewModel ViewModel => this.DataContext as EditItemViewModel; } 以某种方式获得该项目。

EditItemPage.xaml.cs

public sealed partial class HomePage : Page
{
    public HomePage()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        if (e.Parameter is NavigationContext)
        {
            var context = (NavigationContext) e.Parameter;
            if (context.ContextType == NavigationContextType.FeedItem)
            {
                if (context.IsNewItem)
                    ViewModel.HandleNewFeedItem(context.FeedItem);
            }
            else
                throw new ArgumentOutOfRangeException();
        }
    }

    private FeedViewModel ViewModel => this.DataContext as FeedViewModel;
}

HomePage.xaml.cs

public class NavigationService : INavigationService
{
    private readonly Dictionary<string, Type> _pagesDictionary;
    private Frame _mainFrame;

    public const string UnknownPageKey = "-- UNKNOWN --";

    public NavigationService()
    {
        _pagesDictionary = new Dictionary<string, Type>();
    }

    public void Configure(string key, Type pageType)
    {
        if (key == null || pageType == null) return;
        Type value;
        if (_pagesDictionary.TryGetValue(key, out value))
        {
            if (value != pageType)
            {
                throw new Exception(
                    $"Attempt to add a page of type '{pageType}' to already existing pair of '{key}:{value}'. Consider another string.");
            }
        }
        else
        {
            _pagesDictionary.Add(key, pageType);
        }
    }


    public void GoBack()
    {
        if (EnsureMainFrame() && _mainFrame.CanGoBack)
            _mainFrame.GoBack();
    }

    public void NavigateTo(string pageKey)
    {
        if (pageKey == null) return;
        Type page;
        if (_pagesDictionary.TryGetValue(pageKey, out page))
        {
            if (EnsureMainFrame())
            {
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
                    AppViewBackButtonVisibility.Visible;
                _mainFrame.Navigate(page, null,
                    new Windows.UI.Xaml.Media.Animation.SuppressNavigationTransitionInfo());
            }
        }
        else
        {
            throw new Exception($"There is no page associated with string '{pageKey}'.");
        }
    }

    public void NavigateTo(string pageKey, NavigationContext context)
    {
        if (pageKey == null) return;
        Type page;
        if (_pagesDictionary.TryGetValue(pageKey, out page))
        {
            if (EnsureMainFrame())
            {
                SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
                    AppViewBackButtonVisibility.Visible;
                _mainFrame.Navigate(page, context,
                    new Windows.UI.Xaml.Media.Animation.SuppressNavigationTransitionInfo());
            }
        }
        else
        {
            throw new Exception($"There is no page associated with string '{pageKey}'.");
        }
    }

    private bool EnsureMainFrame()
    {
        if (_mainFrame != null) return true;
        var appShell = Window.Current.Content as AppShell;
        if (appShell != null) _mainFrame = appShell.AppFrame;
        return _mainFrame != null;
    }

    public string CurrentPageKey
    {
        get
        {
            string key;
            try
            {
                key = _pagesDictionary.FirstOrDefault(x => x.Value == _mainFrame.CurrentSourcePageType).Key;
            }
            catch (NullReferenceException)
            {
                return UnknownPageKey;
            }
            return key;
        }
    }

    public void RemoveLast()
    {
        if (EnsureMainFrame() && _mainFrame.CanGoBack)
        {
            _mainFrame.BackStack.RemoveAt(_mainFrame.BackStackDepth - 1);
        }
    }
}

的NavigationService

GoBack()

我已经读过OnNavigatedFrom没有传递任何参数但是当我点击它时方法NavigationContext以某种方式得到那个旧的Array ( [0] => 1 [1] => 2 )。当我犯错误时告诉我:)

0 个答案:

没有答案