XmlParseException for a picture on a button - c#

I would like to insert a picture une a button, and, when i run my application, i have a XmlParseException.
I don't know why :(
<Button Grid.Column="2" HorizontalAlignment="Left" Height="20" Margin="15,2,0,0" VerticalAlignment="Top" Width="41">
<Button.Background>
<ImageBrush ImageSource="Images/search.png" Stretch="None"></ImageBrush>
</Button.Background>
</Button>
Anyone have an idea please ?

Make sure that your Visual Studio project contains an Images folder which in turn contains the image file search.png. Moreover the Build Action of the image file must be set to Resource.

You must check if your image has set Build Action property to Resource:

Related

How to show an embedded video inside of a window in C# WPF Application?

What I need :
In C# WPF on Visual studio 2019 in Windows 10, I want to show and play a video inside a My window from the app not directly path
What have I done :
I have included that video in my project, then I set the path for the video and it shows it to me in design view! :
but when I start my project , It won't show the video !
What have I tried and my code :
so I decided to try it in three-way, also to try to show a gif for sure about the path.
Embedded path
Normal Path
Direct path on windows
but only the direct path works! While the same embedded path works for GIF
MyGIF : Source="pack://application:,,,/Medias/1Correctmation.gif" => works fine
My Video : Source="pack://application:,,,/Medias/Video1.mp4" => not working
My Video : Source="C:\Users\user\Desktop\Video1.mp4" => workis fine but I don't want this directly path
also for my video file I tried these BuildAction:
Embedded
Resource
Content Resource
none of them work.
XAML:
<Grid>
<Label Content="Embeded Video :" HorizontalAlignment="Left" Margin="10,34,0,0" VerticalAlignment="Top" Width="100"/>
<MediaElement x:Name="Embd_Video1_mp4" Margin="115,10,499,309" Stretch="Fill" Source="pack://application:,,,/Medias/Video1.mp4"/>
<Label Content="Normal Path :" HorizontalAlignment="Left" Margin="10,172,0,0" VerticalAlignment="Top" Width="82"/>
<MediaElement x:Name="Normal_Video1_mp4" Margin="115,134,444,154" Stretch="Fill" Source="Medias/Video1.mp4"/>
<Label Content="Direct Path :" HorizontalAlignment="Left" Margin="10,345,0,0" VerticalAlignment="Top" Width="82"/>
<MediaElement x:Name="Dir_Video1_mp4" Margin="115,278,331,10" Stretch="Fill" Source="C:\Users\user\Desktop\Video1.mp4"/>
<Label Content="Embeded Gif Here ↓:" HorizontalAlignment="Left" Margin="452,34,0,0" VerticalAlignment="Top" Width="114"/>
<Image Margin="452,65,30,189" Source="pack://application:,,,/Medias/1Correctmation.gif" />
</Grid>
What should I do ?
Update:
I tried to check if has error in MedaFailed event , so check it out
Source="pack://siteoforigin:,,,/Medias/CorrectVideo.mp4" error says :
...
Source="pack://application:,,,/Medias/CorrectVideo.mp4" error says :
Source Code :
https://mega.nz/file/B1Zi2S6C#VtCk8xcdabekNIySc9-6C2yMNdV5z-qHoTCUnYl0CXA
is there any alternative for this issue ?

Cannot create an instance of UserControl in design view, works in Release mode and at runtime

I have a user control LoadingIndicatorControl which is displayed in various other controls when loading is happened.
This user control is very simple and looks like this:
<StackPanel HorizontalAlignment="Stretch"
Visibility="{Binding Loading, Converter={StaticResource Bool2Vis}}">
<Image x:Name="imgLoadingSpinner"
gif:ImageBehavior.AnimatedSource="pack://application:,,,/Images/Loading_Spinner.gif"
HorizontalAlignment="Right"
Width="30"
Height="30" />
</StackPanel>
I nest it in other views like this:
<uc:LoadingIndicatorControl Loading="{Binding Loading}"
DockPanel.Dock="Bottom"/>
When I run this application it compiles without issues and the loading indicator is displayed as I expect.
However at design time only I'm getting errors about this user control, related to the image resource. This gif is set to Build Action Resource.
In my host view I get this:
And if I look at the detail of why, it says Cannot locate resource 'images/loading_spinner.gif':
If I look at LoadingIndicatorControl in design view it does in fact locate the loading_spinner.gif resource and gives no errors.
What I have tried:
Clean and rebuild numerous times
Close VS2019 and reopen
Clean, close VS, reopen and rebuild
Set Loading_Spinner.gif Build Action to Embedded Resource (was Resource which also works at runtime)
Tried platform targets Any CPU, x64, x86
Interestingly, setting to build configuration to Release does prevent the error from appearing! It only appears when Debug is used.
I'm using Visual Studio Professional v16.11.16.
Any ideas how I can fix this?
Just use the direct path to the image:
<Image gif:ImageBehavior.AnimatedSource="Images/Loading_Spinner.gif"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="30"
Height="30" />
If you really need the pack URI, the format is explained here, it look like this:
<Image gif:ImageBehavior.AnimatedSource="pack://application:,,,/SO_72648958;component/Images/Loading_Spinner.gif"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="30"
Height="30" />
Working demo here.

