XamlParseException in XAML - wpf

I'm trying to display some text from Resources in a TextBox. In my Properties.Resources, I have a string resource named DefaultUrl.
I'm using the following code to display it in the TextBox:
<Window x:Class="........"
...
...
xmlns:properties="clr-namespace:project_name.Properties">
<TextBox Text="{x:Static properties:Resources.DefaultUrl}"/>
</Window>
On building the project, it correctly displays the resource value in the TextBox, but on running the project, it gives me the following error:
Exception thrown: 'System.ArgumentException' in System.Xaml.dll
Exception thrown: 'System.Xaml.XamlObjectWriterException' in System.Xaml.dll
Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
'Provide value on 'System.Windows.Markup.StaticExtension' threw an exception.' Line number '725' and line position '149'.
How can I display Properties.Resources string in XAML ??

Solution:
Going with #Max and #Styx comments, I was able to resolve this issue by changing the access modifier dropdown ( in Project resources ) value from Internal to Public

Related

WPF Behaviors no longer working with newest NuGets

The app used to work fine, but a few weeks ago we updated hundreds of NuGet packages. We recently discovered that any reference to a WPF Behavior gives the following error:
System.Windows.Markup.XamlParseException: ''Add value to collection of
type 'Microsoft.Xaml.Behaviors.BehaviorCollection' threw an
exception.' Line number '78' and line position '86'.'
Inner Exception ArgumentException: Cannot add instance of type
'TextBoxInputBehavior' to a collection of type 'BehaviorCollection'.
Only items of type 'T' are allowed.
I suspect this is because of a NuGet update, but I don't know which one had caused the error, or which need to be changed. I'm using the latest version for Microsoft.Xaml.Behaviors.Wpf (1.1.31).
I would appreciate advice. Thanks.
Code:
xmlns:Behaviors="clr-namespace:MyCompany.MyProject"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
<TextBox Text="...">
<i:Interaction.Behaviors>
<Behaviors:TextBoxInputBehavior InputMode="DecimalInput" JustPositiveDecimalInput="False" RangeMin="-1000000.000" RangeMax="1000000.000" />
</i:Interaction.Behaviors>
</TextBox>
//...
using Microsoft.Xaml.Behaviors;
//...
public class TextBoxInputBehavior : Behavior<TextBox>
//...

Odd InvalidCastException

I'm getting an odd invalid cast exception, which I suspect is due to mismatched namespaces, but I'm not sure where.
The exception message is:
Additional information: Unable to cast object of type 'FishTracker.FishTracker' to type 'FishTracker.Application'.
Now this has come about because I decided to rebuild an application from the ground up having spotted a crucial flaw. It's a wpf app.
The main xaml reads like this:
<Application x:Class="FishTracker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FishTracker"
StartupUri="Views\MainRibbonView.xaml">
<Application.Resources>
</Application.Resources>
In the vb file behind that is defined thus:
Public Partial Class FishTracker
Inherits Windows.Application
and in the application settings the Assembley name is FishTracker and the RootNamespace is FishTracker.
As far as I can see everything matches, it builds yet fails when it runs with the invalidcast exception thrown from MyWpfExtension.vb (and I can't even see that in the solution) .
Any ideas most welcome.

Using a style resource from one project in another project

In one project, I have a resource dictioary and a usercontrol. A style is defined in the resource dictionary and the usercontrol uses the style in resource dictionary via staticresource. When another project tries to use this usercontrol, we get an exception 'Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.' Is there any way we can solve this?

ImageBrush throws exception when trying to set Window background

I'm trying to set the background of my WPF window to an image but I'm getting this exception when I try to run it:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll Additional information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '8' and line position '10'.
I don't want to add the image to the project, as I would like to be able to change the image at runtime. My intention is to use databinding to set the background picture during start-up once I have this bit working.
Source Code:
<Window x:Class="ColinsTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Colin's Test Application"
WindowStyle="None"
WindowState="Maximized">
<Window.Background>
<ImageBrush
ImageSource="DeLaRue-Background.jpg"/>
</Window.Background>
<Grid></Grid>
</Window>
Any Ideas? Thanks
Wrong. It should be set to CONTENT not Resource.
Include in your project (use the file explorer)
Right click on the image > Properties > Advanced.
Build Action: Content
Copy to Output Directory: Copy always.
Your image's build action is probably set to Content, change it to Resource
Set the item (your image file) to be a Resource. (In Visual Studio) Right click,Properties,Build Action,Resource
I had the same problem with
<Button>
<Button.Background>
<ImageBrush ImageSource="/Resources/icon_done.png">
</Button.Background>
</Button>
I used / before Resources and it work correctly, but i used solution with properties image ->Properties->Build to Resources.
These two actions solved my issue.
Add the forward "/" to the location
Then the whole ImageSource string should look like
<ImageBrush ImageSource="/Resources/Image1.png" Stretch="None" />

More Issues Opening Window WPF

I'm using VB.net and WPF 4.
I cannot seem to open a new window in WPF consistantly. It will work one time, and then the next time I execute the same script, it throws the following exception:
XAMLParseException occured 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '4' and line position '208'. {Cannot evaluate expression because the code of the current method is optimized.}
Here is the script:
Dim Window As Window = New GAME_WINDOW
Window.Show()
Yes, "GAME_WINDOW" is a legitimate WPF window. Here are the first four lines of XAML code for the window I'm trying to open:
<Window x:Class="GAME_WINDOW"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Operation SpyRat: Word4Word" Icon="/VBP-WORD4WORD;component/alicia.ico" WindowState="Maximized" Background="Black" xmlns:my="clr-namespace:VBP_WORD4WORD">
I suspect the problem is the window Icon specification in the XAML. Try removing the Icon="..." from your XAML, and see if it corrects the issue.
If so, you'll need to make sure this is specified in proper Pack URI format. Most likely, this would be:
Icon="pack://application:,,,/component/alicia.ico"

Resources