RibbonSplitButton cannot locate resource - wpf

I have a RibbonSplitButton that contains a SmallImageSource like so:
<UserControl x:Class="myProgram/toolbars/MainToolbar">
<RibbonSplitButton SmallImageSource="/Images/Undo.png"/>
<!-- more buttons-->
</UserControl>
I try to use this in another file (main):
<UserControl x:Class="myProgram/main"
xmlns:toolbar="clr-namespace:myProgram.toolbars">
<toolbar:MainToolbar/>
</UserControl>
I get the error message in main:
Cannot locate resource 'images/undo.png'
However, when I switch to a normal button:
<Button>
<Image Style="{StaticResource buttonstyle}">
<Image.Source>
<BitmapImage DecodePixelWidth="40" UriSource="/Images/Undo.png"/>
</Image.Source>
</Image>
</Button>
the error is gone. I have tried using Pack URIs, changing the Build Action to Resource and Embedded Resource, as well as Clean Soultion, Rebuild Solution, Restarting Visual Studios (rinse and repeat). I image this has something to do with the RibbonSplitButton's image source, but I have no idea. The program runs fine (the Undo.png image shows and works perfectly), it's just annoying to have the error in my developer. Anyone have suggests on how to get rid of the error? (Note, this is a simplified folder structure, for example purposes).
EDIT
For anyone who ends up here at some point in the future, I found that this answer worked.

Related

How to set Startup directory to a Image in xaml

