In XAML:
<Rectangle Stroke="Aqua" Opacity="0.7" StrokeThickness="10" Canvas.Left="24" Canvas.Top="22" Height="86" Width="102">
<Rectangle.Fill>
<ImageBrush ImageSource="C:\Users\xiaorui.dong\Pictures\profile.jpeg"></ImageBrush>
</Rectangle.Fill>
</Rectangle>
XAML works fine, but how to create the above ImageBrush in C# code:
C# should be like this:
Rectangle rectangle = new Rectangle();
rectangle.StrokeThickness = 10;
rectangle.Height = 200;
rectangle.Width = 100;
rectangle.SetValue(Canvas.LeftProperty, 100d);
rectangle.SetValue(Canvas.TopProperty, 100d);
rectangle.Fill = new ImageBrush(new BitmapImage(new Uri(#"C:\Users\xiaorui.dong\Pictures\profile.jpeg")));
I'm guessing the problem is with locating the image and that the exception you're getting is because you don't provide the UriKind parameter. Try giving UriKind.Relative as a parameter to the Uri:
rectangle.Fill = new ImageBrush(new BitmapImage(
new Uri(#"C:\Users\xiaorui.dong\Pictures\profile.jpeg", UriKind.Relative)));
In c# you can use it like , first thing is remove the fill from XAML then use the same code in c# as you have used it . It must work.
ImageBrush ib = new ImageBrush();
ib.ImageSource = new BitmapImage(new Uri("your path",UriKind.Relative));
rectangle.Fill = ib;
This code creates ImageBrush using image "Assets/image.png" which is part of the UWP project. Relative paths are unavailable in UWP version of ImageBrush.
var imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/image.png"));
Related
I'm trying to draw a pushpin to a map in the map page of my application but when I add the code to overlay a pushpin to the map, I get the following errors:
The type or namespace name PushPin could not be found (are you missing a using directive or an assembly reference?)
and
Microsoft.Phone.Maps.Controls.Map does not contain a definition for Children and no extension method Children accepting a first argument of type Microsoft.Phone.Maps.Controls.Map could be found
I understand from this that I'm missing a reference? I have references for controls.maps and contols.toolkit, so I can't understand why.
Also this is the code I'm using to draw the pushpin:
PushPin myPin = new Pushpin();
myPin.Location = MyGeoPosition;
myPin.Content = "My car";
MyMap.Children.Add(myPin);
Try this.
MapLayer layer1 = new MapLayer();
Pushpin pushpin1 = new Pushpin();
pushpin1.GeoCoordinate = MyGeoPosition;
pushpin1.Content = "My car";
MapOverlay overlay1 = new MapOverlay();
overlay1.Content = pushpin1;
overlay1.GeoCoordinate = MyGeoPosition;
layer1.Add(overlay1);
myMap.Layers.Add(layer1);
You create new overlay for each pushpin, add all overlays to a layer, and add the layer to the map element.
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);
Try this. First I have this in a utils class:
public MapLayer GetCurrentPosition (out MapOverlay myPositionOverlay)
{
MapLayer myPositionLayer = new MapLayer();
myPositionOverlay = new MapOverlay();
Image myPositionImage = new Image();
myPositionImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Assets/myImagePin.png", UriKind.RelativeOrAbsolute));
myPositionImage.Height = 70;
myPositionImage.Visibility = Visibility.Visible;
myPositionImage.HorizontalAlignment = HorizontalAlignment.Center;
myPositionOverlay.Content = myPositionImage;
myPositionLayer.Add(myPositionOverlay);
return myPositionLayer;
}
Then this goes on your MapLoaded method:
MapLayer mypositionArrow = placePushPins.GetCurrentPosition(out myPositionOverlay);
MyMap.Layers.Add(mypositionArrow);
Then, if you want your pushpin not to be static, to move according to your position, add this to your watcher:
myPositionOverlay.GeoCoordinate = myCurrentPositionCoordinate;
To make the pushpin move smoothly just implement animation like this: http://blogs.bing.com/maps/2014/09/25/part-2-bring-your-maps-to-life-creating-animations-with-bing-maps-net/
I just cleared layers off my mapping program and when I try to add a new layer I receive this error message.
newSystem.ArgumentException was unhandled by user code
HResult=-2147024809
Message=Value does not fall within the expected range.
Source=System.Windows
InnerException:
If anyone knows why this is I would very much appreciate your assistance
private void loadZoomLevel12Pics()
{
map1.Layers.Clear();
MapLayer pinLayer = new MapLayer();
// Create a new empty Pushpin
// Beny Sur- Mer War Cemetary
MapOverlay pinOverlay = new MapOverlay();
// Add the location of the Pushpin using latitude and longitude.
pinOverlay.GeoCoordinate = new GeoCoordinate(49.33783000, -0.45215600);
//Image pinOverlayImage = new Image();
pinOverlayImage.Source = new BitmapImage(new Uri("images/Hedgehog.png", UriKind.Relative));
pinOverlay.Content = pinOverlayImage;
pinOverlay.PositionOrigin = new Point(0.0, 0.0);
pinOverlayImage.Opacity = 0.8;
pinOverlayImage.Height = 8;
pinOverlayImage.Width = 8;
pinOverlayImage.Tap += pinOverlayImage_Tap;
pinLayer.Add(pinOverlay);
map1.Layers.Add(pinLayer);
Then these pictures are cleared and a new zoom level is loaded
private void loadZoomLevel13Pics()
{
map1.Layers.Clear();
MapLayer pinLayer = new MapLayer();
// Create a new empty Pushpin
// Beny Sur- Mer War Cemetary
MapOverlay pinOverlay = new MapOverlay();
// Add the location of the Pushpin using latitude and longitude.
pinOverlay.GeoCoordinate = new GeoCoordinate(49.33783000, -0.45215600);
//Image pinOverlayImage = new Image();
pinOverlayImage.Source = new BitmapImage(new Uri("images/Hedgehog.png", UriKind.Relative));
pinOverlay.Content = pinOverlayImage;
pinOverlay.PositionOrigin = new Point(0.0, 0.0);
pinOverlayImage.Opacity = 0.8;
pinOverlayImage.Height = 30;
pinOverlayImage.Width = 30;
pinOverlayImage.Tap += pinOverlayImage_Tap;
pinLayer.Add(pinOverlay);
map1.Layers.Add(pinLayer); // THIS IS THE LINE CAUSING THE PROBLEM
All of the images are declared globally because they are used in other functions/methods inside the program.
It seems like its trying to add the same layer that was previously added and is having difficulty doing so, but all of the layers are cleared on the first line of the method.
I had this same issue using a global polygon instead of an image. The issue is that even though you are calling map1.Layers.Clear() this does not get done immediately after it is called therefore you need to create a new image. In general, this can be fixed by not using global/instance variables for the overlay.Content...just declare it each time and assign it to the Content.
how i can set background on string path?
canvas1.Background = "/path/";
please help!!! thanks :)
I think you are looking for the ImageBrush class:
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage(new Uri(#"/path/", UriKind.Relative));
canvas1.Background = imageBrush;
Use this
mycanvas.Background = new ImageBrush()
{ ImageSource = new BitmapImage((new Uri(mypath, UriKind.Absolute))) };
I want to create a button in c# with no content but just a image within it.
I dont know how to use the setter. Can anyone enlighten me?
Setter setter = new Setter();
setter.Property = Image.SourceProperty;
setter.Value = "asd.jpg"; //<--error
Style Rstyle = new Style();
Rstyle.TargetType = typeof(Button);
Rstyle.Setters.Add(setter);
Button _Button = new Button()
{
Width = 41,
Height = 41
};
_Button.Style = Rstyle;
What should i put at setter.Value to obtain the image which resides in the project directory. "asd.jpg"
Button has a Content property that you can just dump a BitmapImage in it:
BitmapImage image = new BitmapImage(new Uri("your source"));
TheButton.Content = image;
the setter is not used in c# code, its a helper class to build styles in xamls.
Just use this code.
BitmapImage bmp = new BitmapImage()
bmp.BeginInit();
bmp.UriSource = new Uri("pack://application:,,,/ApplicationName;component/Resources/myImage.png");
bmp.EndInit();
Image image = new Image();
image.Source = bmp;
myButton.Content = image;
<ControlTemplate x:Key="image"> //XAML in window resources tag
<Image Source="Save.png"/>
</ControlTemplate>
btn.Template = (ControlTemplate)FindResource("image");
I think this is more flexible and reusable.
can someone give the C# equivalent of this xamle code?
<ListBox Width="400" Margin="10"
ItemsSource="{Binding Source={StaticResource myTodoList}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=TaskName}" />
<TextBlock Text="{Binding Path=Description}"/>
<TextBlock Text="{Binding Path=Priority}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
That's because I would like to know how to dynamically add PivotItems into a Pivot, for a windows phone 7 app, with the default Pivot layout generate in VS 2010. For instance, I got this code, but I didn't find a way to add the StackPanel object sp, to ListBox:
// epgXml is an XElement object, wich has channel and programmes tags
// it's an app about a tv guide, where the user clicks on a channel and gets a list of programmes for that channe.
// in th xaml code, I have a grid with a pivot object in it:
<Grid x:Name="LayoutRoot" Background="Transparent">
<controls:Pivot x:Name="MainPivot" ItemsSource="{Binding PivotItems}" Title="ZON EPG">
</controls:Pivot>
</Grid>
// In MainViewModel.cs I have this code:
public void client_GetEPGCompleted(Object sender, GetEPGCompletedEventArgs e)
{
DataTemplate dt = new DataTemplate();
epgXml = e.Result.Output.EPGXml;
if (epgXml != null)
{
//Channels = new List<string>();
// parse the xml and show channels and programmes
var channels = from c in epgXml.Elements("channel")
select c;
foreach (XElement el in channels)
{
PivotItem pi = new PivotItem();
pi.Header = el.Elements("display-name").FirstOrDefault().Value;
ListBox lb = new ListBox();
lb.Margin = new Thickness(0, 0, -12, 0);
//ItemsControl ic = new ItemsControl();
//ic.ItemTemplate = dt;
lb.ItemTemplate = dt;
StackPanel sp = new StackPanel();
sp.Margin = new Thickness(0, 0, 0, 17);
sp.Width = 432;
TextBlock tb = new TextBlock();
Binding binding = new Binding("ProgTitle");
binding.Mode = BindingMode.TwoWay;
tb.SetBinding(TextBlock.TextProperty, binding);
tb.Margin = new Thickness(12, 0, 0, 0);
tb.TextWrapping = TextWrapping.NoWrap;
tb.Style = Application.Current.Resources["PhoneTextExtraLargeStyle"] as Style;
sp.Children.Add(tb);
tb = new TextBlock();
binding = new Binding("ProgStart");
binding.Mode = BindingMode.TwoWay;
tb.SetBinding(TextBlock.TextProperty, binding);
tb.Margin = new Thickness(12, -6, 0, 0);
tb.TextWrapping = TextWrapping.NoWrap;
tb.Style = Application.Current.Resources["PhoneTextSubtleStyle"] as Style;
sp.Children.Add(tb);
tb = new TextBlock();
binding = new Binding("Duration");
binding.Mode = BindingMode.TwoWay;
tb.SetBinding(TextBlock.TextProperty, binding);
tb.Margin = new Thickness(12, 0, 0, 0);
tb.TextWrapping = TextWrapping.NoWrap;
tb.Style = Application.Current.Resources["PhoneTextSubtleStyle"] as Style;
sp.Children.Add(tb);
// get programmes foreach channel
var progrs = from p in epgXml.Elements("programme")
where p.Attributes("channel").FirstOrDefault().Value == el.Attributes("id").FirstOrDefault().Value
select new ItemViewModel()
{
ProgTitle = p.Elements("title").FirstOrDefault().Value,
ProgStart = p.Attributes("start").FirstOrDefault().Value,
Duration = p.Elements("length").FirstOrDefault().Value
};
foreach (ItemViewModel item in progrs)
{
this.Items.Add(item);
}
// create new instance - this holds all the programms for each channel
this.Items = new ObservableCollection<ItemViewModel>();
PivotItems.Add(pi);
}
Thx in advance
You have to use XamlReader to create datatemplates (as in your XAML) on the phone.
See a full example and explanation at http://www.windowsphonegeek.com/tips/wp7-dynamically-generating-datatemplate-in-code
For an example off dynamically adding PivotItems seee How to dynamically add pivot item and add Image to each pivot item?