namespace MyApp
{
public sealed partial class MainWindow : Window
{
AppWindow m_appWindow;
public MainWindow()
{
this.InitializeComponent();
m_appWindow = GetAppWindowForCurrentWindow();
}
private AppWindow GetAppWindowForCurrentWindow()
{
IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
WindowId myWndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
return AppWindow.GetFromWindowId(myWndId);
}
private void SwitchPresenter_FullScreen(object sender, RoutedEventArgs e)
{
m_appWindow.SetPresenter(AppWindowPresenterKind.FullScreen);
}
}
}
SwitchPresenter_FullScreen function is works but how can i set my app's default window mode to full screen? Can i call SwitchPresenter_FullScreen while app starting?
No. Don't call SwitchPresenter_FullScreen directly. This method is for your UI controls (like CheckBox or ToggleButton).
Just add this line to your MainWindow constructor.
public MainWindow()
{
this.InitializeComponent();
m_appWindow = GetAppWindowForCurrentWindow();
m_appWindow.SetPresenter(AppWindowPresenterKind.FullScreen); // This line
}
Related
I have the following window using Windows App SDK 1.1.5 and WinUI 3, I want to keep it always at the bottom, just above the desktop, m_AppWindow.MoveInZOrderAtBottom(); moves it to the very bottom as I intended, but once it is pressed it comes back to the foreground.
How can I prevent it from coming to the foreground once clicked? I suppose it may be related to the HWND handle.
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using System;
using WinRT.Interop;
namespace Widgets {
public sealed partial class MainWindow : Window {
private AppWindow m_AppWindow;
public MainWindow() {
this.InitializeComponent();
m_AppWindow = GetAppWindowForCurrentWindow();
m_AppWindow.MoveInZOrderAtBottom();
}
private AppWindow GetAppWindowForCurrentWindow() {
IntPtr hWnd = WindowNative.GetWindowHandle(this);
WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
return AppWindow.GetFromWindowId(wndId);
}
}
}
This is just a slightly modified MainWindow.cs from the visual studio Blank App, Packaged (WinUI 3 in Desktop) C# template.
Thanks in advance.
You can try it this way.
public sealed partial class MainWindow : Window
{
private AppWindow m_AppWindow;
public MainWindow()
{
this.InitializeComponent();
m_AppWindow = GetAppWindowForCurrentWindow();
m_AppWindow.Changed += M_AppWindow_Changed;
}
private void M_AppWindow_Changed(AppWindow sender, AppWindowChangedEventArgs args)
{
if (args.DidZOrderChange is true)
{
m_AppWindow.MoveInZOrderAtBottom();
}
}
private AppWindow GetAppWindowForCurrentWindow()
{
IntPtr hWnd = WindowNative.GetWindowHandle(this);
WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
return AppWindow.GetFromWindowId(wndId);
}
}
I have a WPF application and here is the application structure:
Views
MainWindow.xaml, ABC.xaml (all with .cs files)
ViewModels
MainWindowVM.cs, ABCVM.cs
I have a Button in MainWindow.xaml (bound to MainWindowVM.cs) that calls a function, SampleFunction() in MainWindowVM.cs when being clicked and the SampleFunction() then creates a new instance of ABC.xaml (bound to ABCVM.cs) and open a new window of ABC.xaml using Show() function.
How can I make sure that clicking the Button in MainWindow would not open another new window of ABC.xaml when the old window is still there, or not create another new instance of ABC.xaml?
MainWindow.xaml.cs
public partial class MainWindow : Window
{
/*...Some other codes...*/
private MainWindowVM _VM = new MainWindowVM();
public MainWindowVM MainWindowVM
{
get { return _VM; }
set { _VM= value; }
}
public MainWindow()
{
InitializeComponent();
this.DataContext = MainWindowVM;
}
private void SomeControl_MouseLeftButtonUp(object sender,MouseButtonEventArgs e)
{
MainWindowVM.SampleFunction();
}
}
MainWindowVM.cs
public class MainWindowVM
{
/*...Some other codes...*/
public void SampleFunction()
{
ABC abc= new ABC();
abc.Show();
}
}
ABC.xaml.cs
public partial class ABC: Window
{
/*...Some other codes...*/
private static ABCVM _abcVM= new ABCVM();
public ABCVM ABCVM { get { return _abcVM; } set { _abcVM = value; } }
public ABC()
{
InitializeComponent();
this.DataContext = ABCVM;
}
}
Use ShowDialog() instead of Show().
Then you have to close the ABC.xaml first, before you can make something on the MainWindow. So you can't open a second ABC.xaml Window.
You can write code to check whether a window type object exists or not.
for each(Window win in Application.Current.Windows)
{
string windowType = win.GetType().ToString();
if (!windowType.Equals(nameSpace + "." + ABC))
{
ABC abc= new ABC();
abc.Show();
}
}
So this sounds strange but I always get a stackoverflow exception when I execute 'this.Content' 3 times.
So I have a main window which stores all userControls so I dont have to create them always:
public partial class MainWindow : Window
{
CreateSessionWindow csw;
RateSessionWindow rsw;
CloseSessionWindow closesw;
MainMenuWindow mmw;
public MainWindow()
{
InitializeComponent();
csw = new CreateSessionWindow();
rsw = new RateSessionWindow();
closesw = new CloseSessionWindow();
mmw = new MainMenuWindow();
AllSessionWindows.csw = csw;
AllSessionWindows.rsw = rsw;
AllSessionWindows.closesw = closesw;
AllSessionWindows.mmw = mmw;
}
private void bttnStartProgram_Click(object sender, RoutedEventArgs e)
{
this.Content = AllSessionWindows.mmw;
}
}
public static class AllSessionWindows
{
public static RateSessionWindow rsw;
public static CloseSessionWindow closesw;
public static CreateSessionWindow csw;
public static MainMenuWindow mmw;
}
In my MainMenuWindow class I have a button and when I click on the button it changes the content:
public partial class MainMenuWindow : UserControl
{
public MainMenuWindow()
{
InitializeComponent();
}
private void bttnCreateSession_Click(object sender, RoutedEventArgs e)
{
this.Content = AllSessionWindows.csw; //here
}
}
And here is where I get usually the stackoverflowexception:
public partial class CreateSessionWindow : UserControl
{
public CreateSessionWindow()
{
InitializeComponent();
}
private void bttnGoBack_Click(object sender, RoutedEventArgs e)
{
this.Content = AllSessionWindows.mmw; //here I always get the exception
}
}
So no matter in which order I call this.Content (for eg. first mmw and than csw or csw and than mmw) I always get a stackoverflow Exception when I call it 3 times which you can see above. What could be the problem be?
The problem in your code is this.Content=... in UserControls (in this case this.Content is UserControl content not Window content). If you want to change content in the main window you should add property with MainWindow to class AllSessionWindows:
public static class AllSessionWindows
{
public static MainWindow MainWindow;
public static RateSessionWindow rsw;
public static CloseSessionWindow closesw;
public static CreateSessionWindow csw;
public static MainMenuWindow mmw;
}
In the MainWidnow constructor you must assign this property:
public MainWindow()
{
InitializeComponent();
...
AllSessionWindows.MainWindow = this;
}
And in UserControl you should use following code:
private void bttnCreateSession_Click(object sender, RoutedEventArgs e)
{
AllSessionWindows.MainWindow.Content = AllSessionWindows.csw;
}
Presented solution to this kind of problem by you is not the best solution. For this kind of problem, you can use Caliburn.Micro framework.
In the following link you can find a good tutorial:
http://www.mindscapehq.com/blog/index.php/2012/1/12/caliburn-micro-part-1-getting-started/
Your problem is described in part 5 and 6 of this tutorial.
I have a main window and would like to pass a value to a Pop Up window (which will use that value to set up some other values dependant on that value). The pop up window has multiple textboxes for user inputs. When a button called SaveButton is pressed I would like the user inputs (all of them) to be sent back to the main window. How can I accomplish this?
I am very new to C# and I am aware that there are similar questions. However, I am having difficulties with adapting the answers to my specific situation. Thank you for keeping that in mind when answering!
Thank you so much!
Edit: About the code
The code that I have:
For initiating the PopUp window (which is a Window WPF called PopUp)
PopUp popUp = new PopUp();
popUp.ShowDialog
The PopUp.xaml.cs is empty except for the standard stuff and an eventhandler for the click event of the button.
Look at this :
Your main window :
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
//Defining public or internal parameters
public int MainWindowProp1 { get; set; }
public string MainWindowProp2 { get; set; }
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
//When you call your child window, pass this (mainwindow) as parameter.
childWindow cw = new WpfApplication1.childWindow(this);
cw.Param2 = "test";
cw.Param1 = 12;
cw.Closed += Cw_Closed;
cw.ShowDialog();
}
private void Cw_Closed(object sender, EventArgs e)
{
//On closed event, you can cast the sender as your child window.
var child = (sender as childWindow);
var param1 = child.Param1;
var param3 = child.Param2;
}
public void TestMethod()
{
//do anything you want
}
}
}
And your Child Window :
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for childWindow.xaml
/// </summary>
public partial class childWindow : Window
{
//declare Mainwindow as a parameter in your child window
public MainWindow mainWindow;
public int Param1 { get; set; }
public string Param2 { get; set; }
//Add a parameter in your child window contructor.
public childWindow(MainWindow _mainWindow)
{
InitializeComponent();
//Assign your global parameter mainWindow to the _mainWindow parameter.
this.mainWindow = _mainWindow;
this.Loaded += ChildWindow_Loaded;
}
private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
{
//You can get or set your main window properties.
this.mainWindow.MainWindowProp1 = 5;
this.mainWindow.MainWindowProp2 = "test";
//You can call methods ant events etc.. of your main window too (depending on acces modifiers).
this.mainWindow.TestMethod();
}
}
}
In general terms the most simplified solution is this one:
From MainWindow to Child window (Window1);
((Window1)Application.Current.Windows[1]).testlabel.Content = "In Sync";
From child window to MainWindow;
((MainWindow)Application.Current.MainWindow).label2.Content = "whatever";
I'm working on a project and need to access a label from a normal class.cs.
NOT from the MainWindow.xaml.cs!
MainWindow.xaml: contains a Label lblTag.
Class.cs needs to execute:
lblTag.Content = "Content";
How can I realize it?
I just end up with InvalidOperationExceptions.
Window1.xaml.cs:
public Window1()
{
InitializeComponent();
[...]
}
[...]
StreamElement se1;
StreamElement se2;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
[...]
se1 = new StreamElement(this);
se2 = new StreamElement(this);
[...]
}
[...]
StreamElement.cs:
[...]
private Window1 _window;
[...]
public StreamElement(Window1 window)
{
_window = window;
}
[...]
//metaSync is called, whenever the station (it's a sort of internet radio recorder)
//changes the meta data
public void metaSync(int handle, int channle, int data, IntPtr user)
{
[...]
//Tags just gets the meta data from the file stream
Tags t = new Tags(_url, _stream);
t.getTags();
//throws InvalidOperationException - Already used in another thread
//_window.lblTag.Content = "Content" + t.title;
}
[...]
You need a reference to an instance of MainWindow class in your Class:
public Class
{
private MainWindow window;
public Class(MainWindow mainWindow)
{
window = mainWindow;
}
public void MyMethod()
{
window.lblTag.Content = "Content";
}
}
You need to pass a reference to your window instance to the class. From inside your MainWindow window code behind you would call:
var c = new Class(this);
c.MyMethod();
EDIT:
You can only access controls from the same thread. If your class is running in another thread you need to use the Dispatcher:
public void metaSync(int handle, int channle, int data, IntPtr user)
{
[...]
//Tags just gets the meta data from the file stream
Tags t = new Tags(_url, _stream);
t.getTags();
//throws InvalidOperationException - Already used in another thread
//_window.lblTag.Content = "Content" + t.title;
_window.lblTag.Dispatcher.BeginInvoke((Action)(() =>
{
_window.lblTag.Content = "Content" + t.title;
}));
}
after the edit, this seems clearer now. Damir's answer should be correct.
Just add a mainwindow object on Class.cs and pass the mainwindow's instance to the class's constructor.
on mainwindow.xaml.cs
...
Class class = new Class(this);
...
on Class.cs
...
MainWindow mWindow = null;
// constructor
public Class(MainWindow window)
{
mWindow = window;
}
private void Initialize()
{
window.lblTag.Content = "whateverobject";
}