<Image /> displays image at design-time but not at runtime; why? - c#

I'm trying to insert an image into my Windows Store app and it appears in design-time in the XAML editor but at runtime the image does not appear.
I'm pretty sure I'm using the right code here, and I have added the logo to the Assets folder however it still doesn't appear at runtime. If I got it wrong with the code or the Assets folder then it wouldn't even be appearing at design-time in the editor I think so what gives?
XAML:
<Page
x:Class="AppName.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppName"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Image Source="Assets\Logos\16380.png"
Stretch="None"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="120, 0, 0, 0"
/>
</Grid>
</Page>

I've managed to solve this problem.
When inserting images onto a page in XAML, you need to right-click the image file in Solution Explorer, click Properties and then make sure its Build Action is set to Content and then set Copy to Output Directory to Copy if newer. You also need to use forward slashes (/) and not back-slashes ().

Related

Displaying an image in WPF (without Visual Studio)

I'm trying to display an image in WPF (whether by defining it as a resource or simply by referring to the path and not defining a resource) but the image doesn't show in my application. I have a JPG image with the name 'MyImage' in a folder called 'Images'. I've seen a lot of answers saying that I need to set the build action to resource in Visual Studio but my computer cannot handle Visual Studio or any of the alternate IDE's so I'm basically doing this using cmd and Sublime Text as a text Editor.
Here's the code for MainWindow.xaml :
<Window x:Class="WPF106.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:WPF106"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="600">
<StackPanel>
<TextBlock Text="Dummy1"/>
<Image Source="/Images/MyImage.jpg" Height="150" />
<TextBlock Text="Dummy2" />
</StackPanel>
</Window>
The project folder
The application result
P.S. I have tried using Visual Studio but unfortunately that is not at all an option so if anyone could come up with a solution to this without using it (and preferably using the image as a resource), please let me know.

How to show relative source image on WPF hosted by WinForm Project without modifying behind code?

I made WinForm project and add ElementHost for using WPF blend effect on some Images.
ElementHost m_host = new ElementHost();
WPF_TK4S wpfTK4S = new WPF_TK4S();
m_host.Child = wpfTK4S;
m_host.Size = new Size(1000, 1000);
m_host.Location = new Point(0, 0);
this.Controls.Add(m_host);
I added the image control on WPF user control like below.
<UserControl x:Class="Test.WPF_TK4S"
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="1000" d:DesignWidth="1000">
<Grid>
<Grid.Background>
<ImageBrush/>
</Grid.Background>
<Image Source="/Resources/TK4S_254X253.png" HorizontalAlignment="Left" Height="344" Margin="48,44,0,0" VerticalAlignment="Top" Width="342" Stretch="None"/>
</Grid>
The image is shown well on control view but is now shown when I run the application.
Fortunately, after I changed the source uri into absolute uri like
"D://Visual Project/Test/Test/Resources/TK4S_254X253.png"
the image is shown well!!
I tried to use all kinds of relative uri like
".../Test/Resources/TK4S_254X253.png", "pack://application:,,,/TK4S_254X253.png"
but it is not work.
I need to use relative path because the project is operating on Server so every project paths are different. And I have to use this things on xaml because I have to use blend after the image is initialized.
Does anything I need to know other option?

Bing Maps in C# - Change Pushpin Image

I've been researching all over the internet for a way to change the pushpin image in the Bing Maps C# control. The closest that I came was using the following solution:
Change image for pushpin WPF
It is not entirely what I'm looking for, as I want to be able to change the color of the push pin as well as add a label.
The above solution is basically an image that is drawn over a push pin without additional functionality such as adding a label. I want to be able to easily change an image while having custom label functionality.
Is there any other way of doing this? An alternative would be to make use of "standard" Bing push pin graphics and be able to change the size. However it seems this functionality is not available in the C# control
This following blog post answers my questions:
http://dennis.bloggingabout.net/2012/10/17/bing-maps-on-wpf-and-custom-pushpin-tutorial-for-pixelsense/
Surprisingly this is the only question about this topic in SOF (the other one is closed). To complete newbies in WPF (like me) it is hard to find good and at the same time simple information, so I will show how I managed to use custom images in programatically added pushpins in VB.NET:
This is my MainWindow.xaml file:
<Window x:Class="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:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
xmlns:local="clr-namespace:MyBingMapsApp"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ControlTemplate x:Key="PushpinControlTemplate" TargetType="m:Pushpin">
<Grid>
<Rectangle Width="24" Height="24">
<Rectangle.Fill>
<ImageBrush ImageSource= "Resources/marker_green.png"/>
</Rectangle.Fill>
</Rectangle>
</Grid>
</ControlTemplate>
</Window.Resources>
<Grid>
<m:Map x:Name="myMap"
CredentialsProvider= "xxxxxx mycredentialskey xxxxxxxx"
Center="42.13618,-0.40822"
ZoomLevel="15">
</m:Map>
</Grid>
</Window>
As you can see, you must define a ControlTemplate whose TargetType="m:Pushpin"
There you can draw whatever you need. The simplest: use an image from your resources.
Important: change image "Build action" to Resource (click on the image in your resources folder of the Solution Explorer and change it in Advanced settings). Otherwise you will have to hardwrite the image path or use Uris or more complicated stuff
Now you are ready to create a pushpin wherever you need and assign your template:
mypin = New Pushpin
mypin.Location = New Location(mylat, mylon)
mypin.ToolTip = "This is a pushpin with custom image"
Dim mytemplate As System.Windows.Controls.ControlTemplate = FindResource(“PushpinControlTemplate”) 'here of course you must put the key name of your template
mypin.Template = mytemplate

