简体   繁体   中英

Windows Store App 8.1 Crashes. PRISM

I'm creating a new project from the "PRISM for windows runtime 8.1". And every time i'm trying to click on the settings charm the application crashes. But creating a new project from the 8.0 doesnt crash the app. Anybody knows how to fix this?

The error i get is

Message = "System.NullReferenceException: Object reference not set to an instance of an object.\\r\\n at Microsoft.Practices.Prism.StoreApps.MvvmAppBase.OnCommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)"

Edit: This is a file that autogenerated when crash.

namespace TestApp
{
#if !DISABLE_XAML_GENERATED_MAIN
    public static class Program
    {
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 4.0.0.0")]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        static void Main(string[] args)
        {
            global::Windows.UI.Xaml.Application.Start((p) => new App());
        }
    }
#endif

    partial class App : global::Microsoft.Practices.Prism.StoreApps.MvvmAppBase
    {
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 4.0.0.0")]
        private bool _contentLoaded;

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 4.0.0.0")]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public void InitializeComponent()
        {
            if (_contentLoaded)
                return;

            _contentLoaded = true;
#if DEBUG && !DISABLE_XAML_GENERATED_BINDING_DEBUG_OUTPUT
            DebugSettings.BindingFailed += (sender, args) =>
            {
                global::System.Diagnostics.Debug.WriteLine(args.Message);
            };
#endif
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
            UnhandledException += (sender, e) =>
            {
                if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break(); <---- the debugger stays on this line.
            };
#endif
        }
    }
}

Download the open source project from the project site. Then in VS choose to add an existing project to your solution and add Prism for win rt. Now you can edit and fix the bug. In MvvmAppBase.cs, line 284 add a null check here.

private void OnCommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
{
    if (args == null || args.Request == null || args.Request.ApplicationCommands == null)
    {
        return;
    }

    var applicationCommands = args.Request.ApplicationCommands;
    var settingsCommands = GetSettingsCommands();
    if (settingsCommands == null) { return; }
    foreach (var settingsCommand in settingsCommands)
    {
        applicationCommands.Add(settingsCommand);
    }
}

As ClevelandBuckeye is pointing out, it does look like there is a bug in the Prism code where it doesn't handle the case when no settings have been configured.

You don't have to fix up their source code though, just override GetSettingsCommands in your App class, and return an empty list of commands, like this:

protected override IList<SettingsCommand> GetSettingsCommands()
{
    return new List<SettingsCommand>();
}

If you want to see an example of how this can be fully implemented, have a look at the AdventureWorks reference implementation here .

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