简体   繁体   中英

How to Navigate to Home screen when app re-Launching in UWP

I am Developing Universal windows app,

I want to Send user Directly to Companies Screen if user Re-Launching the app, i am using following block of code but it gives me Null reference exception

 Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
        Windows.Storage.ApplicationDataCompositeValue composite =(Windows.Storage.ApplicationDataCompositeValue)localSettings.Values["exampleCompositeSetting"];
        if (composite == null)
        {
            // No data
        }
        else
        {
            string user = composite["UserId"].ToString();
            Frame.Navigate(typeof(Companies));              
        }

Any one Help me on this Please.

Thanks, Srinivas.

If there is no data, this will be null.

localSettings.Values["exampleCompositeSetting"];

If you cast null to another type, you will get a NullReferenceException.

Use as keyword for this casting. Read docs for “as”, and also “is” keywords if you are not familiar with them.

 Windows.Storage.ApplicationDataCompositeValue composite = localSettings.Values["exampleCompositeSetting"] as (Windows.Storage.ApplicationDataCompositeValue);

It won't throw exception if the value is null, you just need to check if the result is null before using the result.

Also refer to this answer: as vs classic casting

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