Successfully referencing ResourceDictionary in file being loaded by XamlReader.Load() - xaml

I am building a common WP7 assembly which will display common help/about information for my apps, each app assembly will specify a pair of StackPanels which have some of the app specific information (well call em Legal.xaml and WhatsNew.xaml).
Ideally these app specific XAML files should be in a plaintext form (vs something that is instantiated in code) so loadable via HTTP or as an embedded resource string.
Loading the XAML works fine, until I try to break out some of the style definitions into another file, then XamlReader.Load() fails with a note that: “Attribute AboutPageDocs/CommonStyles.xaml value is out of range. [Line: 43 Position: 45]”
That error would happen when loading Legal.xaml, which when we look around like 43 we find where I am attempting to load the ResourceDictionary that now contains the custom styles:
<StackPanel.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="AboutPageDocs/CommonStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</StackPanel.Resources>
Here is the bugger... if simply copy & paste the StackPanel code (which is being loaded dynamically at runtime) and drop it into a UserControl... things work fine.
Short of having to define my styles inline in both Legal.xaml & WhatsNew.xaml... is there any way to have XamlReader.Load() property lookup CommonStyles.xaml?
On the thought that the Source path was not correct, I have tried placing copies of CommonStyles.xaml in various locations through both assemblies... as well as experimented with the pack:// uri syntax... all to no avail thus far.
What am I missing?

As I realized that XamlReader is able to resolve referenced XAML files when they are specified as absolute paths, I looked for a possibility to specify an own context.
I found this working for me, when I specify a ParserContext when calling XamlReader.Load()
public static FlowDocument ReadFlowDocument( FileInfo xamlFile )
{
// Specify a ParserContext.
// It's important to set BaseUri to the file itself
// not to its parent direcory!
ParserContext parserContext = new ParserContext();
parserContext.BaseUri = new Uri( xamlFile.ToString() );
// Create a stream from this file
FileStream stream = new FileStream( xamlFile.ToString(), FileMode.Open );
// Let the XamlReader load and parse the XAML. It will resolve any referenced ResourceDirectories
// specified with a relative path
return (FlowDocument) XamlReader.Load( stream, parserContext );
}

Related

Loading XAML workflow with multiple activity assemblies

I have a XAML workflow which, which uses custom activities that are stored in more than one dlls. I’m trying to execute this workflow using WorkflowApplication. However I cannot figure out how to resolve multiple reference assemblies, while loading the XAML. I’m aware that the XamlXmlReaderSettings provides a LocalAssembly property, which allows us to provide the reference assembly. However, it only allows to provide a single assembly. How do I provide multiple reference assemblies to the reader, so that it is able to resolve the external types? Any help will be greatly appreciated. I’ve pasted the code I’m using for reference.
public void LoadWorkflowFromFileAsync(string workflowXaml, Assembly activityAssembly)
{
var xamlReaderSettings = new XamlXmlReaderSettings
{
LocalAssembly = activityAssembly
};
var xamlSettings = new ActivityXamlServicesSettings
{
CompileExpressions = true
};
using (var reader = new XamlXmlReader(workflowXaml, xamlReaderSettings))
{
_activity= ActivityXamlServices.Load(reader, xamlSettings);
}
}
Does your xmlns in the XAML include the assembly name (ex. xmlns:ede="clr-namespace:Sample.MyActivityLibrary;assembly=Sample.MyActivityLibrary")?
I'm not aware of anyway to reference multiple local assemblies in XamlXmlReaderSettings but if the assembly is referenced in the XAML it should resolve automatically.

Reference a class' static field of the same internal module but in a different file?

