I'm trying to display an overlay during an auto login HTTP call.
I've found this code, which seems outdated somehow, but found nothing more recent.
Anyway, the Overlay is showing but not covering the whole screen as expected.
The calling code is this:
AppDelegate.FinishedLaunching
var avc = new AutoLoginViewController();
var navController = new UINavigationController(avc);
AutoLoginViewController.ViewDidLoad
var bounds = UIScreen.MainScreen.Bounds;
// show the loading overlay on the UI thread using the correct orientation sizing
loadPop = new LoadingOverlay(bounds, NSBundle.MainBundle.GetLocalizedString("connecting"));
View.Add(loadPop);
But the result is the following:
If I set a breakpoint in the LoadingOverlay constructor, I can see that the screen bounds (iPhone 6) are fine:
{{X=0,Y=0,Width=375,Height=667}}
public class LoadingOverlay : UIView
{
public LoadingOverlay(CGRect frame, string text) : base(frame)
{
// configurable bits
BackgroundColor = UIColor.Black;
Alpha = 0.75f;
AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
...
}
}
Clearly the UIView width is incorrect.
Because we're in 2020, maybe there is another way.
Any help appreciated.
EDIT: The app breaks on iPhone 8 iOS 13.3 simulator, So I can't say if this is tied to a particular screen size (1x in my case).
Cause :
It seems that you didn't set the LaunchImage , So whether on a simulator or real device , the value of bounds is a static value .
Solution:
The easiest way is set the size of overlay as bounds of View .
var bounds = View.Bounds;
Or you could set all size of LaunchImage of different screen .
Related
I've been trying to set the icon for my OpenTK window by using the ImageSharp library to load the image from my device and then converting the data to a byte array which I then set as the window icon using the WindowIcon method.
Although this did set the icon to something, it doesn't look anything like it should; it should be a comical picture of my cat. However, the result was three black horizontal lines on top of a grey and pink background.
...
If anyone could help me it would be greatly appreciated :)
(I'm using Visual Studio 2019 as my IDE with, of course, the language C#, and .NET Framework 5.0)
My code:
public static byte[] ImageToByteArray(string Icon)
{
var image = (Image<Rgba32>)SixLabors.ImageSharp.Image.Load(Configuration.Default, Icon);
image.Mutate(x => x.Flip(FlipMode.Vertical));
var pixels = new byte[4 * image.Width * image.Height];
image.CopyPixelDataTo(pixels);
return pixels;
}
public Game(int width = 1280, int height = 768, string title = "Window") :
base(
GameWindowSettings.Default,
new NativeWindowSettings()
{
Title = title,
Size = new Vector2i(width, height),
APIVersion = new Version(4, 6),
Icon = new WindowIcon(new OpenTK.Windowing.Common.Input.Image(100, 100, ImageToByteArray(#"C:\Users\xenon\Downloads\BobbilyIcon.png")))
})
{
this.CenterWindow();
}
Sadly, I can't directly include images since I am a new user, so I've attached links to a couple useful images concerning my problem below:
The picture of my cat which I am trying to set as the icon:
https://i.stack.imgur.com/uEMLk.jpg
The unexpected result:
https://i.stack.imgur.com/nvpdz.jpg
Okay, so apparently I'm a bit dumb. Turns out that when I am setting the icon, using the WindowIcon function, the height and width of the icon must match that of the image being used, which makes a lot of sense now that I think about it. I thought it was some incompatibility between the ImageSharp library used to load the image to memory and copy the data to the byte array and OpenTK.
In the below version of App.xaml.cs, written using Visual Studio 2019, associated with my Windows C# UWP solution / project called Example_Application, class App's constructor successfully resizes the blue app window that appears when starting the app. My question: Assuming a resolution scale of 1, just to make things easier, how do I change 1920 and 1080 to the two numbers comprising my Windows-10 display resolution?
namespace Example_Application
{
sealed partial class App : Windows.UI.Xaml.Application
{
public App()
{
Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize = new Windows.Foundation.Size(1920, 1080);
Windows.UI.ViewManagement.ApplicationView.PreferredLaunchWindowingMode = Windows.UI.ViewManagement.ApplicationViewWindowingMode.PreferredLaunchViewSize;
}
protected override void OnLaunched(Windows.ApplicationModel.Activation.LaunchActivatedEventArgs e)
{
Windows.UI.Xaml.Controls.Frame rootFrame = new Windows.UI.Xaml.Controls.Frame();
Windows.UI.Xaml.Window.Current.Content = rootFrame;
rootFrame.Navigate(typeof(MainPage), e.Arguments);
Windows.UI.Xaml.Window.Current.Activate();
}
}
}
Things I've tried:
Changing "PreferredLaunchViewSize" to "Maximized" does not maximize my blue window on my monitor. Changing "PreferredLaunchViewSize" to "FullScreen" does make my application take up the full screen, but this is not what I want because I want to be able to see my application title bar and my Windows-10 taskbar.
I can write Windows.Foundation.Rect visibleBounds = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds; only at the very end of OnLaunched, and bounds' Width and Height properties return present application width and height, not Windows-10 display resolution.
I can write uint screenWidthInRawPixels = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels; only at the very end of OnLaunched, and screenWidthInRawPixels is the present application width, not Windows-10 display width.
To get the screen resolution in UWP apps, you could try to use DisplayInformation.ScreenHeightInRawPixels Property and DisplayInformation.ScreenWidthInRawPixels Property.
Like the following code:
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
........
Window.Current.Activate();
}
//screen resolution
string heightsize = DisplayInformation.GetForCurrentView().ScreenHeightInRawPixels.ToString();
string widthsize = DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels.ToString();
Size mysize = new Size(Convert.ToDouble(widthsize), Convert.ToDouble(heightsize));
ApplicationView.PreferredLaunchViewSize = mysize;
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
}
My resolution is 1920*1080. In my test, it could get my screen resolution correctly as 1920*1080.
Ultimately, I choose to maximize my UWP App (written in C#) in my workspace, that area of my screen above my Windows taskbar. I would like to provide minimal instructions for creating a maximized app that you know is working.
I created a new default Blank App (Universal Windows) using C# in Visual Studio Community 2019, called "Draw Bounding Boxes". I included spaces here so that I could access "Draw Bounding Boxes" with spaces from my Start Menu.
I replaced the contents of "App.xaml.cs" with the following code block.
namespace Draw_Bounding_Boxes
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
sealed partial class App : Windows.UI.Xaml.Application
{
/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file.
/// </summary>
/// <param name="e">Details about the launch request and process.</param>
protected override void OnLaunched(Windows.ApplicationModel.Activation.LaunchActivatedEventArgs e)
{
// Resize app.
uint screenWidthInRawPixels = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels;
uint screenHeightInRawPixels = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenHeightInRawPixels;
double rawPixelsPerViewPixel = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
double screenWidthInViewPixels = System.Convert.ToDouble(screenWidthInRawPixels) / rawPixelsPerViewPixel;
double screenHeightInViewPixels = System.Convert.ToDouble(screenHeightInRawPixels) / rawPixelsPerViewPixel;
// If offsetToScreenWidthInViewPixels is less than 15,
// on first load app will be of default size, and on second load app will be full screen.
// A loaded image will have height equal to full screen height minus app title bar height minus app toolbar height minus 5 view pixels of padding.
// Part of a loaded image with aspect ratio less than one will be behind Windows taskbar.
// This is all very complicated and undesirable.
// If offsetToScreenHeightInViewPixels is less than 40,
// on first load app will be of default size, and on second load app will be full screen.
// A loaded image will have height equal to full screen height minus app title bar height minus app toolbar height minus 5 view pixels of padding.
// Part of a loaded image with aspect ratio less than one will be behind Windows taskbar.
// This is all very complicated and undesirable.
// If offsetToScreenWidthInViewPixels is greater than or equal to 15 and offsetToScreenHeightInViewPixels is greater than or equal to 40,
// on first load app will be of PreferredLaunchViewSize, and a loaded image with aspect ratio less than one will have height exactly equal to height of app minus app title bar height minus app toolbar height.
// If PreferredLaunchViewSize.Height is only screenHeightInViewPixels - offsetToScreenHeightInViewPixels,
// part of app and a loaded image with aspect ratio less than one will be behind taskbar.
// If taskbarHeight is taken off of screenHeightInViewPixels - offsetToScreenHeightInViewPixels,
// bottom of app and coincident bottom of loaded image will be slightly above taskbar.
// I consider this ideal.
double offsetToScreenWidthInViewPixels = 15;
double offsetToScreenHeightInViewPixels = 40;
double taskbarHeight = 40;
Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize = new Windows.Foundation.Size(screenWidthInViewPixels - offsetToScreenWidthInViewPixels, screenHeightInViewPixels - offsetToScreenHeightInViewPixels - taskbarHeight);
Windows.UI.ViewManagement.ApplicationView.PreferredLaunchWindowingMode = Windows.UI.ViewManagement.ApplicationViewWindowingMode.PreferredLaunchViewSize;
// Set the app window to a new Frame.
Windows.UI.Xaml.Controls.Frame rootFrame = new Windows.UI.Xaml.Controls.Frame();
Windows.UI.Xaml.Window.Current.Content = rootFrame;
// Navigate the frame to the initial default page.
rootFrame.Navigate(typeof(MainPage), e.Arguments);
// Attempts to activate the application window by bringing it to the foreground and setting the input focus to it.
Windows.UI.Xaml.Window.Current.Activate();
} // protected override void OnLaunched
} // sealed partial class App
} // namespace Draw_Bounding_Boxes
I added property x:Name="page" to the <Page> tag in "MainPage.xaml".
I removed the <Grid> </Grid> environment from MainPage.xaml.
I replaced the contents of "MainPage.xaml.cs" with the following code block.
// Create namespace Draw_Bounding_Boxes to contain all classes associated with our app.
namespace Draw_Bounding_Boxes
{
// Create class MainPage that inherits fields and methods from Windows.UI.Xaml.Controls.Page and
// is used to declare and define user-interface elements and functionality.
public sealed partial class MainPage : Windows.UI.Xaml.Controls.Page
{
// Create constructor public MainPage.
public MainPage()
{
// Necessary to instantiate this Page, add a stackPanel to this Page, et cetera.
this.InitializeComponent();
// Find width of app in view pixels and height between bottom of app and bottom of title bar in view pixels.
double widthOfAppInViewPixels = Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize.Width;
double heightBetweenBottomOfAppAndBottomOfTitleBarInViewPixels = Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize.Height;
// Create a stackPanel.
Windows.UI.Xaml.Controls.StackPanel stackPanel = new Windows.UI.Xaml.Controls.StackPanel();
// Create a toolbar with width equal to the width of the app, height equal to 50 view pixels, and background color of light blue that has one row and four columns.
Windows.UI.Xaml.Controls.Grid toolbar = new Windows.UI.Xaml.Controls.Grid();
toolbar.Width = widthOfAppInViewPixels;
toolbar.Height = 50;
toolbar.Background = new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.Colors.AliceBlue);
Windows.UI.Xaml.Controls.RowDefinition row = new Windows.UI.Xaml.Controls.RowDefinition();
toolbar.RowDefinitions.Add(row);
Windows.UI.Xaml.Controls.ColumnDefinition column = new Windows.UI.Xaml.Controls.ColumnDefinition();
column.Width = new Windows.UI.Xaml.GridLength(widthOfAppInViewPixels);
toolbar.ColumnDefinitions.Add(column);
stackPanel.Children.Add(toolbar);
page.Content = stackPanel;
}
}
}
Is there a way (either C# or XAML) I can maximize a UWP app window even after I resized and closed it previously on desktop?
I have tried with ApplicationViewWindowingMode.FullScreen but this makes the app go entire full screen and covers the Windows Taskbar too.
You can use another value PreferredLaunchViewSize from ApplicationViewWindowingMode and then set ApplicationView.PreferredLaunchViewSize but the key is to find out what the size is going to be.
Theoretically, you could use a really big number and window would just extend to the max it could be. However, it's probably safer to just calculate the screen dimensions in effective pixels.
So if you just call the following method before InitializeComponent(); on your main Page, it should maximize the window on startup.
private static void MaximizeWindowOnLoad()
{
// Get how big the window can be in epx.
var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
ApplicationView.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height);
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
}
Note the app somehow remembers these settings even after you uninstalled it. If you ever want to change back to the default behavior (app starts up with the previous window size), simply call ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Auto; once and remove all the code.
Update
Looks like in the latest Windows 10 build, ApplicationView.GetForCurrentView().VisibleBounds no longer returns the full window size in effective pixels anymore. So we now need a new way to calculate it.
Turns out it's quite straightforward since the DisplayInformation class also gives us the screen resolution as well as the scale factor.
The following is the updated code -
public MainPage()
{
MaximizeWindowOnLoad();
InitializeComponent();
void MaximizeWindowOnLoad()
{
var view = DisplayInformation.GetForCurrentView();
// Get the screen resolution (APIs available from 14393 onward).
var resolution = new Size(view.ScreenWidthInRawPixels, view.ScreenHeightInRawPixels);
// Calculate the screen size in effective pixels.
// Note the height of the Windows Taskbar is ignored here since the app will only be given the maxium available size.
var scale = view.ResolutionScale == ResolutionScale.Invalid ? 1 : view.RawPixelsPerViewPixel;
var bounds = new Size(resolution.Width / scale, resolution.Height / scale);
ApplicationView.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height);
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
}
}
If you want to MAXIMISE your app on launch you can use the following:
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Maximized;
But be sure to put it into the Loaded Event for your Page or it will not work!
I've too few points to comment directly. None of the above resized to a maximized view for me (or the below single-line ApplicationViewWindowingMode.Maximized method), but I have used some of the answers to come up with something that worked for me. It is still very clunky however. The screen size given in 'DisplayInformation' is too big to allow the page to be resized directly to it. Trying to do it didn't work and I had to take 60 off height and width to get it to return 'true', therefore I have the following bit of nonsense which worked, maybe it will help someone else find a better answer. It goes in the page/window loaded event. Nothing else needs to be added elsewhere.
private void Page_Loaded(object sender, RoutedEventArgs e)
{
var view = ApplicationView.GetForCurrentView();
var displayInfo = DisplayInformation.GetForCurrentView();
double x = ActualWidth;
double y = ActualHeight;
bool answer = true;
// Get the screen resolution (APIs available from 14393 onward).
var resolution = new Size(displayInfo.ScreenWidthInRawPixels-60, displayInfo.ScreenHeightInRawPixels-60);
answer = view.TryResizeView(resolution); //This should return true if the resize is successful
if (answer)
{
x = displayInfo.ScreenWidthInRawPixels - 60;
y = displayInfo.ScreenHeightInRawPixels - 60;
}
answer = true;
while (answer == true)
{
x++;
answer = view.TryResizeView(new Size { Width = x, Height = y });
}
x = x - 1;
answer = true;
while (answer == true)
{
y++;
answer = view.TryResizeView(new Size { Width = x, Height = y });
}
Adding the following line to the OnLaunched event under App.xaml.cs did it for me.
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.FullScreen;
NOTE: Make sure to add it before the following line
Window.Current.Activate();
If you like to go fullscreen at the runtime use the following line.
ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
I have this one liner that works as I expected Justins code to, but for some reason, when using Justins answer, my window would not be maximized... But then I changed something that did make it maximized but I lost all my fluent design such as Acrylic and RevealHighlite...
So I came up with this one liner which keeps all of my fluent design principles happy:
ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
Something to note:
I did try Justins answer, and I am using his method of MaximizeWindowOnLoad() which I have called straight after the initializeComponent();
Full overview:
public class()
{
this.InitializeComponent();
MaximizeWindowOnLoad();
}
private static void MaximizeWindowOnLoad()
{
ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
}
I am creating a camera app using MediaCapture. I am trying to adapt to screen rotation and thus I have created the following method:
private void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
{
width = Window.Current.Bounds.Width;
height = Window.Current.Bounds.Height;
//captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
//if (ApplicationView.GetForCurrentView().Orientation.ToString() == "Portrait") capturePreview.Stretch = Stretch.Fill;
capturePreview.Width = width;
capturePreview.Height = height;
imagePreview.Width = width;
imagePreview.Height = height;
//if (ApplicationView.GetForCurrentView().Orientation.ToString() == "Portrait") capturePreview.Stretch = Stretch.Fill;
//else capturePreview.Stretch = Stretch.Uniform;
}
When I open my app the camera fills the whole page but when I flip the camera the capturePreview only takes half of the page, It doesn't matter if I start the app in portrait or horizontal mode, the other mode won't work but the first mode will also if flipped in to.
Another question is about the actual capturePreview layout, I found that if I keep the screen at horizontal layout the camera works great but if the device is in portrait mode I cant fill the whole page without stretching the photo, is there a way to keep only one element on the screen in a certain rotation (capturePreview) I tried rotating it with a rotation tranform but that also affects the actual place of the element.
Thank you
In WP you get the CurrentViewState through the Window.Sizechanged Event.
See this MSDN article (first result from google)
I am trying to capture the screen of the PC connected to my PC via HDMI using Direct-show. I am using capture card as the hardware and Direct show's sample grabber method to render those captured frames.
The issue is I am not able to render the full screen of the secondary monitor on to my computer. Both PC's are of different configuration. I have tried giving different frame size values like 1366*768 but it just picks up 1280*768 I believe .
Moreover I have set the frame size to be captured as 1366*768 and even my PC is set to the same display setting still it does not render the full screen.
Here is the code I am using for capturing and rendering. Capture class has a property called frame size whose size has been set to 1366*768 but as soon as I do that and run the code it shows a total blank screen and when i change the setting to 1280*768 it will render the secondary monitor but won't render the full screen of it.
Size size = new Size(1366, 768);
capture.FrameSize = size;
where capture class has the below given property
public Size FrameSize
{
get
{
BitmapInfoHeader bmiHeader;
bmiHeader = (BitmapInfoHeader) getStreamConfigSetting( videoStreamConfig, "BmiHeader" );
// Size size = new Size( bmiHeader.Width, bmiHeader.Height );
Size size = new Size(1280, 768);
return( size );
}
set
{
BitmapInfoHeader bmiHeader;
bmiHeader = (BitmapInfoHeader) getStreamConfigSetting( videoStreamConfig, "BmiHeader" );
bmiHeader.Width = 1280;
bmiHeader.Height = 768;
setStreamConfigSetting( videoStreamConfig, "BmiHeader", bmiHeader );
//#if NEWCODE
this.videoCaps = null;
//#endif
}
}
Any suggestions or findings that how can I capture the full screen of the secondary monitor will be really appreciated.