UWP show Fullscreen Popup, ContentDialog or Flyout - c#

I need to display a full screen dialog (in application window boundaries) in my UWP application, but can't seems to make it work. I tried with :
ContentDialog only shows vertically stretched with FullSizeDesired="True"
Popup, even trying to set the width and height in code behind its not working
Flyout Placement="Full" only stretch it vertically just like the contentdialog
Can't believe I spend so much time on that thing :(
Thanks

Have you tried something like this:
var c = Window.Current.Bounds;
var g = new Grid
{
Width = c.Width,
Height = c.Height,
Background = new SolidColorBrush(Color.FromArgb(0x20, 0, 0, 0)),
Children =
{
new Rectangle
{
Width = 100,
Height = 100,
Fill = new SolidColorBrush(Colors.White),
Stroke = new SolidColorBrush(Colors.Black),
StrokeThickness = 3
}
}
};
var p = new Popup
{
HorizontalOffset = 0,
VerticalOffset = 0,
Width = c.Width,
Height = c.Height,
Child = g
};
p.IsOpen = true; // open when ready
You should see a semi-transparent overlay with a white rectangle in the middle of your screen.

To make a popup Fullscreen I did the following. Hope this helps.
I have a user control I wanted to show as a popup. Its name "URLUserControl"
Step 1: UI of the UC (Just the first bit. Not complete code)
<UserControl>
<Grid>
<Popup x:Name="MPopup" VerticalAlignment="Center">
<Grid x:Name="MainGrid">
Step 2: UC constructor adds the following
private void InitURLUserControl()
{
MPopup.IsOpen = true;
Window.Current.SizeChanged += Current_SizeChanged;
MainGrid.Width = Window.Current.Bounds.Width;
MainGrid.Height = Window.Current.Bounds.Height;
}
Step 3:
private void Current_SizeChanged(object sender, WindowSizeChangedEventArgs e)
{
MainGrid.Width = Window.Current.Bounds.Width;
MainGrid.Height = Window.Current.Bounds.Height;
}
The above code will help the popup to be the center of the screen and display in fullscreen in size. Check it out. Happy coding!

Related

Styling Windows Form Tab

I am creating windows Tabbed Application. Everything is good but the tabs are quiet faded and borders are very dull. I have tried changing the border style to 3D as well but no effect. Below is the screenshot
There are forums where people have suggested to use third party library to make Google Chrome type tabs. But I want the native way to get beautiful tabs.
You can take control of how the tabs are drawn by setting the DrawMode = TabDrawMode.OwnerDrawFixed. The example below assumes you have a TabControl named tabControl1 on the form, this will add a new tab with a blue box.
private Rectangle myTabRect;
private Rectangle myInsideRect;
private Rectangle myOutsideRect;
public Form1()
{
InitializeComponent();
TabPage tabPage1 = new TabPage();
// Sets the tabs to be drawn by the parent window Form1.
// OwnerDrawFixed allows access to DrawItem.
tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
tabControl1.Controls.Add(tabPage1);
tabControl1.Location = new Point(25, 25);
tabControl1.Size = new Size(250, 250);
tabPage1.TabIndex = 0;
myTabRect = tabControl1.GetTabRect(0);
myInsideRect = new Rectangle(tabControl1.DisplayRectangle.X -1, tabControl1.DisplayRectangle.Y -1, tabControl1.DisplayRectangle.Width + 1, tabControl1.DisplayRectangle.Height + 1);
myOutsideRect = tabControl1.ClientRectangle;
myOutsideRect.Width--;
myOutsideRect.Height--;
ClientSize = new Size(300, 500);
Controls.Add(tabControl1);
tabControl1.DrawItem += new DrawItemEventHandler(OnDrawItem);
}
private void OnDrawItem(object sender, DrawItemEventArgs e)
{
// Draw your tab graphics here
Graphics g = e.Graphics;
Pen p = new Pen(Color.Blue);
g.DrawRectangle(p, myTabRect);
p = new Pen(Color.Red);
g.DrawRectangle(p, myInsideRect);
p = new Pen(Color.Green);
g.DrawRectangle(p, myOutsideRect);
}
You can draw whatever style you like into the graphic context, add text, images, etc

How to popup Image using codes UWP

