Reputation: 157
I am creating an application in visual studio 2017. I have two .xaml pages and I want to change between the two when a button is clicked. So far my code is:
private void bakingButton(object sender, RoutedEventArgs e)
{
CookingMenu cooking = new CookingMenu();
cooking.show();
}
The page I am on is called BakingMenu.xaml and I want to go to CookingMenu.xaml. However, this gives the error message: The type or namespace name 'CookingMenu' could not be found (are you missing a using directive or an assembly reference?)
Do I need to reference the other file somewhere else in the code or am I missing something else?
Upvotes: 0
Views: 49
Reputation: 825
You need to include a reference to where CookingMenu
is defined; specifically the namespace it is defined in. If you created both CookingMenu.xaml
and BakingMenu.xaml
in the same folder in your Visual Studio project, and left thier namespaces as the default generated namespaces, then they will be in the same namespace, and you don't need a reference.
If you created them in different folders, however, or edited their namespaces, then they are in different namespaces, and you need a reference.
Think of it like you are using the first name for your page (class), but the compiler doesn't know which "Joe" you are referring to. You need to use its full name.
You can do this reference in-line (although you need to use it EVERY time you reference the CookingMenu
type):
NameSpace.CookingMenu cooking = new NameSpace.CookingMenu();
Where NameSpace
is replaced by whatever the namespace CookingMenu
is declared in (which you can see by opening the C# file for CookingMenu
, and looking near the top... it likely includes several words separated by periods). An example namespace might look like MyApp.Models.Class
. By default, it wraps around your class definition (so it is the outer-most set of curly braces). Do not include "namespace" in your reference, but simply everything that follows it on that line.
Alternatively, you can put a single namespace reference on the top of your file, and then you don't need to include the namespace in every single reference (within that file):
using NameSpace;
Then you can use the code you have written as written.
Here is the documentation on the using
directive: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive.
Hope that helps!
Upvotes: 1