for my WPF-Application I'd like to add a splashscreen to my Project ("Project"). Therefore I added another WPF-Window to my application (called it "SplashScreen") and changed its Build Action in the properties window to 'SplashScreen'.
But when I try to build my solution I get those error messages:
'Project.SplashScreen' does not contain a constructor that takes 1 arguments (App.g.cs)
No overload for method 'Show' takes 1 arguments
The name 'InitializeComponent' does not exist in the current context
That is the code in my App.g.cs Window:
public static void Main() {
SplashScreen splashScreen = new SplashScreen("splashscreen.xaml"); (first error message refers to this line)
splashScreen.Show(true); (second error message to this)
GVPI_Fernwartung.App app = new GVPI_Fernwartung.App();
app.InitializeComponent();
app.Run();
}
I don't really understand where those error messages come from, as this code was generated automatically by Visual Studio. When I add an image to my project and change its 'Build Action' to 'SplashScreen' I get the same error messages.
Does anyone know how to avoid this? I'd appreciate any help!
You should not set the Build Action of a WPF window to SplashScreen. Set it back to Page.
The SplashScreen Build Action should only be used for static images: https://msdn.microsoft.com/en-us/library/cc656886(v=vs.110).aspx
Related
I've attempted to start playing with .Net MAUI and I've setup my development environment following the steps as described in:
https://learn.microsoft.com/en-us/dotnet/maui/get-started/first-app?pivots=windows
https://learn.microsoft.com/en-us/windows/apps/project-reunion/set-up-your-development-environment#required-workloads-and-components
https://learn.microsoft.com/en-us/xamarin/android/get-started/installation/android-emulator/device-manager?tabs=windows&pivots=windows
I've also run the 'maui-check' CLI tool and everything checks out, but when I create a new .NET MAUI App with Visual Studio 2019 v16.11.0 Preview 2.0 (running on Windows 10 Home 20H2), I get the 'The name 'InitializeComponent' does not exist in the current context' build errors. It also doesn't find the references to any controls on the form e.g. 'The name 'CounterLabel' does not exist in the current context'
I've tried almost everything in this post The name 'InitializeComponent' does not exist in the current context which contains suggestions like adding and removing files, making changes and changing them back... basically everything except throwing a penny in a wishing well.
I found that a common mistake is a namespace mismatch, but here is what I have showing that the namespaces are correct:
App.xaml:
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiApp1"
x:Class="MauiApp1.App">
...
</Application>
App.xaml.cs
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;
using System;
using Application = Microsoft.Maui.Controls.Application;
namespace MauiApp1
{
public partial class App : Application
{
public App()
{
InitializeComponent(); <-- This is throwing the build error...
}
protected override IWindow CreateWindow(IActivationState activationState)
{
this.On<Microsoft.Maui.Controls.PlatformConfiguration.Windows>()
.SetImageDirectory("Assets");
return new Microsoft.Maui.Controls.Window(new MainPage());
}
}
}
MainPage.xaml:
ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.MainPage"
BackgroundColor="{DynamicResource PageBackgroundColor}">
...
</ContentPage>
MainPage.xaml.cs
using System;
using Microsoft.Maui.Controls;
namespace MauiApp1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent(); <-- This is throwing the build error...
}
int count = 0;
private void OnCounterClicked(object sender, EventArgs e)
{
count++;
CounterLabel.Text = $"Current count: {count}"; <-- This is throwing the build error...
}
}
}
Any help will be greatly appreciated!
---=== UPDATE ===---
The path to the project I created is c:\develop\c#...... as soon as I copy the project to a folder that doesn't contain 'c#' it works. This clearly causes some parsing in the background to fail.
I faced the same issue and looks like when I created a ContentPage in VS its still pointing to Xamarin Forms. After changing namespace to MAUI, I updated the Build Action(RightClick on Xaml Page>>Properties>>BuildAction) of XAML Page to MauiXaml and it worked for me.
Close VS2022
Open VS2022
Open project with menu VS2022(File - Open - Project...)
What caused it for me was that I had renamed the xaml and xaml.cs file something else but I hadn't updated it in the ContentPage node in the xaml under x:Class
The same error popped in for me being an absolute beginner and missing the ending '>' in the XAML file. So, It could also be XAML errors, which leads to this error.
The path to the project I created is c:\develop\c#...... as soon as I copy the project to a folder that doesn't contain 'c#' it works. This clearly causes some parsing in the background to fail.
A very simple answer but has happened to me a few times.
Make sure you are using the preview version of VS 2022. It is very easy to accidently open up any other version of VS, which will cause the error to occur.
I have found a workaround for this problem. I am a newbie to c# and especially to .net Maui therefore, my apologies if I misinterpreted things differently.
First things first The error is about the context that the InitializeComponent() resides in. Context is nothing but the nuget package dependency that the file is looking for.
To change the context
Open the .cs file and at the top of the editor click the navigation bar as shown in the
Image
Change the context to Windows specific dependency from the list.
And also the other workaround would be to change the build action of the xaml file to MauiXaml as others mentioned
I'm using VS 2022 Preview 17.4.0 Preview 1.0, I don't know why but when you create a new Content Page it creates with the wrong namespace. take a look in the C# file and fix the namespace. It worked for me.
I am using preview version of 17.5 VS2022, I add to do below changes to run the app after adding new page.
*.Xmal Build Action -> BundleResource
*.Xmal.cs Build Action -> Compile
I'm trying to print to stdout with Avalonia from inside my MainWindow.axaml.cs file. I'm unable to view the output of Console.WriteLine. This github ticket says to use System.Diagnostics.Debug.WriteLine, but that doesn't work either. It does however point to this wiki page about Avalonia's logging infrastructure, but that page doesn't have examples of using the logging. After some digging, I saw the following usages throughout a lot of Avalonia code
using Avalonia.Logging;
// ...
Logger.TryGet(/* event level */, /* log area */)?.Log(/* calling object */, "log string")
// example
Logger.TryGet(LogEventLevel.Warning, LogArea.Visual)?.Log(this, "TryGetPointAndTangentAtDistance is not available in Direct2D.");
I've tried specifying different LoggingLevels and LoggingAreas within the main method, as well as placing the logging before and after InitializeComponent in the constructor, but nothing seems to work.
Steps to reproduce:
create new dotnet app with Avalonia.MVMM template
Place any of the mentioned logging attempts (writeline, system diagnostics, avalonia.logging) inside of Views/MainWindow.axaml.cs
dotnet run will open up the welcome to Avalonia window, but the console won't have any output.
Attaching a debugger (VS Debugging or Rider) and viewing the Debug output will show anything logged via Logger.TryGet and System.Diagnostics.Debug.WriteLine. Creating a new project and with the following MainWindow.axaml.cs:
using Avalonia.Logging;
public partial class MainWindow : Window
{
public MainWindow()
{
Logger.TryGet(LogEventLevel.Fatal, LogArea.Control)?.Log(this, "Avalonia Infrastructure");
System.Diagnostics.Debug.WriteLine("System Diagnostics Debug");
InitializeComponent();
}
}
Shows the following output in the Rider debug output:
...
[Control] Avalonia Infrastructure (MainWindow #33726620)
System Diagnostics Debug
...
I've attempted to start playing with .Net MAUI and I've setup my development environment following the steps as described in:
https://learn.microsoft.com/en-us/dotnet/maui/get-started/first-app?pivots=windows
https://learn.microsoft.com/en-us/windows/apps/project-reunion/set-up-your-development-environment#required-workloads-and-components
https://learn.microsoft.com/en-us/xamarin/android/get-started/installation/android-emulator/device-manager?tabs=windows&pivots=windows
I've also run the 'maui-check' CLI tool and everything checks out, but when I create a new .NET MAUI App with Visual Studio 2019 v16.11.0 Preview 2.0 (running on Windows 10 Home 20H2), I get the 'The name 'InitializeComponent' does not exist in the current context' build errors. It also doesn't find the references to any controls on the form e.g. 'The name 'CounterLabel' does not exist in the current context'
I've tried almost everything in this post The name 'InitializeComponent' does not exist in the current context which contains suggestions like adding and removing files, making changes and changing them back... basically everything except throwing a penny in a wishing well.
I found that a common mistake is a namespace mismatch, but here is what I have showing that the namespaces are correct:
App.xaml:
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiApp1"
x:Class="MauiApp1.App">
...
</Application>
App.xaml.cs
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;
using System;
using Application = Microsoft.Maui.Controls.Application;
namespace MauiApp1
{
public partial class App : Application
{
public App()
{
InitializeComponent(); <-- This is throwing the build error...
}
protected override IWindow CreateWindow(IActivationState activationState)
{
this.On<Microsoft.Maui.Controls.PlatformConfiguration.Windows>()
.SetImageDirectory("Assets");
return new Microsoft.Maui.Controls.Window(new MainPage());
}
}
}
MainPage.xaml:
ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.MainPage"
BackgroundColor="{DynamicResource PageBackgroundColor}">
...
</ContentPage>
MainPage.xaml.cs
using System;
using Microsoft.Maui.Controls;
namespace MauiApp1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent(); <-- This is throwing the build error...
}
int count = 0;
private void OnCounterClicked(object sender, EventArgs e)
{
count++;
CounterLabel.Text = $"Current count: {count}"; <-- This is throwing the build error...
}
}
}
Any help will be greatly appreciated!
---=== UPDATE ===---
The path to the project I created is c:\develop\c#...... as soon as I copy the project to a folder that doesn't contain 'c#' it works. This clearly causes some parsing in the background to fail.
I faced the same issue and looks like when I created a ContentPage in VS its still pointing to Xamarin Forms. After changing namespace to MAUI, I updated the Build Action(RightClick on Xaml Page>>Properties>>BuildAction) of XAML Page to MauiXaml and it worked for me.
Close VS2022
Open VS2022
Open project with menu VS2022(File - Open - Project...)
What caused it for me was that I had renamed the xaml and xaml.cs file something else but I hadn't updated it in the ContentPage node in the xaml under x:Class
The same error popped in for me being an absolute beginner and missing the ending '>' in the XAML file. So, It could also be XAML errors, which leads to this error.
The path to the project I created is c:\develop\c#...... as soon as I copy the project to a folder that doesn't contain 'c#' it works. This clearly causes some parsing in the background to fail.
A very simple answer but has happened to me a few times.
Make sure you are using the preview version of VS 2022. It is very easy to accidently open up any other version of VS, which will cause the error to occur.
I have found a workaround for this problem. I am a newbie to c# and especially to .net Maui therefore, my apologies if I misinterpreted things differently.
First things first The error is about the context that the InitializeComponent() resides in. Context is nothing but the nuget package dependency that the file is looking for.
To change the context
Open the .cs file and at the top of the editor click the navigation bar as shown in the
Image
Change the context to Windows specific dependency from the list.
And also the other workaround would be to change the build action of the xaml file to MauiXaml as others mentioned
I'm using VS 2022 Preview 17.4.0 Preview 1.0, I don't know why but when you create a new Content Page it creates with the wrong namespace. take a look in the C# file and fix the namespace. It worked for me.
I am using preview version of 17.5 VS2022, I add to do below changes to run the app after adding new page.
*.Xmal Build Action -> BundleResource
*.Xmal.cs Build Action -> Compile
I've created a custom view called PINControl, inheriting from StackLayout. I have used this view in my XAML like so:
<local:PINControl x:Name="PIN" HorizontalOptions="CenterAndExpand"/>
In the code-behind I have this constructor:
public PINControl () { ... }
I get a System.MissingMethodException when the page is executing the LoadFromXaml method. The message is Default constructor not found for type MyNamespace.PINControl
I heard some talk about linkers, but setting the linker setting to Link SDK assemblies only it cannot build due to The 'LinkAssemblies' task failed unexpectedly
Some insights would be appreciated.
Damn. Xamarin disappoints me. After having Xamarin Studio restarted twice, the issue is resolved. Don't know what the actual solution was.
I'm using Mono.WinForms to create external windows in Unity and it works great except that I get this error message each time I play the scene (but never the first time when Unity is freshly started):
Could not register the window class, win32 error 0.
I figured that it was because that the form classes are not unregistered when I stop the scene. They do get unregistered when I restart Unity and I don't have the problem when I make a build, which makes sense.
I get this popup error twice and it's caused by:
myForm = new Form1();
myForm.Show();
I went through the code of the XplatUIWin32.cs file and found the RegisterWindowClass method. When Mono registers a class it does this:
class_name = string
.Format("Mono.WinForms.{0}.{1}",
System.Threading.Thread.GetDomainID ().ToString (), classStyle);
So I managed to get rid of the second error by doing this:
UnregisterClass("Mono.WinForms.1.0", IntPtr.Zero);
So my question is why does it only work for the 2nd popup error and how do I get rid of the 1st one? I tried to force the garbage collection but it didn't help. I tried to find another registered class name but no luck here neither.
For info I use the mono version of System.Windows.Forms.dll as a reference, compile my code in Visual Studio and import the produced DLL in Unity. It works great apart from those popup errors.
class_name = string.Format ("Mono.WinForms.{0}.{1}", System.Threading.Thread.GetDomainID ().ToString (), classStyle);
Two class name should be:
Mono.WinForms.1.0
Mono.WinForms.1.40
I use dnspy patch the System.Windows.Forms.dll to see the MessageBox info with class_name shown.
I think the classStyle may not fixed, better try through different numbers.
BUT!!
I still get the messagebox shown with “Mono.WinForms.1.40” info