Displaying an image in WPF (without Visual Studio) - c#

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.

Related

"DataContext is not set" message on main window

I'm getting the XLS1106 "DataContext is not set" message on my main window XAML in Visual Studio. As far as I can tell, I haven't used anything related to that and nothing is broken. I would just supress the message, but I'm not sure why it's there.
Here is my whole XAML:
<Window x:Name="MyWindow" x:Class="Whiteboard.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:Whiteboard"
mc:Ignorable="d"
Title="Untitled - CFWhiteboard" Height="450" Width="800" MouseDown="WindowMouseDown" MouseMove="WindowMouseMove" KeyDown="WindowKeyDown" KeyUp="WindowKeyUp" Icon="icons8-interactive-whiteboard-100.png" Loaded="WindowLoaded" Closing="WindowClosing">
<Window.Resources>
<ResourceDictionary>
<FrameworkElement x:Key="CursorRectangle" Cursor="Resources/Cursors/rectangle.cur"/>
<FrameworkElement x:Key="CursorSquare" Cursor="Resources/Cursors/lockrect.cur"/>
<FrameworkElement x:Key="CursorEllipse" Cursor="Resources/Cursors/ellipse.cur"/>
<FrameworkElement x:Key="CursorCircle" Cursor="Resources/Cursors/lockellipse.cur"/>
</ResourceDictionary>
</Window.Resources>
<Canvas x:Name="MainCanvas"/>
</Window>
The message is a false positive. The fix for the problem is available in the latest preview version of Visual Studio:
Microsoft Solution - Bhavya Udayashankar [MSFT] Closed - Fixed ยทยทยท
A fix for this issue has been released! Install the most recent preview release from https://visualstudio.microsoft.com/downloads/. Thank you for providing valuable feedback which has helped improve the product.
See issue "XLS1106 on virgin C# WPF .Net application" on the Visual Studio Developer Community site.
In the simplest applications without any data bindings set up, there is no need for a data context to be set, and the warning given can be safely ignored.
Note that you should NOT follow the advice of the comment above telling you to add DataContext = this; to the window's constructor for real-world applications. Write a proper view model object type, and create an instance of that to set as your DataContext reference.

WPF/XAML compiles but errors show in XAML

With a colleague, we try to make a POC of a WPF application. We created a new project with a page and a resx file to test internationalization (we are French). The application work well, no error on the compile and the resx file give good data.
but in the XAML file, we have a lot of errors.
Here is the XAML header:
<Window x:Class="MAPPrintProcessEditor.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:res="clr-namespace:MAPPrintProcessEditor.Properties"
mc:Ignorable="d"
Title="MAP Print Process Editor"
WindowStartupLocation="CenterScreen"
Height="{Binding SystemParameters.PrimaryScreenHeight}" Width="{Binding SystemParameters.PrimaryScreenWidth}">
Here, is a line marked in error:
<MenuItem Header="{x:Static res:Resources.MenuItemHeaderClose}">
<MenuItem.Icon>
<Image Source="pack://application:,,,/Resources/Close-16.png"/>
</MenuItem.Icon>
</MenuItem>
Visual Studio underline only this part:
Header="{x:Static
And finally, here is the error message:
The name "Resources" does not exist in the namespace "clr-namespace:PrintProcessEditor.Properties"
What is wrong in this code?
We use VS2017 and .Net 4.6.1.
If the you can build correctly than just ignore that message and use BLEND to design your app.

How to create a WPF form containing a web-browser tool that(web-browser) can adjust it's size to fit it's Window

I am new to C# and Visual Studio. I am using Visual Studio 2013 Express. I want to create a WPF application in C# that should has a web-browser in it. I want a web-browser in that form that can adjust it's size depending on the Window size.
I searched many times but I didn't found for what I'm looking for.
Note: I don't want a Window adjusting its size to fit its contents rather, I want a form containing(Web-browser) and it should adjust its size as Window of form stretched.
I guess you could use the WebBrowser control as the root element of the window and bind its Width and Height properties to the ActualWidth and ActualHeigth properties of the window:
<Window x:Class="WpfApplication1.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:WpfApplication4"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300"
x:Name="win">
<WebBrowser x:Name="wb" Source="http://google.com"
Width="{Binding ActualWidth, ElementName=win}"
Height="{Binding ActualHeight, ElementName=win}"/>
</Window>

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

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 ().

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