How to insert a text file as a XAML application resource in UWP Desktop app? - c#

I have several image files defined in the <Application.Resources> section of the App.xaml file. Is it possible to do the same with text files? If yes, how to do it?
<Application.Resources>
<Image x:Key="Img_DefaultCover" Source="/Images/defaultCover.png" />
</Application.Resources>

Related

WinUI XAML: Using a ResourceDictionary from another project

In my WinUI 3 application, I am trying to use a ResourceDictionary that is located in another project.
Lets say the referenced project is ResourceTestLib and this library project has a folder "Styles" which has a file "_Thickness.xaml".
In the app.xaml file of the main application, I tried these two approaches below the "Other merged dictionaries here" comment, but none of them seem to work, i.e. that app crashes on startup with that message "Cannot locate resource ...." message.
This is my app.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<!-- Other merged dictionaries here -->
<ResourceDictionary Source="/ResourceTestLib;component/Styles/_Thickness.xaml"/>
<ResourceDictionary Source="pack://application:,,,/ResourceTestLib;component/Styles/_Thickness.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
When I hover over the respective line, Visual Studio shows a tooltip with "Path X not found" for the first referenced ResourceDictionary and a "Invalid characters in path" (probably because of "application:,,,") message for the second.
I suppose that that WinUI XAML might be different than WPF XAML in that respect? Or even that this is not supported yet?
Pack URIs are only used in WPF.
UWP and Win UI use the ms-appx URI scheme to refer to a file that resides in another assembly so try this:
<ResourceDictionary Source="ms-appx:///ResourceTestLib/Styles/_Thickness.xaml" />
More on UWP style URI schemes:
https://learn.microsoft.com/en-us/windows/uwp/app-resources/uri-schemes

How can I make all Resources in one place

I work on C# WPF
I have more than one DataGridView all of them has the same style (Background, row width, column height...etc) but each one has different numbers of columns
So my question is Can I make one style for these grids and share between them?
like css in Web
You can create a resource file in wpf where you can store all your style and template. Don't confuse it with the resource file in the Properties directory. You can add a Resource file by right clicking your project in visual studio and adding an existing xaml file or creating a new one.
After that, define all your styles and templates. Then you only need to include it in your available resources
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="UsingResourceDictionaries.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="myDirectory/myDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Don't forget to give a key to each one of your style and template, so that you can use them in your User controls

How to add ResourceDictionary in Phone class library project and access it

I am working on a project where i have child project which is referencing the library project.
In my Library project(Phone class library) how do i create ResourceDictionary.xaml where i need to add some styles and use it in xaml files and as well as .cs files.
I need to access styles in ResourceDictionary.xaml in my xaml files as well as .cs files how to do it ?
Create a folder as Generic in the root folder and have your resource files there...
To access it in XAML
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
or
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/AssembleName;component/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
To access it in .cs
new URI(pack://application:,,,/AssembleName;component/Generic.xaml)
OK, Finally i solved it myself. here is how i solved it.
When you create Phone Class Library Project you will not the ResourceDictionary.xaml by default, so what create a xaml page,like how you create a normal xaml file.
Right click on solution ->Add New Item ->Windows Phone Portrait Page.
Now remove the .cs file that is created, add in the .XAML file add your style which is within ResourceDictionary tag.
The above is all done in Library project.
Now in the child app:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="LibraryResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
suppose you have a style defined in ResourceDictionary as MainScreenButtonStyle you can set in this way
button.Style = (Style)Application.Current.Resources["MainScreenButtonStyle"] as Style;

How do I use a .resx file in a class library inside a WPF control?

So I have got a library that hosts all my images. This works fine for my WinForms controls; however, I am having difficult using this same class library's resources in WPF.
MyApp.Resources //Is my control library project.
Resources //Is the folder that contains all my images at the root of the project.
How do I reference an image inside a WPF control from my class library?
What I have tried:
<ImageBrush ImageSource="/MyApp.Resources;Resources/manage.png" />
--UserControl Property
Resources="pack://application:,,,/MyApp.Resources;Resources.resx">
Add a new RESX file.
In this file add ur files.
Check ur RESX file in Solution Explorer in VS and change the build action from Embedded Resource into Resource.
Open RESX file and change the access modifier from Internal into Public.
Create a new Resource Dictionary file (eg. Files.xaml):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<BitmapImage x:Key="someImage"
UriSource="/YourAppNamespace;component/Resources/image.png" />
</ResourceDictionary>
Import the dictionary in your window or app resources:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Files.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Use ur image in WPF app:
<Image Source="{StaticResource someImage}" />

Xaml: C# How to keep style consistent

I'm working on a semi-large windows application using wpf and C# in VS 2010. While working on the xaml, I added a tag so that all buttons and datagrids are styled in the same way. I've copied and pasted this block into several of my .xaml files and that works fine. Of course the problem I'm running into now is that I've added to and changed the style several times.
What is the best way to keep style consistent between my different Windows? Is it subclassing, using Resources.resx, or another way enirely?
If you define the style in the Application level ResourceDictionary (App.xaml), then it will automatically be inherited by your other XAML Windows/Controls.
yeah, if you were to create a new file called Resources.xaml and then add this to your Application.xaml file:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
then you should be able to reference the styles in the Resources.xaml from all the windows in your application.

Categories