Navigating to another XAML file - c#

I have three XAML files, they are mainwindow.xaml,login.xaml,homepage.xaml. Since the files can be navigated through frames, i added a frame to main window which fits the whole screen.
XAML of MainWindow:
<Window x:Class="Myproject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" WindowState="Maximized" Initialized="Window_Initialized">
<Grid>
<Frame Name="pageFrame"></Frame>
</Grid>
</Window>
CS file of MainWindow:
private void Window_Initialized(object sender, EventArgs e)
{
pageFrame.Height = SystemParameters.WorkArea.Height-10;
pageFrame.Width = SystemParameters.WorkArea.Width;
pageFrame.Navigate(new login());
}
It navigates perfectly to login page and perform login actions there.
The problem is it does not navigate to the homepage.xaml from the login.xaml.cs
Code used for navigating to homepage.xaml from login.xaml.cs
MainWindow mw = new MainWindow();
mw.pageFrame.Navigate(new homepage());
This code goes in a if statement section and i checked by using breakpoints if these statements are executed. And it does execute those and the objects are populated but the naviagtion does not occur.
What am i doing wrong? Is this not the correct approach?

The problem is, well, mw is a new window and it is not even shown at all. And you are staying in your old instance of MainWindow, nothing happens to your old MainWindow.
You need to navigate from within your old MainWindow, not a new one.
((MainWindow)(Application.Current.MainWindow)).pageFrame.Navigate(new homepage());
You have a reference to your main window, Application.Current.MainWindow, but you need to cast it into your own type of MainWindow first.

Related

How to prevent/close a WPF window from load?

I have a window, it will do some checking before it is shown.
public class MyDlg : Window
{
public MyDlg()
{
Initialized += new EventHandler(Window_Initialized);
}
private void Window_Initialized(object sender, EventArgs eventArgs)
{
if (!/*do some checking*/)
{
Loaded += (s, e) => Close();
}
}
}
If "do some checking" fail, the above code will close my window immediately after the window is loaded. However this is too late because I can see the window just appear and disappear.
How can I close my window without showing it?
EDIT:
The one who will construct MyDlg is like:
MyDlg dlg = new MyDlg ();
dlg.ShowDialog();
But it is hard for me to prevent calling 'ShowDialog()', because they are written by other people (I'm trying to write MyDlg in some library)
How can I close my window without showing it?
Perform the check before calling the Show or ShowDialog method of the window. You could either do this in the calling code:
MyDlg dlg = new MyDlg();
//perform your check here...
dlg.ShowDialog();
...or in the constructor of the MyDlg window:
public MyDlg()
{
//perform your check here...
}
Obviously the window is already shown by the time the Window_Initialized event handler gets invoked so then it is too late to perform any check if you don't want the window to appear. You cannot close a window that hasn't been opened.
You can create splash dialog inside your new window.
And set IsEnabled=False on window/dialog.
Or if your operation is quick then there is no need for splash. Just hide your window:
<Window x:Class="Wpf.Test01.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:Wpf.Test01"
xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
mc:Ignorable="d"
IsEnabled="False"
WindowStyle="None"
AllowsTransparency="True"
Title="MainWindow" Height="350" Width="525">
<Window.Background>
<SolidColorBrush Opacity="0.0" Color="White" />
</Window.Background>
You can see it done here WPF Window with transparent background containing opaque controls
Of course change the properties back to visible/default if everything is ok

Navigating between pages in wpf not working

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.

Navigation in Windows Universal Apps 10 Not Working (Taking me to a blank page)

and thank you. I'm using Visual Studio 2015 and trying to navigate between 2 pages, MainPage.xaml and Meds.xaml On MainPage.xaml I put this button to navigate to Meds.xaml with.
<Button x:Name="button_meds" Content="Meds" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="268,405,0,0" FontSize="18.667" Click="button_meds_Click"/>
In the MainPage.xaml.cs file, I simply did this
private void button_meds_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(Meds));
}
Meds.xaml has an image and background, but I omitted the logo and grid from the code below
<Page
x:Class="SQLMobileMoodSwing.Meds"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SQLMobileMoodSwing"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
</Page>
I'm not getting any errors, but when I click the button, it simply takes me to a white page. Not sure what I'm doing wrong, have looked for solutions but nothing seems to work.
Thank you!
I have created Meds.xaml as XAML View, not as Blank Page.
Then I deleted it and created again as a Blank Page and BOOM! It works