Sorry for bad describe in the title. I have a image in "C:\Users\aUser\Desktop\Program\Image\Image.png"
But my program is in the same folder with the Image. I can set the Directory manually <Image Source="C:\Users\aUser\Desktop\Program\Image.png" But when the parent directory is moved, the code will no longer work. So how can I set the Source of the image that in the child folder without use of the code behind
Image.Source = new BitmapSource(new Uri(AppDomain.CurrentDomain.BaseDirectory + #"\Image\Image.png")
You will need to be more specific what XAML 'version' you are using (e.g. WPF, UWP or Xamarin.Forms). Anyways, here goes:
UWP
You should read through these docs. Your XAML code could look like this:
<Image Source="ms-appx:///Assets/Image.png"/>
Where Assets/Image.png is a path to your image
Xamarin.Forms
Have a look at these docs. The solution depends on the platform
WPF
Have a look at these docs. Your XAML code could look like this:
<Image>
<Image.Source>
<BitmapImage UriSource="/Images/image.png" />
</Image.Source>
</Image>

Using app-level resources in the Generic.xaml file of a custom control library

I have a button on a custom control and I'm trying to display an image on it which is defined as a resource in my App.xaml file like so:
<Application.Resources>
<BitmapImage x:Key="PlusSymbol" UriSource="Resources/PlusSymbol.png" />
</Application.Resources>
For some reason I can't use this as a static resource within my custom control's template defined in the Themes\Generic.xaml file, it crashes my application during runtime saying that it cannot find the requested resource. Here's the xaml code I'm using to bind to the resource:
<Button Grid.Row="0" Grid.Column="1" Margin="3">
<Image Source="{StaticResource PlusSymbol}"/>
</Button>
It DOES work during runtime if I define the resource the exact same way but within the Generic.xaml file, however it gives me a pre-compiler warning that it can't find the file since it's now looking for it in Themes/Resources/ rather than just in /Resources/. So I get a warning during design time but it works fine in runtime.
So why can't it find my resource when it's defined in App.xaml? I do this the exact same way in a regular WPF project and it works flawlessly, but in this custom control library it is giving me headaches. Any help is much appreciated!
This should work if you switch your StaticResource to DynamicResource so that the resource will be evaluated dynamically at runtime rather than statically. If you switch to DynamicResource.
<Button Grid.Row="0" Grid.Column="1" Margin="3">
<Image Source="{DynamicResource PlusSymbol}"/>
</Button>
I believe this is because of how theme-based styles and templates are handled, as opposed to standard resources. This answer and this answer speak specifically to Generic.xaml and how it is different from other ResourceDictionaries.
So the way I ended up getting this to work was by defining the BitmapImage in the Generic.xaml file and using a Pack URI to get to the file (here's the MSDN article about Pack URIs, which frankly just confused me). This way it's using a relative path to the file, and specifying the assembly that it's coming from (the file is located at \Resources\PlusSymbol.png in the MyCustomControlLibrary project and has a build action of Resource):
<BitmapImage x:Key="PlusSymbol" UriSource="pack://application:,,,/MyCustomControlLibrary;component/Resources/PlusSymbol.png" />
Then in the control template in Generic.xaml I use the resource like so:
<Button Grid.Row="0" Grid.Column="1" Margin="3" Height="25" Width="25"
<Image Source="{StaticResource PlusSymbol}"/>
</Button>
Note that I got fooled thinking that I could use the shorter version of the Pack URI like so:
<BitmapImage x:Key="PlusSymbol" UriSource="pack://application:,,,/Resources/PlusSymbol.png" />
However this was still causing the program to crash at runtime. I think the fact that this is a custom control library, and that the end consumer of the image is my UI project, makes the longer version which specifies the assembly that actually contains the image necessary.
Hope that helps anyone else who is having similar problems.
Note I don't think this technically answers my original question which specified using app-level resources (aka defined in the custom control library's App.xaml file) from Generic.xaml. Defining the PlusSymbol resource there still crashes the program. However I didn't actually care about doing it in App.xaml, I was just trying to get it to work right at both design time and run time. This solution does the trick for me, is fairly simple, and from what I can tell from my research is the best practice.

How to debug DependencyProperty.Unsetvalue errors when the InnerException is null

I have a custom login usercontrol that has a normal textbox and a passwordbox.
Blend refuses to cooperate with the PasswordBox saying that "DP.UnsetValue" is not valid for PasswordChar property. However the project compiles and runs fine in Blend or in VS2010. The cider designer in VS2010 doesn't seem to mind the error because it actually renders my UserControl for design time configuration.
Normally when I get one of these errors there is an InnerException with a path to the file/resource missing. That's not the case here and I'm not sure how to figure out how to fix it for when this comes up in the future.
I swapped the tags to turn the PasswordBox into a normal TextBox and it seems to be fine with that. However I need the input masking that PasswordBox provides. It's not very practical to comment out that object and finish styling my control in Blend.
Here's my Xaml:
<PasswordBox x:Name="PasswordTextbox" PasswordChar="*" Height="26" VerticalAlignment="Center" Grid.Column="1" Grid.Row="1" Margin="5" RenderTransformOrigin="0.5,0.5" TabIndex="3">
<PasswordBox.RenderTransform>
<TransformGroup>
<ScaleTransform/>
</TransformGroup>
</PasswordBox.RenderTransform>
<PasswordBox.Effect>
<DropShadowEffect />
</PasswordBox.Effect>
<PasswordBox.Triggers>
<EventTrigger RoutedEvent="UIElement.GotFocus">
<BeginStoryboard Storyboard="{StaticResource StoryboardScaleUpFontIncrease}"/>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.LostFocus">
<BeginStoryboard Storyboard="{DynamicResource StoryboardScaleNormalFontNormal}"/>
</EventTrigger>
</PasswordBox.Triggers>
</PasswordBox>
Does anyone know how to debug this behavior?
I also had the same issue in xaml designer but in a sightly different way. I add the detail here in case it is helpful to someone.
I was using a 3rd party control which has their own theme defined in a dll. I referenced this dll and use 'StaticResource' to reference some resources defined in that dll. Then the xaml designer underlined my xaml code saying something like 'D.Unset is not a valid value for xxx'. But the program compile and run without any problem.
To eliminate these annoying underline, the solution is simple: Change 'StaticResource' to 'DynamicResource'.
Another more complicated way is to use 'ResourceDictionary.MergedDictionaries' to include resources in that dll into your xaml file.
I was finally able to solve this issue by using the techniques described on this blog entry:
http://web.archive.org/web/20090602111317/http://bea.stollnitz.com/blog/?p=52
I advice to recheck (twice or more) your ResourceDictionary. I came to this error after I added to my ResourceDictionary:
<Style TargetType="Label" x:Key="myLabel">
<Setter Property="Foreground" Value="{StaticResource myDefinedColor}"/>
...
</Style>
while {StaticResource myDefinedColor} was defined BELOW it. In my case error was about Foreground that has UnsetValue. So actually it really was an unset value...
I moved my style for Label below defining my color and that was it!
Just wanted to add that this was helpful in solving an issue for getting a "DependencyProperty.UnsetValue is not a valid value for property" error solved, in particular the first part of the blog suggesting that your resource might not be valid (i.e. previously defined and available for use). Turns out in my case it was the classic issue of not defining my resources in the right scope and before they are actually called upon.
My advice: First look to make sure you define the StaticResource in the appropriate Element.Resources section. Make sure that definition is physically located before your actual call to the resource.
Seems an old post. I solved my issue by putting the stuff in order of use in resourceDictionnary merging. In the example below, I called the resource 'Couleur.xaml' later and i had the error message. So I put it in first place and it solved my problem:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/TS;component/Ressources/Couleur.xaml"/>
<ResourceDictionary Source="/TS;component/Ressources/ButtonStyle.xaml"/>
<ResourceDictionary Source="/TS;component/Ressources/DataGridStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
Hope it's gonna help somebody out there!

Pack URI and path not resolving image in WPF

I have the following directory structure
Project
\Images
+view.png
control.xaml
and in the control I have a button defined by the following XAML:
<Button Click="Search"
Grid.Column="1"
Margin="0,5,5, 0"
HorizontalAlignment="Right">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Image Source="pack://application:,,,/images/view.png"
Width="16"
Height="16"
ToolTip="Search"
Cursor="Hand"
Opacity="0.8" />
</ControlTemplate>
</Button.Template>
</Button>
However, neither this pack URI method nor the "/images/view.png" is working. As I understand it, this is the same issue this question raises. However, I get the same error. The confusing thing is that in designer in Visual Studio 2008, the image renders correctly, but on the call to the InitializeComponent() call, I get:
Cannot convert string 'pack://application:,,,/images/view.png' in attribute 'Source' to object of type 'System.Windows.Media.ImageSource'. Cannot locate resource 'images/view.png'. Error at object 'System.Windows.Controls.ControlTemplate' in markup file 'RecapSpecEditControl;component/modaltreadgroupdatadialog.xaml' Line 61 Position 40.
I thought that maybe there was a namespace that I had to declare but according to the msdn site I believe I don't have to do anything like that.
I actually got this to work, but had to set my source to "/ProjectName;component/images/view.png" Because I have the ProjectName as a referenced assembly this is then the same as the Path: portion at the msdn page that I referenced in the question.
Set the Build Action for 'view.png' to Resource instead of Content and this problem should go away. I was able to reproduce your problem this way and it works correctly when set as a Resource.
Xaml.VB
Call the Image from Application folder and Design Page
Private Sub LoadImages()
Dim strUri As String
strUri = AppDomain.CurrentDomain.BaseDirectory() & "\NavigationImages\settingsicon.png"
Image2.Source = New BitmapImage(New Uri(strUri))
End Sub
Page load in Xaml.VB
Call LoadImages()
Xaml Design Page
Image Name="Image2"Height="32" HorizontalAlignment="Left"

What to do when WPF Window doesn't recognize user control defined in the same project?

I have a window "Operation" in XAML that uses a user control "Status" defined in the same project. When I build the solution, it returns ok, but when I open the design view for the Window, visual studio says "Could not create an instance of type 'Status'. The XAML for the Window "Operation" is below:
<Window x:Class="TCI.Indexer.UI.Operacao"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tci="clr-namespace:TCI.Indexer.UI.Controles"
Title=" "
MinHeight="550"
MinWidth="675"
Loaded="Load"
ResizeMode="NoResize"
WindowStyle="None"
WindowStartupLocation="CenterScreen"
WindowState="Maximized"
Focusable="True">
<Canvas Name="canv">
<tci:Status x:Name="ucStatus"/>
<Grid Canvas.Top="0" Canvas.Left="0">
<StackPanel Orientation="Horizontal">
<!-- Indices -->
<Label Width="200"/>
</StackPanel>
</Grid>
</Canvas>
</Window>
The xmlns:tci is the namespace where the Status Usercontrol is. And tci:Status becomes underlined in blue when this error happens. How to use a UserControl like this?
I was consistently having this problem with one control in my project and wound up resolving it by changing all of my images to DynamicResource rather than StaticResource.
The really strange thing is that the designer worked just fine for the control itself and showed the images as expected. It was only when I used the control inside my main window that the designer gave me the "Could not create instance of " message.
I don't know exactly whats the solution, but that happens to me too from time to time. I end up deleting declaration of the namespace, rebuilding and trying again. :\
Try cleaning your entire project, then do a full rebuild. The WPF designer in VS is poorly done, in my opinion, and there are weird issues like this all over the place.
I recommend not relying on Design View for anything, at this point - it's just too unstable. Try Expression Blend, it's a little bit better with things like this. If you don't want to go that route, you're probably better off building and running your app :-(
If you're running VS 2008, do you have SP1 installed?
I typically see this when I haven't built the control. Make sure you build the control and see if you still see the problem. Occasionally, VS just gets confused and you need to close and open the control you are having a problem with. In this case, that would be your window.
Similar to what Jon Norton said, I also found this link (-link removed, see below-) that implicates resources. I had the situation described in the link, and Jon's fix sorted it.
EDIT
Sorry, the link now requires a login and the page doesn't exist anymore. I couldn't find what it was supposed to be after three years.

Resources