Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a XAML file internal in .NET

In my Silverlight project, I would like to make an assembly which contains .xaml that can't be seen from outside of the assembly. However, there is no clear way I can do this. When I modify the access modifier on the .xaml.cs file, the compiler tells me:

Partial declarations of "My xaml class here" have conflicting accessibility modifiers    

In the xaml file itself, there doesn't seem to be a place to put my access modifier. In short, how do I set the class associated with my xaml file to internal?

like image 525
user64349 Avatar asked Mar 15 '09 02:03

user64349


People also ask

How do I create a XAML file?

To begin editing your first XAML file, use Visual Studio or Visual Studio for Mac to create a new Xamarin. Forms solution. (Select the tab below corresponding to your environment.) In the Configure your new project window, set the Project name to XamlSamples (or whatever your prefer), and click the Create button.

How do I add an existing XAML file to Visual Studio?

If you drag-and-drop the . xaml file from Windows Explorer into the Solution Explorer window, it will automatically add the . xaml with the code-behind . cs file.

Does XAML work on net framework?

XAML can be used in different platforms such as WPF (Windows Presentation Foundation), Silverlight, Mobile Development, and Windows Store App. It can be used across different . Net framework and CLR (common language runtime) versions.

How add XAML file to WPF?

Just right click the project, choose Add -> Window and that will add a new xaml file along with its corresponding .

How to set the class associated with my XAML file to internal?

In the xaml file itself, there doesn't seem to be a place to put my access modifier. In short, how do I set the class associated with my xaml file to internal? Show activity on this post. You need to add a x:ClassModifier="internal" in the UserControl tag of the XAML file if you change the visibility of the class in the .cs file to internal.

What is XAML and how to use it in a UI?

In a .NET Multi-platform App UI (.NET MAUI) app, XAML is mostly used to define the visual contents of a page and works together with a C# code-behind file. The code-behind file provides code support for the markup. Together, these two files contribute to a new class definition that includes child views and property initialization.

Is it possible to declare an element as internal in XAML?

error CS1527: Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal so it is not possible to do what you are trying to do. Even if it were legal, there would be no way to access the class from other classes anyway. The window can be internal, read more: Making a XAML file internal in .Net

How to create a WPF application from XAML code?

This can be done with the Code keyword in the XAML language namespace, as explained below. The first step is to create the WPF Application project in Visual C# and delete C# source code, i.e., files matching a specified CS extension. The starting point is an XAML template, which is named App.xaml by default.


1 Answers

You need to add a x:ClassModifier="internal" in the UserControl tag of the XAML file if you change the visibility of the class in the .cs file to internal.

The file generated from the XAML file (e.g. MyControl.g.cs) needs to match the code-behind file (e.g. MyControl.cs). x:ClassModifier allows you to control the visibility of the part declaration in the MyControl.g.cs file.

like image 92
chuckj Avatar answered Sep 25 '22 18:09

chuckj