I'm using TypeScript and require.js to resolve dependencies in my files. I'm in a situation where I want to reference a static field of a class in an other file, but in the same internal module (same folder) and I am not able to access it, even if the Visual Studio pre-compiler does not show any error in my code.
I have the following situation :
Game.ts
class Game {
// ...
static width: number = 1920;
// ...
}
export = Game;
Launcher.ts
/// <reference path='lib/require.d.ts'/>
import Game = require("Game");
var width: number = Game.width;
console.log(width); // Hoping to see "1920"
And the TypeScript compiler is ok with all of this. However, I keep getting "undefined" at execution when running the compiled Launcher.ts.
It's the only reference problem I'm having in my project, so I guess the rest is configured correctly.
I hope I provided all necessary information, if you need more, please ask
Any help is appreciated, thanks !
Your code seems sound, so check the following...
You are referencing require.js in a script tag on your page, pointing at Launcher (assuming Launcher.ts is in the root directory - adjust as needed:
<script src="Scripts/require.js" data-main="Launcher"></script>
Remove the reference comment from Launcher.ts:
import Game = require("Game");
var width: number = Game.width;
console.log(width); // Hoping to see "1920"
Check that you are compiling using --module amd to ensure it generates the correct module-loading code (your JavaScript output will look like this...)
define(["require", "exports", "Game"], function (require, exports, Game) {
var width = Game.width;
console.log(width); // Hoping to see "1920"
});
If you are using Visual Studio, you can set this in Project > Properties > TypeScript Build > Module Kind (AMD)
If you are using require.js to load the (external) modules, the Game class must be exported:
export class Game {}
If you import Game in Launcher.ts like
import MyGame = require('Game')
the class can be referenced with MyGame.Game and the static variable with MyGame.Game.width
You should compile the ts files with tsc using option --module amd or the equivalent option in Visual Studio

How to configure multiple sitemaps using MVCSiteMapProvider v4 with StructureMap DI

The problem, essentially, is that I can't get my sitemap config to support multiple sitemaps. It's always looking for "default" even when I name my instances and request another. Now for the background.
I've been pouring over the docs for the new implementation of MVCSiteMapProvider. They are now using Dependency Injection to configure the SiteMapProvider. We have an existing StructureMap DI implementation, so I followed the instructions and added, in our case
ObjectFactory.Configure(x =>
{
...
x.AddRegistry<MvcSiteMapProviderRegistry>();
...
});
Then I started tweaking the MvcSiteMapProviderRegistry.cs file to implement my multiple sitemap scenario. I have multiple site map files, either will work as long as it's called "default". If I remove the "default" item then it breaks and complains that "default" is missing. Which I assume is because it can't find my instance. Here's how I have them defined. I suspect the problem is somewhere in here... the loader which it says I have to configure in the Global.asax is looking for ISiteMapLoader but I'm adding my multiple configuration to SiteMapBuilderSet... anyway here's the code.
// Register the sitemap builder
string absoluteFileName = HostingEnvironment.MapPath("~/Main.sitemap");
string absoluteFileName2 = HostingEnvironment.MapPath("~/Test.sitemap");
var xmlSource = this.For<IXmlSource>().Use<FileXmlSource>()
.Ctor<string>("fileName").Is(absoluteFileName);
var reservedAttributeNameProvider = this.For<ISiteMapXmlReservedAttributeNameProvider>()
.Use<SiteMapXmlReservedAttributeNameProvider>()
.Ctor<IEnumerable<string>>("attributesToIgnore").Is(new string[0]);
var builder = this.For<ISiteMapBuilder>().Use<CompositeSiteMapBuilder>()
.EnumerableOf<ISiteMapBuilder>().Contains(y =>
{
y.Type<XmlSiteMapBuilder>()
.Ctor<ISiteMapXmlReservedAttributeNameProvider>().Is(reservedAttributeNameProvider)
.Ctor<IXmlSource>().Is(xmlSource);
y.Type<ReflectionSiteMapBuilder>()
.Ctor<IEnumerable<string>>("includeAssemblies").Is(includeAssembliesForScan)
.Ctor<IEnumerable<string>>("excludeAssemblies").Is(new string[0]);
y.Type<VisitingSiteMapBuilder>();
});
var xmlSource2 = this.For<IXmlSource>().Use<FileXmlSource>()
.Ctor<string>("fileName").Is(absoluteFileName2);
var builder2 = this.For<ISiteMapBuilder>().Use<CompositeSiteMapBuilder>()
.EnumerableOf<ISiteMapBuilder>().Contains(y =>
{
y.Type<XmlSiteMapBuilder>()
.Ctor<ISiteMapXmlReservedAttributeNameProvider>().Is(reservedAttributeNameProvider)
.Ctor<IXmlSource>().Is(xmlSource2);
y.Type<ReflectionSiteMapBuilder>()
.Ctor<IEnumerable<string>>("includeAssemblies").Is(includeAssembliesForScan)
.Ctor<IEnumerable<string>>("excludeAssemblies").Is(new string[0]);
y.Type<VisitingSiteMapBuilder>();
});
// Configure the builder sets
this.For<ISiteMapBuilderSetStrategy>().Use<SiteMapBuilderSetStrategy>()
.EnumerableOf<ISiteMapBuilderSet>().Contains(x =>
{
/* x.Type<SiteMapBuilderSet>()
.Ctor<string>("instanceName").Is("default")
.Ctor<bool>("securityTrimmingEnabled").Is(securityTrimmingEnabled)
.Ctor<bool>("enableLocalization").Is(enableLocalization)
.Ctor<ISiteMapBuilder>().Is(builder)
.Ctor<ICacheDetails>().Is(cacheDetails);*/
/*
x.Type<SiteMapBuilderSet>()
.Ctor<string>("instanceName").Is("MainSiteMapProvider")
.Ctor<bool>("securityTrimmingEnabled").Is(securityTrimmingEnabled)
.Ctor<bool>("enableLocalization").Is(enableLocalization)
.Ctor<ISiteMapBuilder>().Is(builder)
.Ctor<ICacheDetails>().Is(cacheDetails);*/
x.Type<SiteMapBuilderSet>()
.Ctor<string>("instanceName").Is("TestSiteMapProvider")
.Ctor<bool>("securityTrimmingEnabled").Is(securityTrimmingEnabled)
.Ctor<bool>("enableLocalization").Is(enableLocalization)
.Ctor<ISiteMapBuilder>().Is(builder2)
.Ctor<ICacheDetails>().Is(cacheDetails);
});
In my global.asax.cs I added
MvcSiteMapProvider.SiteMaps.Loader = Resolver.Get<ISiteMapLoader>();
and to reference in my view I have
#Html.MvcSiteMap("TestSiteMapProvider").Menu(false, true, true)
but it must not be able to find "TestSiteMapProvider" because it always displays "default" or complains if it doesn't exist.
I also thought it might have something to do with the Cache, as I see the filename referenced there, but I don't know how to add multiple instances to the cache, so I just disabled it. I'm really not doing anything fancy with my sitemaps anyway, and this whole thing is really feeling like massive overkill just to get some flippin automatic breadcrumbs!
Apparently there was another help doc that I wasn't aware of. I had completed all of the steps thus far properly, but I also needed to implement ISiteMapCacheKeyGenerator.
See this doc (which wasn't named this when I started.)
https://github.com/maartenba/MvcSiteMapProvider/wiki/Multiple-Sitemaps-in-One-Application

Access Denied when deleting image file previously used in DataTemplate in WinRT

I have image (PNG) files used by my GridView as part of its DataTemplate. If I try to delete a specific object row in my GridView, I would delete the related image file for that row as well. The images are different for every item on the list.
I'm using this code to delete the image file
StorageFile _file = await DataStore.GetFileAsync(filename);
await _file.DeleteAsync(StorageDeleteOption.Default);
The image file is rendered on the GridView under the GridView's DataTemplate.
So in each object model in my List, I have a public property there that returns an ImageSource for my DataTemplate.
I'm calling my delete procedure right after i deleted the object row from the List and after the GridView has been refreshed of the new List items.
Even though the List does not contain the object (consuming the image) anymore, the app throws the Access is Denied exception if i try to delete the file. While the app is running, if i try to delete that particular file manually (through file explorer), it won't allow me too.
I tried clearing all unused objects in my app, even setting the GridView's ItemSource to null and the List to null before I delete the image. Still the exception persist.
Thanks in advance.
One method you can try is to load the image into a memory steam, then create a BitmapImage object from that stream, you can then set the source of your Image control to that bitmap image.
Since you are not using the actual image file as the source of the image, you can easily delete it anytime :)
Though this is an old question, I have encountered the problem recently in a UWP app and actually managed to find a solution. But first some background information about the problem:
When you create a BitmapImage with a URI, the created URI object holds a reference to the file in your local storage and keeps it open, i.e. non-writable. This btw is only true when the Bitmap is just big enough to fit into the Image entirely, typically small or medium sized Bitmaps. If the Bitmap happens to be big enough, WinRT automatically uses a downsampled version when it is displayed in an Image. In this case the URI does NOT hold a reference to the original file.
Now to the actual solution:
Setting Image.Source to null doesn't do the trick here, as the URI is still alive (until the next GC cycle at least). What DID work for me, was casting the Source to the BitmapImage it originally was and settings the UriSource to null.
var bitmapImage = image.Source as BitmapImage;
if (bitmapImage != null)
bitmapImage.UriSource = null;
And yes, this IS stupid.
Mark already mentioned part of this but there is no need to make it that complicated. Simply, wherever you expect that you need to delete the bitmap while the system is holding it, use a converter like this:
public class SafeImageFileConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
string path = (string)value;
var stream = new MemoryStream(File.ReadAllBytes(path));
return ImageSource.FromStream(() => stream);
}
}
In practice, you might want to check whether the path exists or return an error.png or similar if it doesn't. Also, don't be tempted to use using with the stream, the system will need the stream so you shouldn't dispose it early.
The trick is to use a Uri object to load the image (instead of a string filename) and then to use the EXACT same Uri instance to delete the file (after removing the image from the UI, of course). Here is an example:
//Save the Uri as a member variable so you can get to it later
private Uri uri;
//Create the Uri
uri = new Uri(OriginalImageFilename, UriKind.Absolute);
//Load the image
BitmapImage bitmapImage = new BitmapImage(uri);
//This can also be done by binding a Image control's source property to the uri.
//Delete the image (remember to use the same Uri instance)
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);
await file.DeleteAsync();
As a work-around, i just deleted the unused images during app launch so that no processes are using it. Thanks.

