Navigating between pages in wpf not working - c#

I've done the simplest thing. I open a new window and put a frame in it and I wanted to show in the frame a page.
The window code:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Page1 p = new Page1();
navigator.NavigationService.Navigate(p);
}
}
navigator is the frame, in Page1 I have black background color to see the difference. When I run it I still see the window and not the page that should be inside the frame. Why isn't this working?
Window:
Page:
But i get the white one.
The code for page 1:
<Page x:Class="test.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="269" d:DesignWidth="292"
Title="Page1">
<Grid Background="Black">
</Grid>
In the cs side i didn't write anything.

You need to set the Content property of the Page to something to be able to actually see it:
<Page x:Class="test.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="269" d:DesignWidth="292"
Title="Page1">
<Grid Background="Black">
<TextBlock>PAGE1</TextBlock>
</Grid>
</Page>
An empty black Grid is not visible but if you put a TextBlock in it you should be able to see it.

Related

Open previous TabViewItems after page navigation of NavigationView class

I want to save the opened TabViewItems dynamically, so that the user can switch to another page and return to his work, for example.
Problem:
Open previous TabViewItems after page navigation of NavigationView class
If you initialize the TabView's DataSource in the Page ctor, you could set the page's NavigationCacheMode as Required like the following
<Page
x:Class="App19.MainPage"
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:local="using:App19"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
Background="Transparent"
NavigationCacheMode="Required"
mc:Ignorable="d">

WPF Page Title Bar Hide

I know how to hide the title bar in windows: WindowStyle="none". But how can i achieve that with Pages?
I have a code like this:
<Page x:Class="SomeNamePage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="SomeName"
mc:Ignorable="d"
d:DesignHeight="700" d:DesignWidth="700" WindowHeight="700" WindowWidth="700"
Title="Page1" Background="#FFFFFF">
How can i hide the title bar with the same results as WindowStyle="None"?
I figured out that's not possible. I solved using a Window as holder for the navigation menu and a frame that switches between Pages for the content.
Hope this helps others!

WPF - canvas declaration xaml

Is it possible to declare a canvas in xaml without it being local to the MainWindow.xaml.cs page?
I have created a class called ImageBoard, but I need this class to have the xaml canvas held locally within it - and not within Mainwindow.xaml.cs.
What I want is the ImageBoard to act as the manager of this canvas.
The only other way I can imagine doing this is to have the ImageBoard derive from Canvas itself, so that it is the canvas. Then I could place this into the MainWindow.
At the moment I have this.
Xaml:
<Canvas HorizontalAlignment="Center" Height="370" VerticalAlignment="Center" Width="688"
x:Name="canvas1" Background="#f6f6f6"/>
But it can only be called from the MainWindow, as it is part of the MainWindow. Because of this I have to pass it to the ImageBoard to perform various Methods.
What I think would be best is this
Public class ImageBoard : Canvas
which would eliminate the need for passing the canvas.
I did try to declare a reference to the canvas, via the ImageBoard's constructor, but I think this would be bad practice; as I am, essentially, using the ImageBoard for all drawing purposes anyway. Why keep a canvas when I can turn the ImageBoard into a canvas, right?
You could build a ImageBoard UserControl:
XAML:
<UserControl x:Class="WpfMagic.ImageBoard"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Canvas x:Name="canvas" Background="Transparent" MouseLeftButtonUp="Canvas_MouseLeftButtonUp"></Canvas>
</Grid>
</UserControl>
Code behind:
using System.Windows.Controls;
using System.Windows.Input;
namespace WpfMagic
{
public partial class ImageBoard : UserControl
{
public ImageBoard()
{
InitializeComponent();
}
private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
TextBlock tb = new TextBlock { Text = "*", FontSize = 20 };
tb.SetValue(Canvas.TopProperty, e.GetPosition(canvas).Y);
tb.SetValue(Canvas.LeftProperty, e.GetPosition(canvas).X);
canvas.Children.Add(tb);
}
}
}
And from you main page you can do:
<Window x:Class="WpfMagic.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfMagic"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:ImageBoard></local:ImageBoard>
</Grid>
</Window>
This way you never directly manipulate the Canvas, it is abstracted away from the code using the ImageBoard, this is encapsulation.

Can't access Windows 8 Store user control from code

I'm writing a Windows 8 Store application and within that I've designed my own user control.
Here is the code for my usercontrol (This is a dummy control but the problem exists with this):
<UserControl
x:Class="Windows8StoreTest.TestUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows8StoreTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Width="70"
Height="40">
<StackPanel>
<Button Content="Hello" Foreground="Pink" BorderBrush="Pink"/>
</StackPanel>
</UserControl>
I've dropped the user control onto my page and give it a name:
<Page
x:Class="Windows8StoreTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows8StoreTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<local:TestUserControl Name="testControl"/>
</Grid>
</Page>
However, when I go to the code behind I can't access the control by that name. It doesn't seem to exist! What is weird is that the control doesn't exists within InitializeComponent() method for the MainPage class which will be why it does exist.
What am I missing from my user control?
I'm using Windows 8 Store, XAML, c#.
Thanks in advance
Try to use this:
<local:TestUserControl x:Name="testControl"/>
Should work...
hello i don't know what is wrong but it should work.i have just made a sample example of it..i am putting it here hope you have done the same way.
<Page
x:Class="App12.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App12"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<local:MyUserControl1 x:Name="hellousercontrol" />
</Grid>
in my mainpage.cs.. i have just use it like this..
public MainPage()
{
this.InitializeComponent();
hellousercontrol.Height = 100;
}
one more this..have build your solution ?
I had the same issue in c++ environment. I observed, I didn't had default constructor in my class, as soon as I added the default constructor, I could use the defined UserControl in my project through XAML file. However without default constructor I was able to use it from within c++ code.

How can i link a canvas on a grid, to the mainwindow.xaml?

I have a canvas in a grid, i want to keep my canvas on that grid, because its the first window, that opens in my program.
In my MainWindow.xaml, i have a ContentPage, that always changes its content, the startup content is the authenticationPage. In this page i have a Canvas that shows my skeletal tracking, and is used for making a gesture. This gestureCanvas is on my authenticationPage. The code behind this gestureCanvas is on my MainWindow.xaml.cs.
I need to link the gestureCanvas with my MainWindow.xaml.cs, because the code is behind MainWindow, and it's going to be used there, because it's an Kinect application.
How to link these ?
partial class MainWindow
{
void LoadCircleGestureDetector()
{
using (Stream recordStream = File.Open(circleKBPath, FileMode.OpenOrCreate))
{
circleGestureRecognizer.TraceTo(gesturesCanvas, Colors.Red);
}
}
}
This is my authenticationPage
<UserControl
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:smartHome2011"
xmlns:MyUserControl="clr-namespace:MyUserControl;assembly=MyUserControl"
mc:Ignorable="d"
x:Class="smartHome2011.AuthenticationPage"
x:Name="UserControl"
d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<Grid x:Name="kinectGrid" HorizontalAlignment="Left">
<Viewbox Margin="204,220,430,220">
<Grid ClipToBounds="True" Margin="204,220,430,220">
**<Canvas x:Name="gesturesCanvas" />**
<Canvas x:Name="kinectCanvas"></Canvas>
</Grid>
</Viewbox>
</Grid>
</Grid>
at your code behind MainWindow you can try following
var gesturesCanvas = YourContentPage.FindName("gesturesCanvas") as Canvas;
if (gesturesCanvas != null) {
// do something
}
hope this helps

Categories

Resources