Textbox created in WPF is not editable at run time

I am creating a WPF Window in which I have text boxes. However, when I run the project in Debug mode, (F5), I am not able to edit the text boxes that I created, nor am I able to choose from the listbox that I created. I googled, found that WPF and Win32 need to communicate to accept keyboard input, and got these 3 lines :
Window w = new Window1();
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(w);
w.Show();
However, I am new to C# and hence I have absolutely no idea where to insert this C# code. I added the System.Windows.Forms and WindowsFormIntegration references to my project.
The window I am designing will be the first window that will appear at the launch of the application, hence I need the textboxes in this window to be editable without launching another window. Kindly guide me.
Edit : This is my XAML code:
<Window x:Name="Window1" x:Class="Myproject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Risk Assessment"
Height="741" Width="1216.091">
<GroupBox x:Name="GroupBox1">
<Grid>
<TextBox x:Name="Length" IsReadOnly ="False" IsEnabled="True" />
</Grid>
</GroupBox>
</Window>
This is my C# code:
namespace Myproject
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
Edit 2: I modified the first line in the App.Xaml code like this :
<Application x:Class="Myproject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup">
And in the App.Xaml.cs I added this snippet:
private void Application_Startup(object sender, StartupEventArgs e)
{
MainWindow win = new MainWindow();
ElementHost.EnableModelessKeyboardInterop(win);
win.Show();
System.Windows.Threading.Dispatcher.Run();
}
But still no luck. Where am I going wrong?
Try to change your Application.xaml to include the StartupUri:
<Application x:Class="Myproject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml"
>
Remove all the startup code you had in the cs file.
Or
Change your cs code to this:
Window1 window1 = new Window1();
this.ShutdownMode = ShutdownMode.OnMainWindowClose;
this.MainWindow = window1;

Run Flash in WPF

i am trying to run a .swf file in my WPF application, i have created a html page and in that i have referenced my .swf file using object tag and then loading that html page in my Main Window
my xaml looks like
<Window x:Class="sirajflash.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<WebBrowser Name="myBrowser"></WebBrowser>
<!--<Frame Name="myframe"/>--> //tried with frame also but no luck
</Grid>
</Window>
assigning the source
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
myBrowser.Source = new Uri(CreateAbsolutePathTo("playflash.htm"), UriKind.Absolute);
}
private static string CreateAbsolutePathTo(string mediaFile)
{
return System.IO.Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName, mediaFile);
}
}
The Problem:
when i run the application the warning occurs that ActiveX content is trying to access etc etc and when i allow it nothing appears in my main window the warning keeps on occuring multiple times.
if i run the flash movie in the browser directly it runs just fine.
Regards.
I have a flash based clock as a .swf file on my C:\Test\MyClock.swf
I have a htm file at C:\Test\MyHtml.htm
<embed src=C:\Test\MyClock.swf
width=200 height=200
wmode=transparent type=application/x-shockwave-flash>
</embed>
I have web browser control as below...
<Window x:Class="MyFlashApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<WebBrowser Source="C:\Test\MyHtml.htm"></WebBrowser>
</Grid>
</Window>
On running the app, I see the webbrowser control giving warning as "To help protect your security, Internet Explorer has restricted this file from showing active content that could access your computer. Click here for options."
I accept the warning by right click and the left click "Allow Blocked Content". A confirmation popup appears to which I say Yes.
I see the Flash based clock.
WebBrowser control can support flash directly . If you don't need to present anything in HTML then you can directly provide the path to the flash file .
myWebBrowser.Source = "C:\Test\MyClock.swf"
However you will still get the IE warning message.

Categories

Resources