Adding a custom font to a windows phone 8 app

I've read articles such as this 31 days of windows phone day 24 embedding-fonts and this windows 8 xaml tips custom fonts that explain very clearly and simply how to do this.
But for some reason I can't figure out, my custom font doesn't show on my app, whether in the designer view or when I run it.
To be sure I haven't left any step out, I'm showing what I did here for any suggestions on what my issue might be:
1. First, this is my xaml textblock code
<StackPanel Grid.Row="1" x:Name="BottomPanel" Height="120" Width="800" Orientation="Horizontal">
<StackPanel.Background>
<SolidColorBrush Color="{StaticResource PhoneSemitransparentColor}"/>
</StackPanel.Background>
<TextBlock x:Name="MainTextBlock" FontFamily="/Fonts/KOMIKAX_.ttf" TextWrapping="Wrap" Text="Hello" Width="680" Height="120" Margin="10,0,0,0" FontSize="25" Padding="20,10"/>
</StackPanel>
2. I created a folder in my project, called it "Fonts" and that is where I copied my font file.
3. I changed the Build Action of the font file to Content. I left the Copy To Output Directory property as None at first, then changed it to Copy If Newer. No success.
What have I done wrong or what did I leave out?
This work for me.
First make a folder in your App with the name Fonts, then Add font to it, then use the following syntax. I used it, it works perfectly.
<TextBlock FontFamily="/Fonts/Comic.ttf#Comic" TextWrapping="Wrap" Text="Hello" FontSize="25"/>
Make sure that the Build Action must be set to Content,
Right Click on font in folder and click property, then select Build Action as Content, otherwise it will not work on Emulator and Device.
Try using this
<TextBlock x:Name="MainTextBlock" FontFamily=".\Fonts\KOMIKAX_.ttf#Komika Axis" TextWrapping="Wrap" Text="Hello" Width="680" Height="120" Margin="10,0,0,0" FontSize="25" Padding="20,10"/>
In case this doesn't work, remember the format is .\FontPath\FontFileName.ttf#FontName

Image permission windows 8 apps

Hi
I'm trying to display an image in my app but I can't display any Image besides the images that were originally in the app like: logo.png, smallogo.png etc...
but when I choose another image it seems like it has no permission can anybody tel me why?
the following works:
<Image x:Name="imag1" Source="Assets/logo.png" Width="100" Height="300" Stretch="None" HorizontalAlignment="Left"/>
and the folowing doesn't:
<Image x:Name="imag1" Source="Assets/camel.png" Width="100" Height="300" Stretch="None" HorizontalAlignment="Left"/>
needless to say that camel.png is in Assets folder
Make sure you also have "Camel.png" added to your project. If you copy it in the Assets folder and is not added to your project, it will not work. To verify this, in the solution explorer, open the Assets folder from the folder tree view, and if it's not there, right click on the folder, Add Existing Item and add your image.
hello the problem is about the properties you should have set for camel.png.so properties should be set in properties windows to Content and Copyalways..hope it help you..

Why do my images show at designtime and not at runtime?

I have a WPF <Image> that I am trying to display on a <Button> inside of a <Toolbar> with the following code.
<ToolBarTray>
<ToolBar Height="26" Name="toolBar1" VerticalAlignment="Top" >
<Button Name="StampButton" Click="StampButton_Click">
<Image Source="/MyApp.Resources;component/Resources/MyImage.png"
Height="16" Width="16" Stretch="Fill"/>
</Button>
</ToolBar>
</ToolBarTray>
The image shows up just fine at design time. However, at runtime nothing is displayed. The resources are in another dll called MyApp.Resources. The button is actually created just fine and the click event works fine also.
Set your image build action to "Resource". Try to use full source path.
I was getting same problem - image displaying at design time, but not at runtime.
I had build action set to resource, tried different pack uris etc etc, but solution was simple
<Window.Resources>
<BitmapImage x:Key="your_image_key" UriSource="your_image.png" />
<Image Source="{StaticResource your_image_key}"/>
</Window.Resources>
This is what worked for me, from a site called wpf-tutorial.com:
<Image Source="/WpfTutorialSamples;component/Images/copy.png" />
Images is a subfolder of the project folder in my case, and I assume also in this sample.

Categories

Resources