简体   繁体   English

UWP Frame在Frame.Navigate方法中为null

[英]UWP Frame is null in Frame.Navigate method

I'm implementing a function in my UWP app (LogoutUser) that logs out the user and redirects him to the LoginPage. 我在UWP应用(LogoutUser)中实现了一个功能,该功能可以注销用户并将其重定向到LoginPage。 LogoutUser has a parameter of type Page, that specifies the page from where the function has been called. LogoutUser具有类型为Page的参数,该参数指定从中调用函数的页面。

I need this parameter for calling the method "parameter".Frame.Navigate(typeof(LoginPage)) so it redirects the user to the login. 我需要此参数来调用方法“ parameter” .Frame.Navigate(typeof(LoginPage)),以便将用户重定向到登录名。

I'm calling this method when a Logout button is pressed and automatically, by a timer, if the Token that the user uses is expired. 如果用户使用的令牌已过期,则在按下“注销”按钮并自动按计时器自动调用此方法时。

My application can start on two different pages: 我的应用程序可以在两个不同的页面上启动:

  • LoginPage if there is no token saved in a json file locally; 如果在本地json文件中没有保存令牌,则为LoginPage;
  • HomePage if the token is present. 主页(如果存在令牌)。

My problem is that when the application starts on the HomePage and the token is expired, the function gets called with the Page being the LoginPage but the Frame being null. 我的问题是,当应用程序在HomePage上启动并且令牌已过期时,该函数将以Page为LoginPage但Frame为null的方式被调用。

I've tried fixing it by restarting the app, if the Frame is null, with the method CoreApplication.RequestRestartAsync but I don't really like this workaround and when the app is restarted it doesn't "open" automatically (It's in the taskbar but the user has to click on it twice to open it). 我曾尝试通过使用CoreApplication.RequestRestartAsync方法重新启动应用程序(如果Frame为空)来修复它,但我真的不喜欢这种解决方法,并且当应用程序重新启动时,它不会自动“打开”(位于任务栏,但用户必须单击两次才能将其打开)。

Function that gets called by the timer on tick. 计时器在滴答时调用的函数。 The function is inside the LoginPage and I'm passing "this" to the LogoutUser function. 该函数位于LoginPage内部,我将“ this”传递给LogoutUser函数。

private async void TimerScadenzaToken(object sender, object e)
        {
            await LoadGlobals();
            _clientAuth.DefaultRequestHeaders.Authorization = new HttpCredentialsHeaderValue("Bearer", _globals.Token.ToString());

            string checkToken = _globals.UriAuth.ToString() + "authentication";
            Uri uriAuthCheckToken = new Uri(checkToken);

            HttpResponseMessage risposta = await _clientAuth.GetAsync(uriAuthCheckToken);
            if (risposta.IsSuccessStatusCode)
            {
                return;
            }
            else
            {
                LogoutUser(this);
                timer.Stop();
                return;
            }
        }

LogoutUser function that also deletes the token from inside the json file and calls an api to delete it from database. LogoutUser函数还从json文件内部删除令牌,并调用api从数据库中将其删除。 You can see my workaround at the end of the function if the Frame is null. 如果Frame为null,则可以在函数末尾看到我的解决方法。

public void LogoutUser(Page pageProvenienzaRichiesta)
        {
            Task asyncLoadGlobals = Task.Run(async () => await LoadGlobals());
            asyncLoadGlobals.Wait();

            string token = _globals.Token.ToString();

            dynamic tokenDinamico = new
            {
                token
            };

            string tokenDaInviare = JsonConvert.SerializeObject(tokenDinamico);
            HttpStringContent httpStringContent = new HttpStringContent(tokenDaInviare, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");

            string deleteToken = _globals.UriAuth.ToString() + "authentication/deleteToken";
            Uri uriAuthDeleteToken = new Uri(deleteToken);

            var eliminaToken = _clientAuth.PostAsync(uriAuthDeleteToken, httpStringContent);
            eliminaToken.AsTask().Wait();

            Task asyncDeleteJsonLocal = Task.Run(async () => await new Globals().DeleteJsonToken());
            asyncDeleteJsonLocal.Wait();

            if (pageProvenienzaRichiesta.Frame != null)
            {
                pageProvenienzaRichiesta.Frame.Navigate(typeof(LoginPage));
            }
            else
            {
                Task asyncRestart = Task.Run(async () => await Reboot());
                asyncRestart.Wait();
            }
        }

I'm calling async methods in this way because I'm calling, sorry for the repetition, the LogoutUser method inside a constructor and UWP freezes if I use .Wait(). 我之所以以这种方式调用异步方法,是因为我在重复,抱歉,构造函数内部的LogoutUser方法和UWP在使用.Wait()时冻结。

I hope you can help me fix this either by showing me if I've done something wrong or by telling me another way of doing this. 希望您可以帮助我解决此问题,方法是向我展示我做错了什么,或者告诉我另一种解决方法。

If you need others informations ask me anything. 如果您需要其他信息,请问我。

Thanks to anyone who is willing to help me. 感谢任何愿意帮助我的人。

I fixed it by removing the app restart and using this piece of code in the last else statement. 我通过删除应用程序重新启动并在最后的else语句中使用了这段代码来修复它。 You could optimize this better but I'm fine with it like this :) 您可以对此进行更好的优化,但是我可以这样:)

if (pageProvenienzaRichiesta.Frame != null)
            {
                pageProvenienzaRichiesta.Frame.Navigate(typeof(LoginPage));
            }
            else
            {
                Frame navigationFrame = Window.Current.Content as Frame;
                navigationFrame.Navigate(typeof(LoginPage));
            }

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

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