简体   繁体   English

刷新NavigationView时导航到特定的ContentFrame

[英]Navigating to specific ContentFrame when refreshing NavigationView

I have an app which uses a NavigationView as a UI-control. 我有一个使用NavigationView作为UI控件的应用程序。

Now I have a SettingsPage where I can change the language of my UI between german and english. 现在,我有了一个SettingsPage ,可以在其中更改德语和英语之间的UI语言。

I change the language with this code: 我使用以下代码更改语言:

public static void German()
    {
        Log.Logger.Information("Language = German")

        ApplicationLanguages.PrimaryLanguageOverride = "de-DE";
        DataCollection.Current.LanguageChangedEvent.LanguageChanged();
    }

The last line invokes an EventHandler which in turn invokes the following event on the MainPage.xaml.cs where the NavigationView is located. 最后一行调用EventHandler ,后者又在NavigationView所在的MainPage.xaml.cs上调用以下事件。

public void ChangedLanguage(object source, EventHandlerBase e)
    {
        if (e.GetStatus())
        {

            Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
            Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();

            Frame.Navigate(this.GetType());

            ContentFrame.Navigate(typeof(SettingsPage));
        }
    }

As it's possible to see I just want to return the user to the SettingsPage after changing the language. 可以看到,我只想在更改语言后将用户返回到SettingsPage But I always return back to the initial HomePage that I use when starting the app. 但是,我总是返回到启动应用程序时使用的初始HomePage

The code I use for the NavigationView is derived from the official NavigationView sample provided by Microsoft. 我用于NavigationView的代码来自Microsoft提供的官方NavigationView示例

Is there any possible way to do this? 有什么可行的方法吗? The only possible thing I can imagine is to set a flag after the first page loading and then always check if that flag is set. 我能想象的唯一可能的事情是在第一页加载后设置一个标志,然后始终检查该标志是否已设置。 But then I have the problem that I still could only land at the SettingsPage because I would have to make it the else destination for the flag-check. 但是,我有一个问题,就是我仍然只能进入SettingsPage因为我必须将其SettingsPage为标志检查的else目标。

Would really appreciate a more dynamic way if thats even possible :/ 如果那是可能的话,将不胜感激更动态的方法:/

Greeting, Daniel 丹尼尔,问候

The NavigationView sample just is a simple code sample for your reference. NavigationView示例只是一个简单的代码示例,仅供您参考。 You need to make some changes by your requirements. 您需要根据需要进行一些更改。

I just want to return the user to the SettingsPage after changing the language. 我只想在更改语言后将用户返回到SettingsPage。 But I always return back to the initial HomePage that I use when starting the app. 但是,我总是返回到启动应用程序时使用的初始主页。

I've used the code on the document to make a code sample for testing. 我已经使用了文档上的代码来制作代码样本进行测试。 There're two places will cause your question. 有两个地方会引起您的问题。

First, in the NavView_Loaded event handler, it will always sets the home page as the selected item. 首先,在NavView_Loaded事件处理程序中,它将始终将主页设置为选定项。 But when you change the language, you re-navigate to 'MainPage' and make 'ContentFrame' navigate to 'SettingsPage'. 但是,当您更改语言时,可以重新导航到“ MainPage”,并使“ ContentFrame”导航到“ SettingsPage”。 At this time, the On_Navigated event handler will be first called. 此时,将首先调用On_Navigated事件处理程序。 Then, the NavView_Loaded event handler will be called. 然后,将调用NavView_Loaded事件处理程序。 That's the reason why your app always will return to home page. 这就是为什么您的应用程序总是返回首页的原因。

Second, event if the NavigationView's SelectedItem has been set. 其次,如果已设置NavigationView的SelectedItem,则发生此事件。 But the NavigationView's ItemInvoked event will not be fired. 但是,不会触发NavigationView的ItemInvoked事件 So, what you see actually is not home page, it's a blank Frame control. 因此,您实际上看到的不是主页,而是空白的Frame控件。 You could use SelectionChanged event instead of ItemInvoked event. 您可以使用SelectionChanged事件而不是ItemInvoked事件。 The SelectionChanged event will be fired when you set a new value for the NavigationView's SelectedItem. 当您为NavigationView的SelectedItem设置新值时,将触发SelectionChanged事件。 See the following sample: 请参阅以下示例:

private void NavView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
    if (args.IsSettingsSelected)
    {
        ContentFrame.Navigate(typeof(SettingsPage));
    }
    else
    {
        NavView_Navigate(args.SelectedItem as NavigationViewItem);
    }
}

Then, let's back to your original question: 然后,让我们回到您的原始问题:

I can imagine is to set a flag after the first page loading and then always check if that flag is set. 我可以想象是在第一页加载后设置一个标志,然后始终检查该标志是否已设置。 But then I have the problem that I still could only land at the SettingsPage because I would have to make it the else destination for the flag-check. 但是,我有一个问题,就是我仍然只能进入SettingsPage,因为我必须将其设置为标志检查的else目标。

In my opinion, the flag will not affect you doing other things. 我认为,标志不会影响您执行其他操作。 You completely could define several flags. 您完全可以定义几个标志。 Please see my following code snippet for reference: 请参阅我的以下代码片段以供参考:

public static void German()
{
    Log.Logger.Information("Language = German")
    ApplicationLanguages.PrimaryLanguageOverride = "de-DE";
    ApplicationData.Current.LocalSettings.Values["IsSwitchingLanguage"] = true;
    DataCollection.Current.LanguageChangedEvent.LanguageChanged();
}

In your MainPage.xaml.cs : 在您的MainPage.xaml.cs

public void ChangedLanguage(object source, EventHandlerBase e)
{
        if (e.GetStatus())
        {
            Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
            Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
            Frame.Navigate(this.GetType());

        }
}

In NavView_Loaded event handler, I made some changes: NavView_Loaded事件处理程序中,我进行了一些更改:

private void NavView_Loaded(object sender, RoutedEventArgs e)
{
        ...... 
        foreach (NavigationViewItemBase item in NavView.MenuItems)
        {
            var IsSwitchingLanguage = ApplicationData.Current.LocalSettings.Values["IsSwitchingLanguage"];
            if (IsSwitchingLanguage != null)
            {
                if ((bool)IsSwitchingLanguage)
                {
                    NavView.SelectedItem = NavView.SettingsItem as NavigationViewItem;
                    ApplicationData.Current.LocalSettings.Values["IsSwitchingLanguage"] = false;
                    break;
                }

            }
            if (item is NavigationViewItem && item.Tag.ToString() == "home")
            {
                NavView.SelectedItem = item;
                break;
            }
        }

        ......
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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