Render Xaml Controls from C# code behind in UWP - c#

The Image that I have created from Code behind is visible in my Visual tree but is not rendered on screen when I run my app.
This is my Code Behind
public MainPage()
{
this.InitializeComponent();
//Image that was created in Xaml
image.Source = new BitmapImage(new Uri("ms-appx:///testimage.jpg"));
//Image creation from C# code
Image image1 = new Image();
image1.Width = 200;
image1.Height = 200;
image1.Margin = new Thickness(50, 50, 0, 0);
image1.Source = new BitmapImage(new Uri("ms-appx:///download.jpg"));
//grid is the Grid name in Xaml
grid.Children.Add(image1);
}
This is my XAML
<Page Name="HI"
x:Class="test1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:test1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid x:Name="grid">
<Image x:Name="image" Visibility="Visible" HorizontalAlignment="Left" Height="200" Margin="10,200,0,0" VerticalAlignment="Top" Width="200"/>
</Grid>
What am I missing here?

Your Xaml isn't complete. You are missing a closing tag for Page.

Related

c# Windows Foundation why can't I draw a shape # coordinates

W11, VS2022
I'm fairly new to UI design in c#. I'm trying to draw a control at runtime and I can't seem to find the way to specify its x & y properties or set it to a Point.
xaml
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid x:Name="layoutRoot"></Grid>
</Grid>
</Page>
c# method
public MainPage()
{
this.InitializeComponent();
var ellipse1 = new Ellipse
{
Fill = new SolidColorBrush(Windows.UI.Colors.Pink),
Width = 200,
Height = 200
};
layoutRoot.Children.Add(ellipse1);
}
`
Have tried setleft, setright and location but they are all missing.
Thanks in Advance
Change it to Canvas
var ellipse1 = new Ellipse
{
Fill = new SolidColorBrush(Colors.Pink),
Width = 200,
Height = 200
};
int left = 50, top = 50;
Canvas.SetLeft(ellipse1, left); // second option Canvas.SetRight()
Canvas.SetTop(ellipse1, top); // second option Canvas.SetTop()
layoutRoot.Children.Add(ellipse1);
EDIT:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Canvas x:Name="layoutRoot">
</Canvas>
</Page>

Change color from another class is not working

I try to change the color of a canvas by pressing a button using command binding insted of click event to avoid any code behind in the MyView.xaml.cs file. The command is fireing and the messageboxes show the correct color code values before and after changing it so the new color is set but the color of the canvas do not change. If a use a click event and the code behind in the MyView.xaml.cs insted than all work fine but I would like to get it work wiht command bindning and without code behind in the MyView.xaml.cs file. How can I do that and what is that I am not doing right?
MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:View="clr-namespace:WpfApp1.View"
xmlns:ViewModel="clr-namespace:WpfApp1.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Background="YellowGreen"
WindowStartupLocation="CenterScreen">
<Window.DataContext>
<ViewModel:MyClass/>
</Window.DataContext>
<View:MyView/>
</Window>
MyView.xaml file
<UserControl x:Class="WpfApp1.View.MyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ViewModel="clr-namespace:WpfApp1.ViewModel"
xmlns:View="clr-namespace:WpfApp1.View">
<Canvas Name="MainCanvas" Height="300" Width="502" Background="#FF148B63">
<Button Name="ChangeColorButton" Command="{Binding CommandChangeColor}" Content="new color" Height="25" Width="55" Margin="225,268,225,10"/>
</Canvas>
</UserControl>
MyClass.cs
public class MyClass : MyView
{
List<Color> _listOfColors = new List<Color>();
public MyClass(){ MyInitColor(); }
public ICommand CommandChangeColor
{
get { return new MyCommand(ChangeColor); }
}
public void ChangeColor()
{
MessageBox.Show("Color before: " + MainCanvas.Background.ToString());
Random rnd = new Random();
int i = rnd.Next(_listOfColors.Count - 1);
MainCanvas.Background = new SolidColorBrush(_listOfColors[i]);
MessageBox.Show("Color after: " + MainCanvas.Background.ToString());
}
}
Since you can have many instances of a UserControl, how would MyClass know which instance of MainCanvas it should be updating?
So similar to your binding for CommandChangeColor you should also bind the Background to some property in MyClass. Something like:
Xaml:
<Canvas Name="MainCanvas" Height="300" Width="502" Background="{Binding BackGroundColor}">
<Button Name="ChangeColorButton" Command="{Binding CommandChangeColor}" Content="new color" Height="25" Width="55" Margin="225,268,225,10"/>
</Canvas>
MyClass.cs
public SolidColorBrush BackGroundColor {get;set;}
public void ChangeColor()
{
MessageBox.Show("Color before: " + MainCanvas.Background.ToString());
Random rnd = new Random();
int i = rnd.Next(_listOfColors.Count - 1);
BackGroundColor = new SolidColorBrush(_listOfColors[i]);
MessageBox.Show("Color after: " + MainCanvas.Background.ToString());
}
You will probably also want to implement INotifyPropertyChanged.

Printing a WPF Visual Containing Images

I am trying to print a WPF visual (a Canvas to be exact) containing multiple images. The image sources are loaded from base64 strings that are converted to BitmapSources. When the canvas is shown in a Window the 2 images are displayed correctly, however when its printed using PrintVisual method of PrintDialog, the images both appear the same. I have created a scaled down example that exhibits the behavior that I am seeing.
Here is the XAML I'm using:
<Window x:Class="ImagePrintTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ImagePrintTest"
mc:Ignorable="d"
Title="MainWindow" Height="800" Width="600">
<StackPanel>
<Button Content="Print" Click="Button_Click" />
<Canvas x:Name="ImageCanvas">
<Image x:Name="ImageA" Canvas.Left="50" Canvas.Top="50" Width="380" Height="56" />
<Image x:Name="ImageB" Canvas.Left="50" Canvas.Top="150" Width="380" Height="56" />
</Canvas>
</StackPanel>
And this is the code behind:
public partial class MainWindow : Window
{
string ABC_DATA = #"iVBORw0KGgoAAAANSUhEUgAAAXwAAAA4CAYAAAD+WUMEAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAxCSURBVHhe7d15cBPXHQfw7+q+JcuSbfm2aWywwRwmlHhCIAdtSpq0SSkz7aQJaabJH50m7TTptE0n+Seh02lnaKczaZv0SNp0kskxjXMMJENJORuOYBtsA8YHsi1fknVZklfn61tLBBsMCPA/ZX8fzxu0u5JWfzy++/a93bcCYwyEEEJufIrcv4QQQm5wFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITNB8+mUOMnWE9kVGcFuN8SWoPOLDYUYNqg0Uwz7wjD+kwYtMDbFfQh3gmk1s5P6VCD7OpCatNRhSolEJudZ6k756Ge7KD9U5H4Z9nVxZ9JSrN9Viiw1V+NyE3Hgp8MksKg8N/Y6+49+DVyRBfVvLSgIdXPojNpQ1oUOYZmmIvGx1/C/d1HYE/lcytnJ9abUe567t4wlWN1TYn7FqDoMttu5xkKoxwfJSNpfz4pPuv+DAwhp5UbuMsFfb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4t+VSxN2vj5SEb+2Jq9Tw3daZWOD6UwpmhOF7MArtbmUaCaAtj4IfNnItPJkv6uyNl/GXax7w0Nb4HKKQiJnZ0muiTVO51ZMlFcTxn4ctZQ607j/2yjU6PvCwewdF8FBcOOziWNNC18hJQBj7R/+CQTthIUFtSTQ+YCnn5sbkdwEA+5eDccAo/eCKNX30upX64x1Ev5AwlxTkRtTlW+GHjeVJge3h3wtPXixr3pKmZCr8zZmJft43tETlgtHqpfdXDRMHT8h5wu6Ixt9TcNMtET8LEiS55tJVaNYuD56PoVvHupKdHCxoz0tSTkfeetvGIbJzhrQIUMgx8IqKQ4AdxIqKQYOATEYUEA5+IKCQY+EREIcHAJyIKCQY+EVFIMPCJiEKCgU9EFArAfwE2JuRnUGyVRgAAAABJRU5ErkJggg==";
public MainWindow()
{
InitializeComponent();
ImageA.Source = Convert(ABC_DATA);
ImageB.Source = Convert(DEF_DATA);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
{
printDialog.PrintVisual(ImageCanvas, "image print test");
}
}
public static BitmapSource Convert(string s)
{
byte[] bytes;
bytes = System.Convert.FromBase64String(s);
BitmapSource source;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes))
{
source = PngBitmapDecoder.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames[0];
}
return source;
}
}
I am assuming that the image sources are being cached somehow somewhere but I really cant explain this behavior. Can anyone explain what is happening here?
It works for me when I create a second BitmapFrame like this:
public static BitmapSource Convert(string s)
{
using (var ms = new MemoryStream(System.Convert.FromBase64String(s)))
{
return BitmapFrame.Create(
BitmapFrame.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad));
}
}
However, I don't have an explanation for this behaviour.
I also noticed that printing seems to spoil the layout. After printing, the ImageCanvas moves to position (0,0) in its StackPanel parent.

how to a zoom option to my wpf app?

I'm working on wpf application , the application draws the countours on an existing image and i want to add a button which give me the hand to zoom the image to see the countours
i added a button and a fonction on .cs but it doesn't work
here's the code cs
namespace AnimationTest
{
public partial class MainWindow: Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
DoubleAnimation da = new DoubleAnimation();
da.From = 0;
da.To = 1000;
da.Duration = new Duration(TimeSpan.FromSeconds(1));
image1.BeginAnimation(ScaleTransform.CenterXProperty, da);
}
}
}
and XAML Code
<Window x:Class="AnimationTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="720" Width="1280">
<Grid>
<Image Height="681" HorizontalAlignment="Left" Name="image1" Stretch="None" VerticalAlignment="Top" Width="1258" Source="/AnimationTest;component/Images/world.jpg" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="1171,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
what's the problem ? and how can i fixe it ?
thanks
This has no sens image1.BeginAnimation(ScaleTransform.CenterXProperty, da); You start an animation on an image of a property that don't belong to Image control. You should create a scale transform, set it on the image and then start the animation on scale transform object, not the image.

Image margin behavior

When I set my image margin in run-time it acts strange, different then when I set it via xaml code.
I want the margin value to be (40, 16, 0, 0).
When I add it in run-time, my code is this:
System.Windows.Controls.Image proba = new System.Windows.Controls.Image();
ImageSource imageSource = GetSlidePart(PresentationDocument.Open(path, false), 0, "rId2");
proba.Source = imageSource;
proba.Width = 2245721 / 9525;
proba.Height = 2286004 / 9525;
proba.Margin = new Thickness(40, 16, 0, 0);
gridName.Children.Add(proba);
But, when I add image in xaml, the code in cs is this:
image1.Source = GetSlidePart(PresentationDocument.Open(path, false), 0, "rId2");
and the code in xaml is this:
<Window x:Class="GetInfoFromPptxFile.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="720" Width="960">
<Grid Name="gridName">
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="416,20,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Image Height="150" HorizontalAlignment="Left" Margin="40,16,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" />
</Grid>
</Window>
When I run my program, image1 is exactly where I want it to be, and the image named proba is not. Why is this when I give them the same margin values?
That's because you're not setting the horizontal and vertical alignments in C#.
Try adding:
proba.VerticalAlignment = VerticalAlignment.Top;
proba.HorizontalAlignment = HorizontalAlignment.Left;
and it'll look the same.

Categories

Resources