I have an app load pictures from the internet I want to add a popup event to the image so the user can view the image by click on it !
I've try a lot of things to do that all that I can found is to resize the image or just view a content dialog with a text on it !
Here's my code :
private static void view_tapped(object sender, TappedRoutedEventArgs e)
{
Viewbox image = sender as Viewbox;
Panel parent = image.Parent as Panel;
if (parent != null)
{
image.RenderTransform = new ScaleTransform() { ScaleX = 0.5, ScaleY = 0.5 };
parent.Children.Remove(image);
parent.HorizontalAlignment = HorizontalAlignment.Stretch;
parent.VerticalAlignment = VerticalAlignment.Stretch;
parent.Children.Add(new Popup() { Child = image, IsOpen = true, Tag = parent });
}
else
{
Popup popup = image.Parent as Popup;
popup.Child = null;
Panel panel = popup.Tag as Panel;
image.RenderTransform = null;
panel.Children.Add(image);
}
}
I want it to look like that I want to keep my old image in the same place and create a new grid that have the same image like this image you can view it here
I can't use the xaml file because my image load from the web by code using a dll file !
The reason why the image doesn't display is because you don't add an Image control into the ViewBox to display the image content, you can try the following code to display an image in the popup.
private void Viewbox_Tapped(object sender, TappedRoutedEventArgs e)
{
Viewbox image = sender as Viewbox;
Image imageControl = new Image() { Width = 500, Height = 500 };
Uri uri = new Uri("Your image URL");
BitmapImage source = new BitmapImage(uri);
imageControl.Source = source;
image.Child = imageControl;
Panel parent = image.Parent as Panel;
if (parent != null)
{
image.RenderTransform = new ScaleTransform() { ScaleX = 0.5, ScaleY = 0.5 };
parent.Children.Remove(image);
parent.HorizontalAlignment = HorizontalAlignment.Stretch;
parent.VerticalAlignment = VerticalAlignment.Stretch;
parent.Children.Add(new Popup() { Child = image, IsOpen = true, Tag=parent});
}
else
{
Popup popup = image.Parent as Popup;
popup.Child = null;
Panel panel = popup.Tag as Panel;
image.RenderTransform = null;
panel.Children.Add(image);
}
}
By the way, please notice that the Popup is a control that displays content on top of existing content, within the bounds of the application window, and pay attention to the Remarks part,
Do not use a Popup if a Flyout, MenuFlyout, ToolTip or ContentDialog (MessageDialog for a Windows 8 app) is more appropriate.

Set focus on a Canvas created programatically, in Windows Phone 8.1

I have an image of a map inside a scrollviewer and, using 2 canvas, I place a little square (or a map pin image) after the user selects where they are.
Now, since they do not "click" on the map to do so, but they select their location from a list, I need to make that square the center of the view, in the scrollviewer, once they see the map.
In my xaml, I have just a few things since, all the information, comes from an API
<Grid x:Name="firstFloor" Margin="0,50,0,0" Visibility="Visible">
<ScrollViewer x:Name="pruebaScrollViewer1" BringIntoViewOnFocusChange="True" ZoomMode="Enabled" HorizontalScrollBarVisibility="Auto" HorizontalScrollMode="Enabled">
<Grid x:Name="myMap1stFloor">
</Grid>
</ScrollViewer>
</Grid>
and in code behind I have
private void showStorePosition2(string map_id, string name, int width, int height, int tile, int x, int y)
{
IdOfMapWhereIAm = map_id;
if (map_id == "10")
{
if (!_primerPiso)
{
//my parent canvas is defined globally
//the width and height of it come from API
myParentCanvas.Width = width;
myParentCanvas.Height = height;
myParentCanvas.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;
myParentCanvas.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center;
Debug.WriteLine(width);
ImageBrush pushpin = new ImageBrush();
pushpin.ImageSource = new BitmapImage(new Uri("ms-appx:/Imagenes/mappin.png", UriKind.Absolute));
TextBlock storeName = new TextBlock();
storeName.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
storeName.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
storeName.TextAlignment = TextAlignment.Center;
storeName.FontFamily = new Windows.UI.Xaml.Media.FontFamily("Open Sans");
storeName.Height = 20;
storeName.Width = Double.NaN;
storeName.FontSize = 15;
storeName.Text = name;
storeName.Margin = new Thickness(-20, -20, 0, 0);
storeName.TextWrapping = TextWrapping.Wrap;
storeName.Foreground = new SolidColorBrush(Colors.Black);
Canvas myInititialStore = new Canvas();
myInititialStore.Background = pushpin;
myInititialStore.Height = 13;
myInititialStore.Width = 13;
Canvas.SetTop(myInititialStore, (y) * tile);
Canvas.SetLeft(myInititialStore, (x) * tile);
myInititialStore.Children.Add(storeName);
myParentCanvas.Children.Add(myInititialStore);
myMap1stFloor.Children.Add(myParentCanvas);
}
}
}
I was thinking about giving myInitialStore a dependency property, setting the focusProperty, but I do not know how to do it either.
In case someone has the same problem I had, I found a way to "focus" on the Canvas I needed to:
Instead of trying to do so, I changed the view in the scrollviewer, using the Canvas values, that way, when the Canvas is loaded inside the scrollviewer, this last one moves automatically to where the Canvas is located:
ScrollViewer1.ChangeView(((initialStore_x * tileMapToGo) - (widthMapToGo / tileMapToGo)), ((initialStore_y * tileMapToGo) - (heightMapToGo / tileMapToGo)), 1, false);
Hope it helps someone. Regards

