When I add an image from my WPF project and use a resource for the source, it looks ok in the designer:
Main window in designer
My code is:
<Window x:Class="WpfPlay.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:WpfPlay"
mc:Ignorable="d"
Title="MainWindow" Height="271.107" Width="392.316">
<Grid>
<Image x:Name="image" HorizontalAlignment="Left" Height="128" Margin="22,22,0,0" VerticalAlignment="Top" Width="342" Source="pack://siteoforigin:,,,/Resources/logo.jpg" />
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="137,185,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Grid>
However I find that when I run the program, although the text box can be seen, the image is blank.
#Clemens has the correct answer - set the build action of the image file to resource
file properties
Just to add useful hint to #Clemens comment: if your logo.jpg file included in the same project as control, then after typing Source="pack://application:,,,/ you can press Ctrl+Space, and visual studio would show you a helper message about what file you can reference.
Related
I'm learning how to create wpf controls (and windows) with Visual Studio 2019. As a first step, I've tried to generate a Window with a label inside. Using the following code, it looks like this (designer window)
<Window x:Name="Ventana" x:Class="PhanUtils.Sistema.Recursos.Ventanas.Mensaje"
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:local="clr-namespace:PhanUtils.Sistema.Recursos.Ventanas"
Width="580" Height="175" MinWidth="580" MinHeight="175" MaxWidth="580" MaxHeight="175"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
HorizontalAlignment="Center">
<Grid>
<Label x:Name="L_Informacion" Content="Label" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="554" Height="126" BorderBrush="#FFCF1E1E" BorderThickness="5"/>
</Grid>
</Window>
However, when I run the application and I show the same dialog, it looks like this:
Does anybody know why I'm having this issue?
Adding an external dll in simple "Hello World" UWP project is giving the two errors shown in the screenshot below:
NOTE: There are many online posts on such errors but most involve complex cases (adding images, using Telerik, ComponentOne tools etc) but this is just a simple text project just to test why adding a dll from a class library that does not do any image processing etc.is causing this issue.
Error [Click on the image to make it more readable]:
UPDATE:
My MainPage.Xaml file:
<Page
x:Class="UWP_Test4.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWP_Test4"
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>
<Button Content="Button" HorizontalAlignment="Left" Margin="695,614,0,0" VerticalAlignment="Top" Click="Button_Click"/>
<RichEditBox Name="rtbTest" HorizontalAlignment="Center" Height="532" Margin="0,10,0,0" VerticalAlignment="Top" Width="1480"/>
</Grid>
</Page>
When I try adding more than one element to my WPF form in the editor in VC#2013, the previous element disappears. In the end, I can't have more than one item in the form. I've already written some code so I'd prefer not starting again from scratch. The form has nothing special besides being borderless, fullscreen and starting maximized.
This is the XAML code for the form right now:
<Window x:Class="queue_bigscreen.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1080" Width="1920" WindowStyle="None" ResizeMode="NoResize" WindowState="Maximized" Background="#FF9EA7CD">
<Label x:Name="nowServingLabel" Content="0" Margin="42,56,1160,131" Foreground="White" Height="893" FontSize="700" HorizontalContentAlignment="Center">
<Label.Effect>
<DropShadowEffect ShadowDepth="13"/>
</Label.Effect>
</Label>
</Window>
And this is what I get after I select a textbox and try adding it to the form:
<Window x:Class="queue_bigscreen.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1080" Width="1920" WindowStyle="None" ResizeMode="NoResize" WindowState="Maximized" Background="#FF9EA7CD">
<TextBlock HorizontalAlignment="Left" Margin="1332,382,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
As you can see, the label disappears, and the textbox I added in turn disappears if I try adding something else. Am I doing something wrong or is it a known bug?
You need to put the items in a container. Window can only have a single root element, so to get multiple elements on the form, you need to have an element that allows children.
The closest to Windows Forms Form would be Grid. You can then put controls in that, with absolute and relative positioning. It's also the default, so I assume you accidentally deleted it from your XAML (or by being too aggressive with pressing delete in the designer).
Example form with a label and a textbox:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label Content="Label" HorizontalAlignment="Left" Margin="23,30,0,0" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="66,32,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
In my first attempts to work with WPF I encountered a problem where coordinates that I choose in designer's view for buttons are different in what I see when I compile my program. Here's how it looks like:
Buttons are a little bit lower (cropped) in compiled version. Any ideas on what is happening here?
Code:
<Window
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" mc:Ignorable="d" x:Class="Player.MainWindow"
Title="Player" ResizeMode="CanMinimize" StateChanged="Window_StateChanged" Icon="Resources/radio.ico" WindowStartupLocation="Manual" Closing="Window_Closing" Left="0" d:DesignWidth="448" d:DesignHeight="110" Width="448" Height="110">
<Grid Margin="0">
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button x:Name="nextButton" Content="Next" HorizontalAlignment="Left" Margin="32,38,0,0" VerticalAlignment="Top" Width="45" Click="nextButton_Click" Height="22"/>
<TextBox x:Name="prevButton" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="209" IsEnabled="False" TextAlignment="Center"/>
</Grid>
</Window>
I opened up GIMP and put your images on top of each other, with the top of having 50% transparency and this is the result:
And voila - the XAML designer and the compiled output is identical. I would even assume that VS designer displays some sort of background-compiled version of your XAML to achieve this high-fidelity representation.
Back to your edited question: according to this question: Why is a window larger in runtime? you need to set the width/height on the Grid and set the SizeToContent attribute of the Window to WidthAndHeight.
Here is an updated version of your code:
<Window (...)
SizeToContent='WidthAndHeight'>
<Grid
Height='110'
Width='448'
Margin="0">
(...)
And the result is:
I have a user control with some Buttons. Each Button contains text which I'm struggling to align/centre within the control.
The XAML I have is
<UserControl x:Class="ProjectX.DetailedInfo"
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"
mc:Ignorable="d"
d:DesignHeight="28" d:DesignWidth="575">
<Grid Height="28" Width="575">
<Button Height="24" HorizontalAlignment="Left" Margin="525,2,0,0" Name="buttonOP1" VerticalAlignment="Top" Width="46" Click="buttonOP1_Click" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="11" />
</Grid>
</UserControl>
But the text is not aligning properly, see below
HorizontalContentAlignment="Center" and VerticalContentAlignment="Center" doesn't seem to be working as expected. Does anyone know why?
(I've checked the text and there's no extra characters)
Thank you
Button content is by default centered in WPF. I would assume that for the buttons that have mis-aligned captions, the text contains spaces after the actual number. But it's hard to tell without you posting all relevant code.