Adding x:Name to a user control gives error in generated code of page its used in

I am basically using a user control for the first time, so hopefully it's just a dumb mistake.
I have a simple user control
<UserControl x:Class="TestProject.WebApp.myUserControl"
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"
xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid>
<sdk:AutoCompleteBox Name="myACB" ItemsSource="{Binding Data}" FilterMode="StartsWith" MinimumPrefixLength="2" >
</sdk:AutoCompleteBox>
</Grid>
</UserControl>
I am using the control in another page (in a stack panel):
<navigation:Page x:Class="TestProject.WebApp.myPageView"
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"
xmlns:local="clr-namespace:TestProject.WebApp"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation">
<Grid>
<StackPanel Orientation="Horizontal">
<local:myUserControl x:Name="myUC" />
</StackPanel>
</Grid>
</navigation:Page>
The control is in the same namespace as page. Removing the x:Name allows the project to compile and the control functionally works. When x:Name is specified I get an error in the generated code of the page:
Type 'TestProject.WebApp.myUserControl' is not defined.
It happens on these lines of code:
internal TestProject.WebApp.myUserControl myUC;
this.myUC == (TestProject.WebApp.myUserControl)this.FindName("myUC")
I tried with two different controls. Same thing. The generated code has "using TestProject.WebApp;" so I don't know why there is an error only when I have a named instance of the control.
I also cannot seem to use just the Name property. I even tried overloading the NameProperty in the control.
I was able to find a post about this here:
.g.vb file claims that Type X is not defined, even though it is!
Apparently, there is an issue when a project has a service reference (in my case I am using RIA services) and the user control resides in an assembly with the same root namespace as the one you are trying to use it in.
So for me it was,
TestProject.WebApp
Then I tried creating a new project for just the control
TestProject.Controls
Same problem.
Then, when I renamed the project and changed the namespace to
CustomControls
it worked.
Hopefully this saves someone else from some frustration and I really hope this problem can be fixed by MS soon.
What you are showing here should work. What is the default namespace in your project property's? Also did you rename your user control after you created it. Both of those issues could be causing you problems.

Using ICSharpCode.AvalonEdit on .Net 3.5?

I'm trying to use the ICSharpCode.AvalonEdit.TextEditor control from the SharpDevelop 4.0 project in a WPF app that I'm building, but I can't seem to get it to work.
I checked out a copy of the source code from svn://svnmirror.sharpdevelop.net/sharpdevelop/trunk/SharpDevelop/src/Libraries/AvalonEdit at revision 4304. Then, I built the project using Visual Studio 2008 SP1, which succeeded without errors.
I then created a blank new WPF project, added the build DLL to the toolbox and dropped the TextEditor control onto the default empty window, like so:
<Window x:Class="AvalonEditTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
Title="Window1" Height="300" Width="300" >
<Grid x:Name="LayoutRoot">
<avalonedit:TextEditor Name="textEditor" />
</Grid>
</Window>
However, when I run the project, the form comes up completely blank. No caret, the mouse cursor stays the default pointer, and the window does not respond to keypresses.
Am I missing something, or is AvalonEdit just a little broken?
[EDIT: I'm starting to think it might be related to my specific setup. I'm running the 64-bit Windows 7 RC. Might that have something to do with it? I've tried building it for x86 only, made no difference.]
Are you sure your namespace declaration is correct?
You can try something like this:
<Window x:Class="Editor.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
xmlns:e="clr-namespace:ICSharpCode.AvalonEdit;assembly=ICSharpCode.AvalonEdit">
<Grid>
<e:TextEditor x:Name="Editor" WordWrap="True" Height="200">
</e:TextEditor>
</Grid>
</Window>
I was able to get it to work without any issues.
The AvalonEdit TextEditor is just a view for a TextDocument model.
The problem was that a new AvalonEdit instance didn't start connected to any model instance, so there wasn't anything to edit.
The reason the code from statictype worked was that he didn't use <avalonedit:TextEditor/>, but <avalonedit:TextEditor></avalonedit:TextEditor>. This will assign an empty string to the Text property, which caused the editor to implicitly create a new document.
But this isn't relevant with recent AvalonEdit versions anymore, the editor will now always create a new TextDocument.
This works for me with the latest build
<DockPanel LastChildFill="True">
<avalonedit:TextEditor
HorizontalAlignment="Stretch"
Name="textEditor1"
VerticalAlignment="Stretch" />
</DockPanel>

Categories

Resources