How to add a Button click event with C#

I have some problems adding a click event to my project. The main logic is correct but I need some help with the click event.
The project puts some buttons on a Canvas and I would like to add a click event on them.
I'm using the Kinect v2 and every time is found a Body, and every time is found a SpineMid joint, the project draws some buttons around the body using the shape of a regular polygon.
In my case the problem is that I cannot click the buttons because they're cleared in every frame.
I call the function InitializeButtons every time I found that joint (and so, I created new buttons with every frame and then cleared them) so it's basically impossible to click them, there's no time to view the content of the button changing: they're immediately replaced!
Here's the code where I call the function:
foreach (Body body in bodies){
foreach (var item in body.Joints){
JointType jointType = item.Key;
Joint joint = item.Value;
if (joint.JointType == JointType.SpineMid) {
InitializeButtons(100, 200, center, verticies, 300)
}
}
}
And here's the code of the InizializeButtons function:
private void InitializeButtons(int height, int width, Point center, Point[] verticies, int radius)
{
Button test_button1 = new Button() { Background = Brushes.Aquamarine, Height = height, Width = width, Content = "I'm test button1!" };
Button test_button2 = new Button() { Background = Brushes.Aqua, Height = height, Width = width, Content = "I'm test button2!" };
Button test_button3 = new Button() { Background = Brushes.Beige, Height = height, Width = width, Content = "I'm test button3!" };
Button test_button4 = new Button() { Background = Brushes.BurlyWood, Height = height, Width = width, Content = "I'm test button4!" };
Button test_button5 = new Button() { Background = Brushes.Bisque, Height = height, Width = width, Content = "I'm test button5!" };
test_button1.Click += test_button1_Click;
canvas.Children.Add(test_button1);
canvas.Children.Add(test_button2);
canvas.Children.Add(test_button3);
canvas.Children.Add(test_button4);
canvas.Children.Add(test_button5);
Canvas.SetLeft(test_button1, 0);
Canvas.SetTop(test_button1, 0);
Canvas.SetLeft(test_button2, 0);
Canvas.SetTop(test_button2, 0);
Canvas.SetLeft(test_button3, 0);
Canvas.SetTop(test_button3, 0);
Canvas.SetLeft(test_button4, 0);
Canvas.SetTop(test_button4, 0);
Canvas.SetLeft(test_button5, 0);
Canvas.SetTop(test_button5, 0);
buttons = new List<Button> { test_button1, test_button2, test_button3, test_button4, test_button5 };
verticies = Polygon.CalculateVertices(buttons.Count, radius, 18, center);
int i = 0;
foreach (Button button in buttons)
{
Canvas.SetLeft(button, verticies[i].X);
Canvas.SetTop(button, verticies[i].Y);
i++;
}
verticies = null;
}
void test_button1_Click(object sender, RoutedEventArgs e)
{
Button clickedButton = (Button)sender;
clickedButton.Content = "let's try it";
}
Is there any way I can go beyond that?
Any help is really appreciate.
Thanks

Transparent canvas, with opaque elements

