Build action for resource dictionary in UWP - c#

What should the build action for a resource dictionary? In WPF it's Resource build action for ResourceDictionary file, but in UWP there no such a build action, and googling didn't help so far.
I tried several options like Embedded Resource and so on but all of them just cause different exceptions.
The project works without using the ResourceDictionary file and there is just one simple style in it.

For ResourceDictionary the setup should look as in this screenshot:
So the Build Action is Page and the Custom Tool should be XamlIntelliSenseFileGenerator.
If you are still getting an exception, ensure that the XAML code is valid or alternatively right-click a folder in your project, select Add > Add new item..., select XAML category on the left and then Resource Dictionary template and let Visual Studio create the correct file for you.

Related

UWP Community Toolkit with source code

"UWP Community Toolkit" is good tool.
https://github.com/Microsoft/UWPCommunityToolkit/tree/master
but If we installed it with NuGet,
We can not access source code ( I thought ).
Now, I only want to use "RadialGauge" only in this toolkit.
and I want to modify "RadialGauge" a bit..
then, I started to use RadialGauge with source code without NuGet install.
here is my step.
I open New UWP project with C# VS 2017.
I installed this code to my "Controls" folder. UWP RadialGauge Source
and I make this project.
but No display, Just white UWP windows is opened... Why ?
I need help... with my poor UWP knowledge, to resolve is not possible...
Here is project file.
https://www.dropbox.com/s/alvw6wcbiv5itsp/RadialGaugeTest.zip?dl=0
When you define a custom control, most of the time you want to give it a default style. This style needs to be placed inside a ResourceDictionary called Generic.xaml which then needs to be inside a folder called Themes.
In your case, you have only copied the code part over. To make the control show up on your page, all you need to do is go grab its default style from here, put it in a Generic.xaml resource dictionary, and then put the dictionary into a folder called Themes.

Visual Studio: cannot access resources from code

I added several files (HTML, CSS, etc.) as resources to my Visual Studio project. According to MSDN, I should be able to access these resources via
MyProjectName.Properties.Resources.Filename
Unfortunately, IntelliSense doesn't even know the Properties property behind MyProjectName. If I type "MyProjectName.", IntelliSense offers me several things but no properties. I already built the project, but nothing changed.
If this is important: that project is not the main/startup project of my Visual Studio solution.
You need to add them here:
Project menu
MyProjectName Properties
Resources tab
"Click here to create resources" (if necessary)
Select Files menu
Add Resource
Now you can access them using MyProjectName.Properties.Resources.Filename
Make the file as a resource from its properties as:
BuildAction=Compile
Take a look at this MSDN Forum link. What I take from it is make sure your resources are added as resources and that the namespace's match.
Are you sure they're marked as resources? Right click and check their properties.
It is a little confusing that there is a folder in Visual Studio called Propertieson the same level as Resourcesbut which is bypassed altogether.
So while the path in your code may go via Solution_name.Properties.Resources.yourFile.png this does not reflect in the folders used in Solution Explorer of Visual Studio, as there you find the image then directly under Solution_name\Resources\yourFile.png.
And just adding a file and moving the resource with the mouse won't make Visual Studio find this file as Resource in your code. So you must go through menu Project, Properties (lowest entry) ? or click Alt + F7, Highlight Resources.
Now click on the Drill-down-Arrow of Add Resource rather than just on the main area of the button. Doing that would open the dialog window for Add New Resource, but actually you want to click Add Existing File. That's it.
I know this is contrary what answer said but on VS2013, I had to set BuildAction=Resource
For anyone else perhaps check the Resources.Designer file?
I found a discrepancy on this line
System.Resources.ResourceManager("DemoApp.Properties.Resources", ...
I had changed the project name to Demo.
I am not sure will it help or not but all I had to do is creating a new form and adding an icon to picturebox. Somehow that solved the problem.

How to build library with WPF forms

Is it possible to build a Class Library dll which also includes WPF forms?
When I try to build one I get following errors:
Error 1 Library project file cannot specify ApplicationDefinition element.
Error 2 The project file contains a property value that is not valid.
If I set my project as Windows Application it compiles & runs tho. Is there a way to get arround this? I'm using VS2010 & C# .NET 4.0 if that might be of any interest to you guys.
Thanks
Choose WPF Custom Control Library or WPF User Control Library when creating/adding new project to your solution.
Change the build action of your UserControl from ApplicationDefinition to Page.
Remove the App.xaml and App.xaml.cs in your WPF Project. Then you will be able to run the project with an output type of Class Library.
This is also occur when we copy paste an Image to ClassLibrary Project.
Change the Build Action Property of that image to resources
Extending #2493490 answer.
Right click on the app.xaml file and exclude it from the project.
Change the project output type to in project properties to Class Library.
Click on the run icon, the solution will successfully build but will not run/execute any code. A error message will be displayed explaining as such.
problems after copying XAML controls from WPF Application to class library
The first answer work for me.
I changed App.xaml from ApplicationDefinition to Page and worked

Does XAML work with file links in Visual Studio?

I'm adding a new WPF project to an existing Visual Studio solution and would like to reuse a bunch of code (C# and xaml) from an existing project within the solution.
I've created the new project and added existing files as follows:
Right click project
Add -> Add Existing Item
Find the file to reuse, use the arrow next to "Add" and "Add as Link"
I now have a nice project set up with all the proper links. However, XAML chokes on these links. For example:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="Resources\Elements\Buttons\Buttons.xaml" />
<ResourceDictionary
Source="Resources\Elements\TextBox\TextBox.xaml" />
</ResourceDictionary.MergedDictionaries>
The files "Buttons.xaml" and "TextBox.xaml" exist as links in my new project. The project builds, but when I run, I get the following XamlParseException:
'Resources\Elements\Buttons\Buttons.xaml'
value cannot be assigned to property
'Source' of object
'System.Windows.ResourceDictionary'.
Cannot locate resource
'resources/elements/buttons/buttons.xaml'.
It seems like the XAML parser is requiring an actual copy of these XAML files to exist in my new project, instead of links.
This is exactly what I'm trying to avoid. I want my project to share these files so that any changes get transferred to the other project without hunting and copying.
Any insight is appreciated!
Linking to an external XAML file does not create a file where the link exists in a project structure, as you have noticed.
My advice is to use relative links in the MergedDictionaries references. If the XAML to reuse in another project is called Common, the Source property of the first nested ResourceDictionary could be:
..\Common\Resources\Elements\TextBox\TextBox.xaml
which is actually the path that you used to add the existing item.

adding images in my project

I'm trying to add/change images in my project, using Microsoft Visual Studio 2008 C#. Along with it, Devexpress components are also included.
What I did, I copied an image (.png file) and paste it it in my "PrintRibbonControllerResources.resx" and then after that I have to open again the MainForm.cs right click on the form and click on the Run Designer. It will open the Ribbon Control Designer. From there, I can add the image.
Do you think its ok? It's my first time to do this, and I don't have any experience & I'm learning it by doing.
Thanks
===
it seems I don't see the add function under resource tab
I generally add an image to the project itself. (Add/Existing item) I do this so I can use SourceControl to check in/out the image file. The resx then links to these files.
Basically, my philosophy is: if it works, don't fix it.
If this solution works for you, it's fine. At least, until you discover a situation where it fails.
If you go in the properties of your project, and select the resources tab, you can add it directly from there and it will be accessible in the default resource file of your project, which might be more convenient.
I like to add it to a solution folder (or folder in the project) and like that to the resx via "Add Exising File".
I get all of the advantages of having the resource file, but also the advantages of having a physical file (editing, quick view, etc.)

Categories

Resources