Home > Mobile >  Why didn't my WPF window show up even with correct syntax?
Why didn't my WPF window show up even with correct syntax?

Time:03-25

Here's the code

<Window x:Class="WpfTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BobuxNotifierWpfTest"
        mc:Ignorable="d"
        Title="test app" Height="450" Width="800"
        Background="#fff0f6">
    <Grid Width="390" HorizontalAlignment="Left" Margin="10,10,0,10" Background="#ffe8f2">
        <Grid.RowDefinitions>
            <RowDefinition Height="400*"/>
            <RowDefinition Height="30*"/>
        </Grid.RowDefinitions>
        <Label FontSize="35" FontWeight="DemiBold" FontFamily="Fonts\Halyard Text Regular" Margin="10,0,10,10">
            heading
        </Label>
        <TextBlock FontSize="18" FontFamily="Fonts\Halyard Text Regular" FontWeight="DemiBold" Foreground="#633247" Margin="15,65,0,0" TextWrapping="WrapWithOverflow">
            <Label>first body line</Label>
            <Label>second body line</Label>
            <Label FontSize="12" Margin="0,20,0,0">footer</Label>
        </TextBlock>
    </Grid>
</Window>

this code didn't give me any errors but when I run it all I got was

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

I also didn't do anything to the Main() class or the xaml.cs file

any help lol

CodePudding user response:

Application is usually one of the most important classes in your WPF project, not useless.

Encapsulates a Windows Presentation Foundation application. [...]

You can implement an Application using markup, markup and code-behind, or code. If Applicationis implemented with markup, whether markup or markup and code-behind, the markup file must be configured as an Microsoft build engine (MSBuild) ApplicationDefinition item.

You have to tell WPF which window should be shown on startup using StartupUri.

<Application x:Class="WpfTest.MainWindow"
             ...
             StartupUri="MainWindow.xaml">

Another way is adding a Startup event handler and creating your window there.

<Application WpfTest.MainWindow""
             ...
             Startup="OnStartup">
public partial class App : Application
{
   private void OnStartup(object sender, StartupEventArgs e)
   {
      new MainWindow().Show();
   }
}

That being said, it is possible to run a WPF standalone application without an Application instance.

A standalone application does not require an Application object; it is possible to implement a custom static entry point method (Main) that opens a window without creating an instance of Application.

However, it is in all likelihood not what you want to do since the application object provides services like an application-wide resource dictionary to name just one with a big impact.

Application implements the singleton pattern to provide shared access to its window, property, and resource scope services. [...]

  • Application Lifetime: Activated, Current, Deactivated, DispatcherUnhandledException, Exit, Run, SessionEnding, Shutdown, ShutdownMode, Startup.

  • Application-Scope Window, Property, and Resource Management: FindResource, GetContentStream, GetResourceStream, LoadComponent, MainWindow, Properties, Resources, StartupUri, Windows.

  • Command-Line Parameter and Exit Code Processing: Application.Startup, Application.Exit, Application.Shutdown.

  • Navigation: FragmentNavigation, LoadCompleted, Navigated, Navigating, NavigationProgress, NavigationStopped, NavigationFailed, SetCookie, GetCookie.

  • Related