Benutzer:MovGP0/WPF/Navigation

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      


Setting up a Shell[1]

[Bearbeiten | Quelltext bearbeiten]
App.xaml.cs
public sealed partial class App
{
    protected override void OnStartup(StartupEventArgs e)
    {
        var resolver = ResolverFactory.Get();

        var logger = resolver.Resolve<ILogger>();
        RegisterGlobalExceptionHandling(logger);
        
        var shell = resolver.Resolve<Sample.Shell>();
        shell.Show();
    }
    
    private static void RegisterGlobalExceptionHandling(ILogger log)
    {
        AppDomain.CurrentDomain.UnhandledException += 
            (sender, args) => CurrentDomainOnUnhandledException(args, log);
    }
    
    private static void CurrentDomainOnUnhandledException(
        UnhandledExceptionEventArgs args, ILogger log)
    {
        var exception = args.ExceptionObject as Exception;
        var terminatingMessage = args.IsTerminating 
            ? " The application is terminating." : string.Empty;
        var exceptionMessage = exception?.Message ?? "An unmanaged exception occured.";
        var message = string.Concat(exceptionMessage, terminatingMessage);
        log.Error(exception, message);
    }
}
Shell.xaml
<Window x:Class="Sample.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
</Window>
Shell.xaml.cs
namespace Sample
{
  [TemplatePart(Name="PART_MainFrame", Type=typeof(Frame))]
  public partial sealed class Shell 
  {
    public Shell()
    {
      InitializeComponent();
    }

    public override void OnApplyTemplate()
    {
      MainFrame = Template.FindName("PART_MainFrame") as Frame;
    }
  }
}
Generic.xaml
<ResourceDictionary>
  <Style x:Key="..." x:TargetType="Sample.Shell">
    <Setter Property="Template">
      <Setter.Value>
        <DockPanel>
          <Frame x:Name="PART_MainFrame" />
        </DockPanel>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>
  1. Paul Stovell: WPF Navigation. Abgerufen am 7. November 2016 (englisch).