When I try to zoom in (Button_Click_1 event) I'm getting an error as I try to add images/layers back to a map.
I should note: I've simplified the code substantially so that it would be easier to pick out the error (that I can't seem to figure out).
Each zoom level has a different set of images which are attached to it in case you're wondering why I need to continually clear the layers/images.
Each of the Images/Layers/Overlays is defined globally (so that I can use them in several methods
Image img1 = new Image();
Image img2 = new Image();
MapLayer lyr1 = new MapLayer();
MapLayer lyr2 = new MapLayer();
MapOverlay ovrly1 = new MapOverlay();
MapOverlay ovrly2 = new MapOverlay();
Each of these is initialized when the page loads in a separate method:
private void initializeImages()
{
ovrly1.GeoCoordinate = new GeoCoordinate(49.33783000, -0.45215600);
img1.Source = new BitmapImage(new Uri("images/push-pin.png", UriKind.Relative));
ovrly1.Content = img1;
ovrly1.PositionOrigin = new Point(0.0, 0.0);
img1.Opacity = 0.8;
img1.Height = 30;
img1.Width = 30;
img1.Tap += img1_Tap;
ovrly2.GeoCoordinate = new GeoCoordinate(49.35783000, -0.45425600);
img2.Source = new BitmapImage(new Uri("images/push-pin.png", UriKind.Relative));
ovrly2.Content = img2;
ovrly2.PositionOrigin = new Point(0.0, 0.0);
img2.Opacity = 0.8;
img2.Height = 30;
img2.Width = 30;
img2.Tap += img2_Tap;
}
When I try to zoom on the Button_Click the first time it works fine. But any other time I try to zoom I get an error:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
map1.ZoomLevel += 1;
map1.Layers.Clear();
lyr1.Add(ovrly1); // ERROR OCCURS HERE
map1.Layers.Add(lyr1);
lyr2.Add(ovrly2);
map1.Layers.Add(lyr2);
}
This error disappears when I declare all of the images/overlays/layers 'locally' inside the Button_Click event. But I can't do that, otherwise I won't be able to access the Images outside of the method.
Any help would be greatly appreciated.
I think the problem is that you are adding many overlays on exactly same position.
Additionaly, in the Button Click event you clear the Layers element of the map, but you don't do the same with each layer, adding new elements to to the layer which already have one, each time. Since you add the elements in the same position, you get the error.
Following code should work if both elements have different GeoCoordinate values:
private void Botoia_Click(object sender, RoutedEventArgs e)
{
map1.ZoomLevel += 1;
map1.Layers.Clear();
lyr1.Clear();
lyr2.Clear();
lyr1.Add(ovrly1);
map1.Layers.Add(lyr1);
lyr2.Add(ovrly2);
map1.Layers.Add(lyr2);
}
Related
I'm trying to create an array/list of picturebox objects that are declared and added to the form on button click (Meaning that I'm not creating multiple objects with my array, but plan to if I can get this to run). Not getting errors, but the pictureboxes themselves do not appear on the form.
private void spawn_Click(object sender, EventArgs e)
{
var pictureTest[0] = new PictureBox();
pictureTest[0].Image = Properties.Resources.testimage;
pictureTest[0].Location = new Point(500, 250);
pictureTest[0].Name = "spawn1";
pictureTest[0].Size = new Size(50, 50);
pictureTest[0].TabIndex = 98;
pictureTest[0].TabStop = false;
this.Controls.Add(pictureTest[0]);
}
Through the course of my research, I've mainly just gotten the advice to use this.Controls.Add, but that doesn't seem to be my issue here. My array is declared earlier with:
PictureBox[] pictureTest = new pictureTest[100];
As #HansPassant says in the comments, this code doesn't compile. The following should broadly do what you want:
int _position = 10;
private void spawn_Click(object sender, EventArgs e)
{
var pictureTest = new PictureBox();
pictureTest.Image = Properties.Resources.testimage;
pictureTest.Location = new Point(_position, 250);
pictureTest.Name = "spawn1";
pictureTest.Size = new Size(50, 50);
pictureTest.TabIndex = 98;
pictureTest.TabStop = false;
this.Controls.Add(pictureTest);
_position += 100;
}
Firstly, there's no need to maintain a separate array of PictureBox, as they are part of your form. Secondly, as #HansPassant said - you were overlaying the images directly on top of each other, so you couldn't tell if you had 1 or 1000.
I want to add a pin on a certain location on a map for a windows phone 8.0 app.
My code so far is the following:
private async void Button_Click(object sender, RoutedEventArgs e)
{
BasicGeoposition bGeo = new BasicGeoposition();
bGeo.Latitude = 37.4333;
bGeo.Longitude = 24.9167;
Geopoint geoPoint = new Geopoint(bGeo,0);
myMap.ZoomLevel = 13;
myMap.Center = geoPoint;
}
private void AddMapIcon()
{
MapIcon MapIcon1 = new MapIcon();
MapIcon1.Location = new Geopoint(new BasicGeoposition()
{
Latitude = 37.4333,
Longitude = 24.9167
});
MapIcon1.NormalizedAnchorPoint = new Point(2.0, 2.0);
myMap.MapElements.Add(MapIcon1);
}
The map is loading properly, but the pin won't appear. Any ideas on this? Is there any way to do it without using xaml controls for the pin?
This is a general way of adding any UI on map control in windows phone:
We need to create "map layers" and "map overlays" and specify the coordinates where we want to place it. Sample code:
Read the tutorial here
You can add an Image control in the overlay and point its source to the pin image you want to plot. Hope this help
You can try..
BitmapImage myImage1;
myImage1 = new BitmapImage(new Uri("/Assets/Images/pushpin-google-hi.png", UriKind.RelativeOrAbsolute));
var image = new Image();
image.Width = 50;
image.Height = 50;
image.Opacity = 100;
image.Source = myImage1;
var mapOverlay = new MapOverlay();
mapOverlay.Content = image;
mapOverlay.GeoCoordinate = new GeoCoordinate(lats, lons);
var mapLayer = new MapLayer();
mapLayer.Add(mapOverlay);
MyMap.Layers.Add(mapLayer);
MyMap.SetView(new GeoCoordinate(lats, lons), 16);
I've been experimenting with adding elements to Windows Forms dynamically via code.
I need to create a PictureBox element. So, far, I have the following code:
private void Form1_Load(object sender, EventArgs e)
{
//stylise form
this.BackColor = System.Drawing.Color.Black;
PictureBox bgui = new PictureBox();
bgui.Image = Properties.Resources.attack_box;
bgui.Name = "bgui";
bgui.Location = new Point(0, 600);
this.Controls.Add(bgui);
bgui.Visible = true;
}
However, when this code is run, I get nothing but the black background which I set earlier. I've looked at many questions similar to mine; and they all say I need to add it to the control, which I have done, yet it still abstains from showing.
I would really appreciate it if you could give me an insight into my wrong-doing.
Thanks, Computo.
You need to set Width and Height Properties of the PictureBox.
Try This:
bgui.Width = 500;
bgui.Height = 500;
Complete Code:
private void Form1_Load(object sender, EventArgs e)
{
//stylise form
this.BackColor = System.Drawing.Color.Black;
PictureBox bgui = new PictureBox();
bgui.Image = Properties.Resources.attack_box;
bgui.Name = "bgui";
bgui.Width = 500;
bgui.Height = 500;
bgui.Location = new Point(0, 600);
this.Controls.Add(bgui);
bgui.Visible = true;
}
Turns out that System.Drawing.Point does not translate to the actual pixels on the screen. I will have to investigate how Point translates into pixels.
Here its works perfect. Specify the SizeMode and change the location.
private void Form1_Load(object sender, EventArgs e)
{
//stylise form
this.BackColor = System.Drawing.Color.Black;
PictureBox bgui = new PictureBox();
bgui.Image = Properties.Resources.attack_box;
bgui.Location = new System.Drawing.Point(100, 0);
bgui.Name = "pictureBox1";
bgui.SizeMode = PictureBoxSizeMode.AutoSize;
this.Controls.Add(bgui);
}
I'm trying to add Image stored in ..\Resources\ebi.png in a stackpanel. Most of the times, the same image will be displayed in Stackpanel, depending on Textbox input "EtReqCount". Below is the sample code tried but getting error saying
"Specified Visual is already a child of another Visual or the root of a CompositionTarget"
Below is the code tried:
private BitmapImage bmp = new BitmapImage(new Uri("WpfApplication1;component/Resources/ebi.png", UriKind.RelativeOrAbsolute));
private void EtReqCount_TextChanged(object sender, TextChangedEventArgs e)
{
StackPanel dynamicStackPanel = new StackPanel();
dynamicStackPanel.Width = 300;
dynamicStackPanel.Height = 200;
dynamicStackPanel.Background = new SolidColorBrush(Colors.LightBlue);
dynamicStackPanel.Orientation = Orientation.Vertical;
if (EtReqCount.Text != "")
{
for (int k = 1; k <= Int32.Parse(EtReqCount.Text); k++)
{
Image img = new System.Windows.Controls.Image(); // This makes the difference.
img.Source = bmp;
dynamicStackPanel.Children.Add(img);
}
}
}
XAML Code:
It is apparent. The same image is already a child of the StackPanel, you cannot add it again and again.
Moreover, you should use a private memeber to hold the repetitively used bmp to save resource:
outside the class method, and inside class.cs:
private BitmapImage bmp=new BitmapImage(new Uri("/....../ebi.png", UriKind.RelativeOrAbsolute);
You can create new instances of the image:
for(int i=0; i<...;i++)
{
img = new System.Windows.Controls.Image(); // This makes the difference.
img.Source = bmp;
dynamicStackPanel.Children.Add(img);
}
I'm trying to set an image background in code-behind. I tried adding a background image to the button, but as soon as I hover over the button, the image disappears. To solve this, I have to write functions to override the button behavior, which is too much to do in code-behind.
I then use an alternative method, that is to add a button and an image separately to a grid cell. The issue is -- when I click on the image, the button won't trigger.
How do I make the button to have the hover and pressed effect, even when, the mouse is either hovering or presses the image on the button, but not the remaining area of the button?
Or hope someone can suggest me a better solution.Below is my code.
InitializeComponent();
Button playBtn = new Button();
playBtn.Width = 60;
playBtn.Height = 30;
Image playIcon = new Image();
playIcon.Source = new BitmapImage(new Uri(#"PATH"));
playIcon.Stretch = Stretch.Uniform;
playIcon.Height = 25;
grid1.Children.Add(playBtn);
grid1.Children.Add(playIcon);
Grid.SetColumn(playBtn, 0);
Grid.SetRow(playBtn, 0);
Grid.SetColumn(playIcon, 0);
Grid.SetColumn(playIcon, 0);
thanks for everyone input, after digging more into it, it sort of work out. What I did is add a Grid to Button.Content then add the image to the Grid. And using Opacity to add the grey out effect for IsEnable false state. Below I post my code, hope someone find it useful or improve on:
Button playBtn = new Button();
Image playIcon = new Image();
public MainWindow()
{
InitializeComponent();
Grid grid2 = new Grid();
RowDefinition grid2_row1 = new RowDefinition();
ColumnDefinition grid2_col1 = new ColumnDefinition();
grid2.RowDefinitions.Add(grid2_row1);
grid2.ColumnDefinitions.Add(grid2_col1);
playBtn.Width = 60;
playBtn.Height = 30;
playBtn.Click += playBtn_Click;
playIcon.Source = new BitmapImage(new Uri(#"pack://PATH..."));
playIcon.Stretch = Stretch.Uniform;
playIcon.Height = 25;
playBtn.Content = grid2;
grid2.Children.Add(playIcon);
grid1.Children.Add(playBtn);
Grid.SetRow(playIcon, 0);
Grid.SetColumn(playIcon, 0);
}
public void playBtn_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello");
}
private void button1_Click(object sender, RoutedEventArgs e)
{
playBtn.IsEnabled = false;
playIcon.Opacity = 0.3;
}
Buttons in WPF have different states = "normal", "mouse over" and "pressed" are three.
When you create the button you are setting up it's "normal" state. You also need to set the "mouse over" state to have the same image as well.
Check the below links
http://blogs.msdn.com/b/knom/archive/2007/10/31/wpf-control-development-3-ways-to-build-an-imagebutton.aspx
http://www.hardcodet.net/2009/01/create-wpf-image-button-through-attached-properties
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/91df562f-414c-4326-ac65-42ef301b5f8f/