I will explain in 3 basic steps what i do and then show the code:
The problem- when i try to add it to the ResourceDictionary it says - : 'Source can only be set from XAML.'
Code:
1)
string content;
using (StreamReader sr = new StreamReader(Assets.Open(assetName)))
{
content = sr.ReadToEnd();
}
`
private static string fileName { get; set; } public static void BeforeLoadForms(string colorsXAML) { var backingFile = Path.Combine(System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal), "colorsXAML.xaml"); using (var writer = File.CreateText(backingFile)) { writer.WriteLine(colorsXAML); } fileName = backingFile; }`
Application.Current.Resources.MergedDictionaries.Clear();
ResourceDictionary rd1 = new ResourceDictionary();
rd1.Source = new Uri(fileName, UriKind.Absolute);
Application.Current.Resources.MergedDictionaries.Add(rd1);
(Untested; I don't know if this will work.)
The error message says you can only set Source in XAML. First, try a manual test, where you use a file you've prepared beforehand.
Add to project a new item, a XAML file. Contents:
<ResourceDictionary
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyNameSpace.MyResourceDictionary" Source="file:///myresources.xml"/>
Put in appropriate names in place of MyNameSpace, MyResourceDictionary, myresources.xml.
I'm not sure the exact form needed for a file URI.
Then in your code, where you currently try to add to Resources.MergedDictionaries, instead do:
var rd = new MyResourceDictionary();
Application.Current.Resources = rd;
If you can get this to work, then you can improve it. For example, could use MVVM, change to Source="{Binding ResourcesURI}"
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.