I'm trying to simulate an Android UI element that unfortunately doesn't exist in Windows 7 phone: ListPreference
I thought about using a Popup, that would take exactly the whole screen (to simulate a modal window).
So the popup would be made of the following elements:
Popup -> Canvas -> Border -> StackPanel -> RadioButtons
The Canvas would be fully transparent (or lightly whitish to clearly show that the element underneath aren't available)
The border would be made so it only big enough to contain all the RadioButtons
Then the StackPanel would be opaque and black.
Unfortunately, if I make the bottom canvas transparent, all children elements are also transparent. I can only make the elements more transparent.
The way transparency works is slightly different than with Android or iPhone (where it's quite easy to have a parent fully transparent, but opaque children).
Is there a way to make a parent fully transparent with the children opaque?
Or maybe someone could suggest another way to simulate a modal window.
Who knows, maybe someone even developed a ListPreference-like UIElement :)
Thank you
Here is how I ended up doing it.
It works in a similar fashion as ListPreference on Android. The constructor takes a string, an array of string and an int indicating which is the default value
When the windows is closed, the delegate Dismissed is called..
So you call it like so:
string[] choices = { "Choice 1", "Choice 2", "Choice3" };
ListPreference lp = new ListPreference("name", choices, 1);
lp.dismissed += new ListPreferences.DismissedHandler(lp_Dismissed);
the code:
public class ListPreference
{
Popup p;
string Name;
int oldValue;
public delegate void DismissedHandler(string name, bool changed, int newvalue);
public event DismissedHandler Dismissed;
public bool IsOpen
{
get
{
return p.IsOpen;
}
set
{
p.IsOpen = value;
}
}
public ListPreference(string name, Array elements, int default_value)
{
p = new Popup();
Name = name;
Dismissed = null;
oldValue = default_value;
double height = (App.Current.RootVisual as FrameworkElement).ActualHeight;
double width = (App.Current.RootVisual as FrameworkElement).ActualWidth;
p.VerticalOffset = SystemTray.IsVisible ? 32.0 : 0.0;
p.Height = height;
p.Width = width;
Canvas canvas = new Canvas();
SolidColorBrush colorBrush = new SolidColorBrush(Colors.Black);
colorBrush.Opacity = 0.75;
//Color.FromArgb(0xff, 0x8a, 0x8a, 0x8a));
canvas.Background = colorBrush;
//canvas.Opacity = 0.765;
canvas.Height = height;
canvas.Width = width;
p.Child = canvas;
Border border = new Border();
border.Width = width - 50.0 * 2.0;
border.BorderBrush = new SolidColorBrush(Colors.LightGray);
border.BorderThickness = new Thickness(5.0);
border.Background = new SolidColorBrush(Colors.Black);
canvas.Children.Add(border);
StackPanel panel2 = new StackPanel();
panel2.Orientation = System.Windows.Controls.Orientation.Vertical;
int i = 0;
foreach (string val in elements)
{
RadioButton radio1 = new RadioButton();
radio1.GroupName = "group1";
radio1.Content = val;
if (i == default_value)
radio1.IsChecked = true;
int j = i;
radio1.Click += (sender, args) => radio1_Checked(radio1, j);
i++;
panel2.Children.Add(radio1);
}
Button button1 = new Button();
button1.Background = new SolidColorBrush(Colors.Black);
button1.Foreground = new SolidColorBrush(Colors.White);
button1.Opacity = 1.0;
button1.Content = "Cancel";
button1.Margin = new Thickness(5.0);
button1.Click += new RoutedEventHandler(closeButton_Click);
panel2.Children.Add(button1);
border.Child = panel2;
// Open the popup.
p.IsOpen = true;
p.UpdateLayout();
border.Height = panel2.DesiredSize.Height + 5.0 * 2.0;
border.SetValue(Canvas.TopProperty, (height - border.Height) / 2.0);
border.SetValue(Canvas.LeftProperty, (width - border.Width) / 2.0);
p.UpdateLayout();
}
void closeButton_Click(object sender, RoutedEventArgs e)
{
// Close the popup.
p.IsOpen = false;
if (Dismissed != null)
{
Dismissed(Name, false, -1);
}
}
void radio1_Checked(object sender, int idx)
{
p.IsOpen = false;
if (Dismissed != null)
{
Dismissed(Name, idx != oldValue, idx);
}
}
}
I would suggest creating a Usercontrol that would do what you need. Set the LayoutRoot grid's background to PhoneSemitransparentBrush or changing the opacity will change the child element's opacity as well. Then your child elements can have any opacity you'd like. You can add this control as a child to the popup. Additionally, you can add doubleanimation to the popup with the opened and closed event triggers. Change the design height of the UserControl to 480x760 to simulate full page.
To answer your question. Using resources like PhoneSemitransparentBrush and TransparentBrush for the Canvas background is one of your options. Opacity will change the opacity of the whole UIElement including its children.

Categories