How to enable configSource attribute for Custom Configuration Section in .NET? - c#

following the wealth of information found here how can we get an external .config to work? I've tried the same setup I would use for an external appSettings file, but it is unable to find the file for my custom section.
<configSections>
...
<section name="CustomSettings" type="Fully.Qualified.TypeName.CustomSettings, AssemblyName" />
</configSections>
<!-- this works -->
<CustomSettings attrib1="val1" attrib2="val2" .../>
however...
<!--this does not work-->
<CustomSettings configSource="someExternalFile.config"/>
where someExternalFile.config would contain
<CustomSettings attrib1="val1" attrib2="val2" .../>
any ideas?

The actual file, must be placed relative to the project output folder (by default "\bin\debug" or "bin\Release"
Also, The file in your project tree, look at the properties of the file, and make sure the "Copy to Output Directory" setting is set to "Copy Always" or "Copy if Newer"
EDIT: make sure the separate config file has an xml Element header. The entire file contents should read as follows:
<?xml version="1.0" encoding="utf-8" ?>
<CustomSettings attrib1="val1" attrib2="val2" .../>

Related

Where can I find ToolbarPlacement attribute of TabbedPage?

I'm trying to locate the Tabbing bar in the bottom of the screen in android and I already did in the C# code behind but I'm just wondering how to do that in the .xaml code?
I've added the Xamarin.Forms.PlatformConfiguration.AndroidSpecific namespace but can't find the ToolbarPlacement attribute to set it to "Bottom" like is shown in the image below.
So is there a way to set it in the xaml code?
Toolbar Placement
It is possible to get these tabs at the bottom with a specific platform:
On XAML code :
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:TabbedPage.ToolbarPlacement="Bottom" >
The blue line indicates that ToolbarPlacement or others cannot be found. It is a problem of intelligisense because they are not properties and are real constructive arguments. don't worry, they work great!
Or in C# code behind:
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
On<Xamarin.Forms.PlatformConfiguration.Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
you can also use these properties to customize the bar at the bottom
BarBackgroundColor="#2196F3"
android:TabbedPage.BarItemColor="Wihte"
android:TabbedPage.BarSelectedItemColor="#66FFFFFF"
BarTextColor="White"
From what I see your Xamarin.Forms package is not up to date because of which you are unable to use that feature. Just update the Xamarin.Forms package to 3+ and I think that should solve your issue.
Feel free to revert in case of queries
Update:
Try adding the following lines manually and try if it works:
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:TabbedPage.ToolbarPlacement="Bottom"
Customizations is changed:
From:
android:TabbedPage.BarItemColor="Wihte"
android:TabbedPage.BarSelectedItemColor="#66FFFFFF"
To:
SelectedTabColor="White"
UnselectedTabColor="#66FFFFFF"

Xamarin - Type not found in xmlns clr-namespace

I am making a Xamarin Forms app, the solution is called RESTTest, my shared project is called RestApp.
In my shared project I have a folder called ViewModels, which contains a class called MainViewModel.cs
I have a page called MainPage.xaml which has a code-behind called MainPage.xaml.cs. In my XAML I am trying to include my Viewmodels folder like this:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:RestApp"
x:Class="RestApp.MainPage"
xmlns:ViewModels="clr-namespace:RestApp.ViewModels;assembly=RestApp">
But when I add binding to my page like this:
<ContentPage.BindingContext>
<ViewModels:MainViewModel />
</ContentPage.BindingContext>
I am getting an unhandled exception:
Type ViewModels:MainViewModel not found in xmlns clr-namespace:RestApp.ViewModels;assembly=RestApp
What am I missing?
Removing the ";assembly=RestApp" in the namespace, and setting the linker behaviour to "Link SDK Assemblies Only" worked for me and solved the problem!
This usually happens when you have Linker optimizations that trim unused code.
Note that deserialization into a type may not be detectable by the linker as a type use.
In the properties of your project find the "Linker Behavior" option under "iOS Build" or "Android Build", and set it to either "Link SDK assemblies only" or "Don’t Link". You'll need to clean+rebuild your whole solution for the change to take effect.
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:cvt="clr-namespace:XamarinTest.UI.Resources.Converts;assembly=XamarinTest.UI">
<cvt:DoubleToImageConverter x:Key="CVT_DoubleToImage"/>
<cvt:MediaTypeToImageConverter x:Key="CVT_MediaTypeToImage"/>
</ResourceDictionary>
In my app, I add the assembly=XamarinTest.UI solved the problem. I think the dev team may removed the assembly=xxxx section in Xamarin Forms 5.0.

I can not seem to reach .resx WPF

I have a WPF c# project, and I created two .resx files for changing the language.
I've set Access Modifier: Public
Added in .xaml
xmlns:resx="clr-namespace:Management_Software.Properties"
When I try to access a string with {x:Static resx:...} I see only a .settings file .
Do I have to add the .resx files in App.config,App.xaml ?
in order to use resoiurces you have to:
1. Include resource file in xaml like
xmlns:resource ="clr-namespace:WpfApplication1.Strings.en_US"
2.You can use it with binding like:
<TextBlock Text="{x:Static resource:Resource1.Greeting}" />
if you do these things try to rebuild the project. Xaml cannot figure out that you included new file and generates an error.
In my resource file I have:
Name = Greeting
Value = Sample test
Hope this helps.

Silverlight XML Save does not overwrite the entire file

The best way to explain my problem is by a example.
I have a Xml file with some elements in it. When I remove a element, I save the file again and this problem occurs:
Before save
<?xml version="1.0" encoding="utf-8"?>
<ElementTree>
<Elements1 />
<Elements2>
<Element Name="TestElement" ID="4efa7cc9-a89a-429b-81f4-b00cde729f24" />
</Elements2>
<Elements3 />
</ElementTree>
After save
<?xml version="1.0" encoding="utf-8"?>
<ElementTree>
<Elements1 />
<Elements2 />
<Elements3 />
</ElementTree>D="4efa7cc9-a89a-429b-81f4-b00cde729f24" />
</Elements2>
<Elements3 />
</ElementTree>
What I suspect it is doing: It keeps writing the text untill it has reached the end of the text to be saved and then it stops. It does not remove the text that is still writen in the file AFTER the last character in the stream.
How can I fix this?
Make sure you replace the existing file when opening it and you close the stream correctly.

How to include external font in WPF application without installing it

How to include external font in WPF application without installing it
I tried this code
System.Drawing.Text.PrivateFontCollection privateFonts = new System.Drawing.Text.PrivateFontCollection();
privateFonts.AddFontFile("C:\\Documents and Settings\\somefont.ttf");
System.Drawing.Font font = new Font(privateFonts.Families[0], 12);
this.label1.Font = font;
It working correctly in Windows Form Application but not in WPF.
There are two ways of doing this: One way is to package the fonts inside the application. The other way is to have the fonts in a folder. The difference is mostly the URI you need to load the files with.
Package with Application
Add a /Fonts folder to your solution.
Add the True Type Fonts (*.ttf) files to that folder
Include the files to the project
Select the fonts and add them to the solution
Set BuildAction: Resource and Copy To Output Directory: Do not copy. Your .csproj file should now have a section like this one:
<ItemGroup>
<Resource Include="Fonts\NotoSans-Bold.ttf" />
<Resource Include="Fonts\NotoSans-BoldItalic.ttf" />
<Resource Include="Fonts\NotoSans-Italic.ttf" />
<Resource Include="Fonts\NotoSans-Regular.ttf" />
<Resource Include="Fonts\NotoSansSymbols-Regular.ttf" />
</ItemGroup>
In App.xaml add <FontFamily> resources. It should look like in the following code sample. Note that the URI doesn't contain the filename when packing with the application.
<Applicaton ...>
<Application.Resources>
<FontFamily x:Key="NotoSans">pack://application:,,,/Fonts/#Noto Sans</FontFamily>
<FontFamily x:Key="NotoSansSymbols">pack://application:,,,/Fonts/#Noto Sans Symbols</FontFamily>
</Application.Resources>
</Application>
Apply your fonts like this:
<TextBlock x:Name="myTextBlock" Text="foobar" FontFamily="{StaticResource NotoSans}"
FontSize="10.0" FontStyle="Normal" FontWeight="Regular" />
You can also set the font imperatively:
myTextBlock.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./Fonts/#Noto Sans");
Copy to Output Directory
Add a /Fonts folder to your solution.
Add the True Type Fonts (*.ttf) files to that order
Include the files to the project
Select the fonts and add them to the solution
Set BuildAction: Content and Copy To Output Directory: Copy if newer or Copy always. Your .csproj file should now have a section like this one:
<ItemGroup>
<Content Include="Fonts\NotoSans-Bold.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Fonts\NotoSans-BoldItalic.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Fonts\NotoSans-Italic.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Fonts\NotoSans-Regular.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Fonts\NotoSansSymbols-Regular.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
In App.xaml add <FontFamily> resources. It should look like in the following code sample.
<Applicaton ...>
<Application.Resources>
<FontFamily x:Key="NotoSansRegular">./Fonts/NotoSans-Regular.ttf#Noto Sans</FontFamily>
<FontFamily x:Key="NotoSansItalic">./Fonts/NotoSans-Italic.ttf#Noto Sans</FontFamily>
<FontFamily x:Key="NotoSansBold">./Fonts/NotoSans-Bold.ttf#Noto Sans</FontFamily>
<FontFamily x:Key="NotoSansBoldItalic">./Fonts/NotoSans-BoldItalic.ttf#Noto Sans</FontFamily>
<FontFamily x:Key="NotoSansSymbols">./Fonts/NotoSans-Regular.ttf#Noto Sans Symbols</FontFamily>
</Application.Resources>
</Application>
Apply your fonts like this:
<TextBlock Text="foobar" FontFamily="{StaticResource NotoSansRegular}"
FontSize="10.0" FontStyle="Normal" FontWeight="Regular" />
References
MSDN: Packaging Fonts with Applications
I use such XAML code:
<Style x:Key="Hatten">
<Setter Property="TextElement.FontFamily" Value="Resources/#HATTEN" />
</Style>
#HATTEN - reference to hatten.tft in Resources.
Using the Style:
<TextBlock x:Name="lblTitle" Style="{DynamicResource Hatten}" FontSize="72"></TextBlock>
The best answer to this question I found here
http://geekswithblogs.net/Martinez/archive/2010/01/29/custom-font-in-wpf-application.aspx
SOLUTION
It involves using even more wicked string than before but works as expected:
<Label FontFamily="pack://application:,,,/Folder1/#Katana Sans">Text</Label>
Where is Folder1 is a folder of your project where you keep a TTF file.
Three notes at the end:
‘Katana Sans’ is the name of the font, not the name of the file. This is significant difference. To get the name of the font simply click the file twice. Note that spaces are included without any changes in this string.
Remember to put the hash sign ‘#’ in front of font name. It will not work otherwise.
Custom font may also be added to the project with ‘Build Action’ set to ‘Content’. This is not recommended approach however and for the sake of simplicity I ignored this possibility.
Some extra links
https://msdn.microsoft.com/en-us/library/ms753303(v=vs.100).aspx
https://msdn.microsoft.com/en-us/library/cc296385.aspx
The easiest way to include external fonts is to
Step 1. Create the folder Fonts And add your fonts into it.
Step 2. Set Build action to content and Copy to Output Directory to Copy always.
Step 3. Build the Solution to update it with the Font directory.
Step 4. Use the font in your elements using FontFamily="Fonts/#font name"
All Done!
Thanks to cscience !
I did not found any answer for exactly that.
But I found a solution that I did not saw on Internet.
I followed a recommendation of doing a folder and marking all the files inside as Resources.
But I needed to enumerate them, and that was my main problem, because I need to load all to my screen without recording the name somewhere. I just want to simple drop another font to that folder and list it.
I found this as a solution to list all the files inside my resources/fonts folder
Fonts.GetFontFamilies(new Uri("pack://application:,,,/resources/fonts/#"))
I expect it to help you organize your fonts.
I use xaml for this
<Window
FontFamily ="./Fonts/#somefont"
>
I included the font file inside the "Fonts" folder
First) Copy fonts to project folder like /Resources/Fonts/ and set font-properties -> Build Action:Resource
Second) Used on code
FontFamily="/AssemblyNamespace;component/Resources/Fonts/IranSans/#IRANSansWeb Medium"
Although the solution from #movgp0 worked at first, it failed when I wanted a Bold text. By searching a bit more online I found the following solution that works in all cases:
<Application.Resources>
<FontFamily x:Key="FiraSansBold">Fonts/FiraSansCondensed-Bold.ttf#Fira Sans Condensed</FontFamily>
<FontFamily x:Key="FiraSansBoldItalic">Fonts/FiraSansCondensed-BoldItalic.ttf#Fira Sans Condensed</FontFamily>
<FontFamily x:Key="FiraSansItalic">Fonts/FiraSansCondensed-Italic.ttf#Fira Sans Condensed</FontFamily>
<FontFamily x:Key="FiraSans">Fonts/FiraSansCondensed-Regular.ttf#Fira Sans Condensed</FontFamily>
</Application.Resources>
Usage: <Label FontFamily="{StaticResource FiraSansBold}">SomeBoldedLabel</Label>
I have a fonts folder inside an assets folder in my project. At build time it will copy those fonts into the folder as content. Then I can simply use the following XAML to use the FontAwesome font to have an icon on my button.
<Button Content="" FontFamily="./assets/fonts/#FontAwesome">
Here's What Worked for me:
(1) Add Directory to project: font
(2) Move ttf font file into font directory
(3) Add ttf font file to project
(4) Set the "Build" Property of the tff font file to "Resource"
(Note: I used "Resource" instead of "Embedded Resource"
in the combobox selection.)
(5) Open the Window where you are using the font and make the
following changes:
<Window ... >
<Window.Resources>
<FontFamily x:Key="YourFontNickName">
pack://application:,,,/font/#NameOfFont
<!-- Note: NameOfFont can be font by double clicking
on font from explorer and writing down name
of the font reported. Its not the same
as the file name -->
</FontFamily>
</Window.Resources>
<!-- Use font as Xaml -->
<StackPanel>
<Textblock FontFamily="{StaticResource YourFontNickName}">
This is a test
</Testblock>
<Textblock Name="text1"/>
</StackPanel>
...
</Window>
(6) If you want to change the font from code. Do this:
public partial class Window1 : Window {
// Use font as C# Code
public void UpdateText1() {
text1.Text = "Hi There";
FontFamily ff = this.Resources["YourFontNickName"]
as FontFamily;
if (ff == null) {
Messagebox.Show("Wrong Font Name", "ERROR");
return;
}
text1.FontFamily = ff;
text1.FontSize = 30;
}
...
}
You need to add fonts as resource item
according to the link; you may add your desired font as application resource file by following these steps:
copy your font file to anywhere in your project in most cases resource files is copied into a folder named "resources" if you want to follow this, create new folder in your project and name it as "resources" then copy your font file which is usually have ttf or otf format into that folder
then you need to declare this file as a resource file for doing so you have two options:
right click on your font file, select properties (or press F4 after selecting it)
and then change "Build Action" to "Resource"
go into your projects directory edit project-name.csproj file and the
following tag:
<ItemGroup>
<Resource Include="resources\<font-file-name>.ttf" />
</ItemGroup>```
finally within you application you may set font-family property like this:
FontFamily="./Resources/Fonts/#<font-name>"
be careful 'font-name' is different from font-file-name you may open font file and check for font name property (in Microsoft Window)
I was trying to get this to work as well with a different font,
it only worked for me in this format with the ./Fonts
<FontFamily x:Key="NotoSans">pack://application:,,,./Fonts/#Noto Sans</FontFamily>
Kind of, Really easy:
<TextBlock x:Name="lblTitle" FontSize="24" Margin="256,25,178,289" Text="gg" FontFamily="/FontTest;component/#Arvo"></TextBlock>
When you import a font, vs will gonna include it in font list😊.

Categories