Adding Image to Grid C# - c#

My problem is that the image that I am setting to my grid is not appearing, the only thing appearing is the black background, so I know the grid is working. I am a noob, and I am very confused. Thanks for the Help :)
Code:
public partial class MainWindow : Window
{
static String ImgNameMole = "C:/Users/MonAmi/Desktop/mole2.png";
public MainWindow()
{
InitializeComponent();
GridMain();
}
private void GridMain()
{
Grid grid_Main = new Grid();
MainWindow1.Content = grid_Main;
grid_Main.Height = 350;
grid_Main.Width = 525;
grid_Main.Background = Brushes.GreenYellow;
CreateImage();
}
private Image CreateImage()
{
Image Mole = new Image();
Mole.Width = 25;
Mole.Height = 25;
ImageSource MoleImage = new BitmapImage(new Uri(ImgNameMole));
Mole.Source = MoleImage;
return Mole;
}
}

Nowhere in your code you are calling CreateImage(), so:
var img = CreateImage();
Grid.SetRow(img, 0);
Grid.SetColumn(img, 0);
grid_Main.Children.Add(img);
assuming that you have added at least one row and one column to your grid.

Related

Custom button not rendering correctly

I've got a custom class for a button with a circular image as I'll be using it multiple times through my program. I thought it'd be pretty simple of creating class, inheriting from Button and slapping my setup into a constructor, but when I'm running the program the buttons are massive and plain (no image or text). Here's my class:
public class ImageButton : Button
{
public Button Button;
public ImageButton(string filename) : this(HorizontalAlignment.Center, VerticalAlignment.Center, filename)
{ }
public ImageButton(HorizontalAlignment hAlignment, VerticalAlignment vAlignment, string filename)
{
Button = new Button
{
Width = 35,
Height = 35,
Background = Brushes.Transparent,
HorizontalAlignment = hAlignment,
BorderBrush = Brushes.Transparent,
VerticalAlignment = vAlignment,
Content = new Image
{
Source = new BitmapImage(new Uri("pack://application:,,,/Resources/" + filename))
}
};
}
}
And here's my implementation of one of the instances
private void SetupHeaders(Grid resultGrid)
{
RowDefinition backbtn = new RowDefinition();
backbtn.Height = new GridLength(0.2, GridUnitType.Star);
resultGrid.RowDefinitions.Add(backbtn);
btn_Return = new ImageButton(HorizontalAlignment.Left, VerticalAlignment.Top, "returnicon.png");
Grid.SetRow(btn_Return, 0);
Grid.SetColumn(btn_Return, 0);
resultGrid.Children.Add(btn_Return);
}
with btn_Return being defined at the top of the class as simply
ImageButton btn_Return;
Here's an image of one of the buttons.
In you constructor you inititalize a Button with you properties and then assign it to a property. You never actually use the initialized button. You are always using ImageButton which is nothing more than an inherited button therefore you get the default behavior.
You have to change your constructor.
public ImageButton(HorizontalAlignment hAlignment, VerticalAlignment vAlignment, string filename)
{
Width = 35;
Height = 35;
Background = Brushes.Transparent;
HorizontalAlignment = hAlignment;
BorderBrush = Brushes.Transparent;
VerticalAlignment = vAlignment;
Content = new Image
{
Source = new BitmapImage(new Uri("pack://application:,,,/Resources/" + filename))
};
}

How to resize the subviews in a View, which is converted into UIView from Xamarin.forms.View

I am working to convert a Xamarin Forms View into a Native UIView using the below code snippet. But the size of view could not be get from the Xamarin forms View, and also the subviews could not be auto resized when the view is Resized.
I have tried using AutoResizingMask but it did not worked.Any help is much appreciated.
public void CustomView(int pageIndex, View customView, Point offset)
{
var visualElement = Platform.CreateRenderer(customView);
visualElement.NativeView.Frame = new CGRect((int)offset.X, (int)offset.Y,
(int)100, (int)100);
visualElement.NativeView.AutoresizingMask =
UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
visualElement.NativeView.ContentMode = UIViewContentMode.ScaleToFill;
visualElement.NativeView.AutosizesSubviews = true;
visualElement.Element.Layout(new CGRect((int)offset.X, (int)offset.Y,
(int)100, (int)100).ToRectangle());
visualElement.Element.AutoresizingMask = UIViewAutoresizing.All;
visualElement.NativeView.AutosizesSubviews = true;
}
CustomView passed on the above:
Grid grid = new Grid();
grid.HeightRequest = 400;
grid.WidthRequest = 300;
grid.BackgroundColor = Color.Yellow;
Image customImage = new Image();
customImage.VerticalOptions = LayoutOptions.FillAndExpand;
customImage.HorizontalOptions = LayoutOptions.FillAndExpand;
customImage.Source = "Approved.png";
grid.Children.Add(customImage);
I have tried to resize the NativeView after conversion, but image is not resizing according to the layout change.
We don't use AutoResizingMaskanymore since auto Layout comes out .
Try to modify your code
public void CustomView(int pageIndex, View customView, Point offset)
{
var visualElement = Platform.CreateRenderer(customView);
var view = visualElement.NativeView;
var margins = View.LayoutMarginsGuide;// Get the parent view's layout
view.LeadingAnchor.ConstraintGreaterThanOrEqualTo(margins.LeadingAnchor,(nfloat)offset.X).Active = true;
view.TopAnchor.ConstraintGreaterThanOrEqualTo(margins.TopAnchor,(nfloat)offset.Y).Active = true;
view.HeightAnchor.ConstraintEqualTo(100).Active = true;
view.WidthAnchor.ConstraintEqualTo(100).Active = true;
}

