I'm using AppWindow to create multiple windows for the app, and I want the user to be able to make the windows full screen, but ApplicationView.TryEnterFullScreenMode doesn't work, it returns false all the time when used in an AppWindow.
Sample code from Microsoft Docs:
private void ToggleFullScreenModeButton_Click(object sender, RoutedEventArgs e)
{
var view = ApplicationView.GetForCurrentView();
if (view.IsFullScreenMode)
{
view.ExitFullScreenMode();
}
else
{
view.TryEnterFullScreenMode(); // Returns false in an AppWindow
}
}
How do you make an AppWindow enter full screen mode?
For AppWindow you should use AppWindowPresenter.RequestPresentation and pass AppWindowPresentationKind.FullScreen as parameter.
The solution I came up with (based on this answer) handles exiting full screen mode using:
The original button.
The back to window button in the title bar.
The escape key.
XAML:
<Button x:Name="ToggleFullScreenButton" Text="Full screen mode" Click="ToggleFullScreenButton_Click" />
Code behind:
public AppWindow AppWindow { get; } // This should be set to the AppWindow instance.
private void ToggleFullScreenButton_Click(object sender, RoutedEventArgs e)
{
var configuration = AppWindow.Presenter.GetConfiguration();
if (FullScreenButton.Text == "Exit full screen mode" && _tryExitFullScreen())
{
// Nothing, _tryExitFullScreen() worked.
}
else if (AppWindow.Presenter.RequestPresentation(AppWindowPresentationKind.FullScreen))
{
FullScreenButton.Text = "Exit full screen mode";
_ = Task.Run(async () =>
{
await Task.Delay(500); // Delay a bit as AppWindow.Changed gets fired many times on entering full screen mode.
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
AppWindow.Changed += AppWindow_Changed;
this.KeyDown += Page_KeyDown;
});
});
}
}
private bool _tryExitFullScreen()
{
if (AppWindow.Presenter.RequestPresentation(AppWindowPresentationKind.Default))
{
FullScreenButton.Text = "Full screen mode";
AppWindow.Changed -= AppWindow_Changed;
this.KeyDown -= Page_KeyDown;
return true;
}
return false;
}
// handles the back-to-window button in the title bar
private void AppWindow_Changed(AppWindow sender, AppWindowChangedEventArgs args)
{
if (args.DidSizeChange) // DidSizeChange seems good enough for this
{
_tryExitFullScreen();
}
}
// To make the escape key exit full screen mode.
private void Page_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
_tryExitFullScreen();
}
}
Related
I am using AxMSTSCLib to develop a Windows App for creating RDP connections.
The steps listed below caused my remote desktop display disappear:
Start an RDP connection with full-screen mode
Click "restore down" button from connection bar
Click "maximize" button again to re-enter full-screen mode
Click "minimize" button
Then my app disappears, I can't see it in taskbar (but still be listed / running in taskmanager)
If I skip steps 2 & 3, it won't disappear when I click "minimize" button from connection bar, it's really weird.
I post some part of my code here, hope anyone can help me to find out the problem.
public partial class RdpShowForm : Form
{
public AxMSTSCLib.AxMsRdpClient9NotSafeForScripting oRemote;
public RdpShowForm()
{
InitializeComponent();
oRemote = new AxMSTSCLib.AxMsRdpClient9NotSafeForScripting();
((System.ComponentModel.ISupportInitialize)(this.oRemote)).BeginInit();
oRemote.Dock = System.Windows.Forms.DockStyle.Fill;
oRemote.Enabled = true;
oRemote.Name = "WindowsVM";
this.Controls.Add(oRemote); // Controls contains 'panel1' and 'oRemote'
((System.ComponentModel.ISupportInitialize)(this.oRemote)).EndInit();
oRemote.CreateControl();
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
}
private void RdpShowForm_Load(object sender, EventArgs e)
{
NewRdpConnectionInstance();
}
private void NewRdpConnectionInstance()
{
oRemote.Server = 'xxx';
...
oRemote.FullScreen = true;
oRemote.AdvancedSettings7.DisplayConnectionBar = true;
oRemote.AdvancedSettings7.ConnectionBarShowMinimizeButton = true;
oRemote.AdvancedSettings7.ConnectionBarShowRestoreButton = true;
oRemote.OnConnected += rdpClient_OnConnected;
oRemote.OnLeaveFullScreenMode += rdpClient_OnLeaveFullScreenMode;
oRemote.Connect();
oRemote.Show();
oRemote.Focus();
}
private void rdpClient_OnConnected(object sender, EventArgs e)
{
this.panel1.Visible = false;
this.Visible = false;
}
private void rdpClient_OnLeaveFullScreenMode(object sender, EventArgs e)
{
this.Visible = true;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SYSCOMMAND)
{
if (m.WParam.ToInt32() == MAXIMIZE)
{
this.Visible = false;
oRemote.FullScreen = true;
oRemote.Show();
}
}
base.WndProc(ref m);
}
}
I've tried some methods, but none of them worked.
oRemote.Bounds = Screen.PrimaryScreen.WorkingArea;
oRemote.IsAccessible = true;
this.ShowInTaskbar = true;
or
this.Visible = ture; // do not hide the main form
or
if (m.WParam.ToInt32() == MAXIMIZE)
{
oRemote.FullScreen = true;
oRemote.Show();
oRemote.Update();
oRemote.Refresh();
}
So far, the only solution that came into my mind is to disable the restore button, like this:
oRemote.AdvancedSettings7.ConnectionBarShowRestoreButton = false;
This way isn't fix the problem, just avoid the issue to be triggered.
I would appreciate any help.
Since AxMSTSCLib has a restore down issue, I turn to use a form to handle its fullscreen mode. In this way, I can get extensive control over full-screen mode behavior, write code to tell the container what to do when the restore down button is called.
// enable container-handled full-screen mode
oRemote.AdvancedSettings7.ContainerHandledFullScreen = 1;
Then, customize OnRequestGoFullScreen, OnRequestLeaveFullScreen, OnRequestContainerMinimize.
oRemote.OnRequestGoFullScreen += corey_OnRequestGoFullScreen;
oRemote.OnRequestLeaveFullScreen += corey_OnRequestLeaveFullScreen;
oRemote.OnRequestContainerMinimize += corey_OnRequestContainerMinimize;
private void corey_OnRequestGoFullScreen(object sender, EventArgs e)
{
// set the form to fullscreen
}
private void corey_OnRequestLeaveFullScreen(object sender, EventArgs e)
{
// set the form back to non-fullscreen mode
}
private void corey_OnRequestContainerMinimize(object sender, EventArgs e)
{
// minimize the form
}
The method I posted here can be a workaround to this issue.
When I press on the hardware back button in my uwp app, the app closes. I use the hamburger interface from Template 10.
I added the following code in the app.xaml.cs and in het schell.xaml.cs but when I press back it says that the parameter canGoBack is false and closes the app.
public Shell(INavigationService navigationService)
{
Instance = this;
InitializeComponent();
// setup for static calls
Window = WindowWrapper.Current();
MyHamburgerMenu.NavigationService = navigationService;
// any nav change, reset to normal
navigationService.FrameFacade.Navigated += (s, e) =>
BusyModal.IsModal = LoginModal.IsModal = false;
SystemNavigationManager.GetForCurrentView().BackRequested += Shell_BackRequested;
}
private void Shell_BackRequested(object sender, BackRequestedEventArgs e)
{
MyHamburgerMenu.NavigationService.GoBack();
}
This is how you should handle the BackRequested event for a default implementation:
SystemNavigationManager.GetForCurrentView().BackRequested += (sender, e) =>
{
if (!e.Handled && Frame.CanGoBack)
{
e.Handled = true;
AppFrame.GoBack();
}
};
Remember that for CanGoBack to be true you should call first a Frame.Navigate()
If there are frames in the Frame.BackStack, then CanGoBack will be true.
I have a Popup that I want to always be open and its content active when a TextBox has keyboard focus. I have attempted this with this code
public partial class MyPopup : Popup
{
public MyPopup
{
InitializeComponent();
EventManager.RegisterClassHandler(
typeof(UIElement),
Keyboard.PreviewGotKeyboardFocusEvent,
(KeyboardFocusChangedEventHandler)OnPreviewGotKeyboardFocus);
}
private void OnPreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (sender is TextBox)
this.IsOpen = true;
else
this.IsOpen = false;
}
}
were I create the Popup in the constructor of App.
The problem with this code is that if the Popup is already open when ShowDialog is used the Popup is no longer active, even though it is still visually on top.
How do I get around this or get the required behavior in another way.
Found one solution where I check if the Window is opening by checking if it has loaded. If so I close the popup and reopen it again after the new Window has had its content rendered.
Not sure it's something I trust enough to use to use so a better solution would be welcome.
private void OnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
e.Handled = true;
if (sender is TextBox)
{
var _parentWindow = Window.GetWindow((UIElement)sender);
if (!_parentWindow.IsLoaded)
{
this.IsOpen = false;
_parentWindow.ContentRendered += (o, i) => this.IsOpen = true;
}
else
{
this.IsOpen = true;
}
}
else
{
this.IsOpen = false;
}
}
I want to handled the backpress button in windows phone 8.1 app.I want whenever backpress is pressed navigate to previous page but when backpress is pressed at the second page(after the mainpage) i want the app to exit or asked to exit.
i am using this code to navigate
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
public bool Handled { get; set; }
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
Frame frame = Window.Current.Content as Frame;
if (frame == null)
{
return;
}
if (frame.CanGoBack)
{
frame.GoBack();
e.Handled = true;
}
}
Try the following code. It works for me.
private async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
e.Handled = true;
var curpage = rootFrame.CurrentSourcePageType.FullName;
if(curpage=="your page name where you want to show dialog")
{
var msg = new MessageDialog("Sure to Exit?");
var okBtn = new UICommand("OK");
var cancelBtn = new UICommand("Cancel");
msg.Commands.Add(okBtn);
msg.Commands.Add(cancelBtn);
IUICommand result = await msg.ShowAsync();
if (result != null && result.Label == "OK")
{
Application.Current.Exit();
}
}
else
{
if (rootFrame.CanGoBack)
{
rootFrame.GoBack();
}
}
}
My understanding to your quesiton is everytime when user what to navigate back to the first page by pressing back button, you want to exit the app. If so, what we need to do is in the first page's OnNavigatedFrom event to check if the NavigationMode is Back. Try the following code:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
if(e.NavigationMode == NavigationMode.Back)
{
App.Current.Exit();
}
}
If you want to delete the history of the first navigated page, you can remove it from BackStack, and the second page is considered as first one. Just put this on your mainpage:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
Frame.BackStack.Remove(Frame.BackStack.First());
base.OnNavigatedFrom(e);
}
I make a program in C# windows form I have tons of function in my form including two datagrid view that connected to dabase and including a camera that direcly connected to my PC I use AForge dll reference to connect to the camera device I just found the tutorial on youtube and it works perfecly for me, as I stated earlier I have too many programs in one form including that camera and it went out that the camera was need to be resized to a small resolution, so I decided to make a popup button that must show the wider resolution when I click the button on my form.
this is the code for my camera.
//Camera
// get the devices name
private void getCamList()
{
try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
comboBox1.Items.Clear();
if (videoDevices.Count == 0)
throw new ApplicationException();
DeviceExist = true;
foreach (FilterInfo device in videoDevices)
{
comboBox1.Items.Add(device.Name);
}
comboBox1.SelectedIndex = 0; //make dafault to first cam
}
catch (ApplicationException)
{
DeviceExist = false;
comboBox1.Items.Add("No capture device on your system");
}
}
//refresh button
private void refresh_Click(object sender, EventArgs e)
{
getCamList();
}
//toggle start and stop button
private void start_Click(object sender, EventArgs e)
{
if (start.Text == "&Start")
{
if (DeviceExist)
{
videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
CloseVideoSource();
videoSource.DesiredFrameSize = new Size(160, 120);
//videoSource.DesiredFrameRate = 10;
videoSource.Start();
lblCam.Text = "Device running...";
start.Text = "&Stop";
}
else
{
lblCam.Text = "Error: No Device selected.";
}
}
else
{
if (videoSource.IsRunning)
{
CloseVideoSource();
lblCam.Text = "Device stopped.";
start.Text = "&Start";
}
}
}
//eventhandler if new frame is ready
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap img = (Bitmap)eventArgs.Frame.Clone();
//do processing here
pictureBox1.Image = img;
}
//close the device safely
private void CloseVideoSource()
{
if (!(videoSource == null))
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource = null;
}
}
//prevent sudden close while device is running
private void Form1_FormClosed(object sender, FormClosingEventArgs e)
{
CloseVideoSource();
}
} }
I also posted a picture so that you have further understanding what I am talking.
as you can see at the lower right corner I have a pop up button there honestly telling you I already tried different methods but nothing works unfotunately I cannot post what I've tried because I created it yesterday and can no longer undo the codes I tried. Any Idea?
Create a new Form
Place a PictureBox on this Form
Add all methods to initialize and get the current frame to the Form (should be refactored to an own class providing an event like FrameChanged giving you the current image)
add a method like to the Form
public void ShowCamPopup(string deviceName)
{
InitializeDevice(string deviceName, int width, int height);
this.Show();
this.BringToTop();
}
You should the really consider to refactor the communication with your cam to an own class which reduces duplicate code and allows you to do performance tweaks (which you will need for sure later) at a single position of your solution.