Changing/Adding Resource Dictionary in code behind, Please help

I'm am currently developing a silverlight application, I am still a beginner with this.
I am wondering if it is possible to change the resource dictionary's source in code behind(C#)
within the App.xaml?
I have tried the code below, but get an exception, i am getting the style folder name from a WCF Service, the variable is called Style(this contains the name of the folder)
ResourceDictionary rDictionary = this.Resources.MergedDictionaries[0];
rDictionary.Source = new Uri(string.Format("Resources/Styles/{0}/Styles.xaml", style), UriKind.Relative);
this.Resources.MergedDictionaries.Add(rDictionary);
I'm getting an error at
rDictionary.Source = new Uri(string.Format("Resources/{0}/Styles.xaml", "Default"), UriKind.RelativeOrAbsolute);
Which reads
System.Exception: Error HRESULT E_FAIL has been returned from a call to a COM component.
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, String s)
at MS.Internal.XcpImports.SetValue(IManagedPeerBase doh, DependencyProperty property, Object obj)
at System.Windows.DependencyObject.SetObjectValueToCore(DependencyProperty dp, Object value)
at System.Windows.DependencyObject.SetEffectiveValue(DependencyProperty property, EffectiveValueEntry& newEntry, Object newValue)
at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
at System.Windows.ResourceDictionary.set_Source(Uri value)
at FCStarFish.App..ctor()
Does this work
<Application.Resources>
<ResourceDictionary x:Key="resourcestyles">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary /> <!-- Dummy, this is the one we will replace -->
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Then we place a ResourceDictionary in [0] (where our dummy ResourceDictionary is).
Load or replace the style dictionary (load it with the default style in Application_Startup in app.xaml.cs)
var rDictionary = new ResourceDictionary();
rDictionary.Source = new Uri(string.Format("/MyApp;component/Resources/Styles/{0}/Styles.xaml", style), UriKind.Relative);
this.Resources.MergedDictionaries[0] = rDictionary;
Replace MyApp with your applications name.
First follow the steps mentioned by NateTheGreat below and prepend your UriStirng with "/". It should look like:
ResourceDictionary rDictionary = this.Resources.MergedDictionaries[0];
rDictionary.Source = new Uri(string.Format("/Resources/Styles/{0}/Styles.xaml", style), UriKind.Relative);
this.Resources.MergedDictionaries.Add(rDictionary);
I just encountered this problem. In my case, the solution was to change the resource dictionary XAML file's build action to "Content", Copy to Output Directory to "Copy if newer", and Custom Tool to an empty string. The defaults were set to Page/Do not copy/MSBuild:Compile, respectively.