How to add a proceduaraly generated billboard to helix 3D

I have a set of procedural images that I would like to add as billboards to my helix 3D application.
Currently my application looks as following:
public partial class _3DControl
{
HelixViewport3D hVp3D;
public _3DControl()
{
InitializeComponent();
createView();
}
public void createView()
{
hVp3D = new HelixViewport3D();
var lights = new SunLight();
lights.Altitude=40;
lights.Ambient=0.4;
this.Content = hVp3D;
hVp3D.Children.Add(lights);
this.Show();
}
public void UploadBillboard(BitmapImage im, System.Windows.Media.Media3D.Point3D position,double width,double height)
{
//create material
var mat = MaterialHelper.CreateImageMaterial(im, 0);
var bboard = new BillboardVisual3D();
bboard.Material = mat;
//set coordinates
bboard.Position = position;
bboard.Width = width;
bboard.Height = height;
//add the billboard
hVp3D.Children.Add(bboard);
}
However when I call the function to add a billboard:
HelixLinker.GetHelix().UploadBillboard(((Bitmap)e).bitmapToBitmapImage(),
new System.Windows.Media.Media3D.Point3D(0, 0, 0), 100, 100);
Then I see nothing being added, any idea what I' m doing wrong?
I also tried with the RectangleVisual3D class.
public void UploadRect(BitmapImage im, System.Windows.Media.Media3D.Point3D position, double width, double height)
{
var mat = MaterialHelper.CreateImageMaterial(im, 0);
var bboard = new RectangleVisual3D ();
bboard.Material = mat;
bboard.Width = width;
hVp3D.Children.Add(bboard);
}
Which if execuded in the same way results in a (promising) image however in this case the material appears not to be properly set.
Note: I hope that the BillboardVisual3D is the right class, I'm working on something that will allow me to put image "on the floor" so to speak, I want to have flat images that don't have a depth and allow for transparancy.
Why do you set the Opacity to 0? Then you wont see anything.
Try this:
//create material
var mat = MaterialHelper.CreateImageMaterial(im);
See the documentation
Edit:
Sorry, my bad with the string. But this ist working for me:
Code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CreateBilboard();
}
private void CreateBilboard()
{
BitmapImage im = new BitmapImage(new System.Uri(#"C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg"));
var mat = MaterialHelper.CreateImageMaterial(im, 1);
var bboard = new BillboardVisual3D();
bboard.Material = mat;
var position = new System.Windows.Media.Media3D.Point3D(0, 0, 0);
var width = 100;
var height = 100;
//set coordinates
bboard.Position = position;
bboard.Width = width;
bboard.Height = height;
//add the billboard
viewPort.Children.Add(bboard);
}
}
XAML:
<Window x:Class="BillboardImageMaterialDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ht="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
Title="BillboardImageMaterialDemo" Height="480" Width="640">
<Grid>
<ht:HelixViewport3D x:Name="viewPort" ZoomExtentsWhenLoaded="True">
<!-- Remember to add some lights -->
<ht:SunLight/>
</ht:HelixViewport3D>
</Grid>
</Window>
Result:

How to Change Content of Button if it is a child of a grid?

I want to change content of button1 on click event of button2 . But not able to get the object of grid's child Button class which is in List<> UiList.
Please Guide me in getting the right approach to look it and solve it . And also guide that if the object is build in runtime then how to access it ?
public partial class MainPage : PhoneApplicationPage
{
List<Grid> UIList = new List<Grid>();
Grid objGrid1 = null;
Button objButton1 = null;
Button objButton2 = null;
// Constructor
public MainPage()
{
InitializeComponent();
createGrid1("grid1");
createButton2("Button2");
}
public void createGrid1(string x)
{
objGrid1 = new Grid();
objGrid1.Height = 100;
objGrid1.Name = x;
objGrid1.Width = 200;
objGrid1.Margin = new Thickness(100, 100, 0, 0);
objGrid1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
objGrid1.VerticalAlignment = System.Windows.VerticalAlignment.Top;
objGrid1.Background = new SolidColorBrush(Colors.Orange);
createButton1("changename");
}
public void createButton1(string _name)
{
objButton1 = new Button();
objButton1.Height = 90;
objButton1.Name = _name;
objButton1.Content="Button1";
objButton1.FontSize = 20;
objButton1.Width = 190;
objButton1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
objButton1.VerticalAlignment = System.Windows.VerticalAlignment.Top;
objButton1.Background = new SolidColorBrush(Colors.Blue);
objButton1.Foreground = new SolidColorBrush(Colors.White);
objGrid1.Children.Add(objButton1);
LayoutRoot.Children.Add(objGrid1);
UIList.Add(objGrid1);
}
public void createButton2(string _name)
{
objButton2 = new Button();
objButton2.Margin = new Thickness(240, 300, 0, 0);
objButton2.Name = _name;
objButton2.Height = 90;
objButton2.Content = "Button2";
objButton2.FontSize = 20;
objButton2.Width = 190;
objButton2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
objButton2.VerticalAlignment = System.Windows.VerticalAlignment.Top;
objButton2.Background = new SolidColorBrush(Colors.Blue);
objButton2.Foreground = new SolidColorBrush(Colors.White);
LayoutRoot.Children.Add(objButton2);
objButton2.Click += (s, e) =>
{
int c = UIList.ElementAt(0).Children.Count;
if (c == 1)
{
//logic to change content of Button1 on click of Button2
}
};
}
}
Assuming you cannot just keep a reference to the created control, such as in a class field, you can iterate through the Children property of the Grid and find the desired Button. If there are multiple buttons, you can differentiate them using the Tag property.
Once you find it, change the contents of the button using the Content property, as discussed in the contents above.

How to populate dynamically a grid on WP7?

this is my very first program in WP7, so i have some issues.
I'm trying to populate a grid with an list of objects that has been bought by another method. Here what i made so far:
public partial class MainPage : PhoneApplicationPage
{
private List<Row> lsResult;
private Grid myGrid = new Grid();
private int i = 0;
// Constructor
public MainPage()
{
InitializeComponent();
ColumnDefinition colData = new ColumnDefinition();
ColumnDefinition colOcorrencia = new ColumnDefinition();
ColumnDefinition colSituacao = new ColumnDefinition();
myGrid.ColumnDefinitions.Add(colData);
myGrid.ColumnDefinitions.Add(colOcorrencia);
myGrid.ColumnDefinitions.Add(colSituacao);
myGrid.ShowGridLines = true;
SolidColorBrush myBrush = new SolidColorBrush(Colors.White);
myGrid.Background = myBrush;
gridResult = myGrid;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
i = 0;
lsResult = null;
lsResult = Rastrear.Busca(txtNumber.Text);
foreach (Row r in lsResult)
{
RowDefinition rNewRow = new RowDefinition();
myGrid.RowDefinitions.Add(rNewRow);
TextBlock lblData = new TextBlock();
lblData.Text = r.Data.ToString();
lblData.HorizontalAlignment = HorizontalAlignment.Center;
lblData.VerticalAlignment = VerticalAlignment.Center;
Grid.SetColumnSpan(lblData, 1);
Grid.SetRow(lblData, i);
TextBlock lblOcorrencia = new TextBlock();
lblOcorrencia.Text = r.Ocorrencia.ToString() ;
Grid.SetColumnSpan(lblOcorrencia, 2);
Grid.SetRow(lblOcorrencia, i);
TextBlock lblSituacao = new TextBlock();
lblSituacao.Text = r.Situacao.ToString();
Grid.SetColumnSpan(lblSituacao, 3);
Grid.SetRow(lblSituacao, i);
i++;
myGrid.Children.Add(lblData);
myGrid.Children.Add(lblOcorrencia);
myGrid.Children.Add(lblSituacao);
}
gridResult = myGrid;
}
}
The method Buscar() is returning the list as it should, but when i click on the button it doesn't do anything, not even the paint to white on the constructor happens actually.
thanks in advance
You're doing some pretty shady stuff in your code behind that i'm used to seeing and should be done in xaml. Change this
gridResult = myGrid;
to
gridResult.Children.Add(myGrid);
I don't think you can just change references of static UI elements like that.

Categories

Resources