Accessing Resources in a C# WPF Application (Same Assembly) -
(before jump nitty-gritty, want set context: i'm trying load wpf frame contents of .html file i'm including in project resource.)
i create new wpf application; add new folder called 'foofiles' project, , add couple of files (page1.foo , page2.foo) folder.
for each newly added .foo file, right-click on it, go "properties," , set build action 'resource,' , copy output directory "copy always."
i want able access files both in xaml:
<frame x:name="bar" source="/foofiles/page1.foo"/>
and in procedural code:
private void somefunc() { bar.source = new uri("/foofiles/page1.foo"); }
but can't figure out why doesn't work -- "format of uri not determined."
in code-behind, tried doing this:
private void somefunc() { bar.source = new uri("pack://application:,,,/foofiles/page1.foo"); }
which didn't throw exception, main window crashed.
in thinking, if add file of type project, , if mark "resource" in "build action," should able use file per examples above. also, use file this:
private void someotherfunc() { system.io.streamreader reader = new system.io.streamreader("/foofiles/page1.foo"); string bar = reader.readtoend(); }
any appreciated... in advance!
try adding component-part pack uri this
pack://application:,,,/assemblyname;component/resourcename
where assemblyname name of assembly. case, following statement should work:
bar.source = new uri("pack://application:,,,/assemblyname;component/foofiles/page1.foo");
more practically, try relative pack uri notation:
bar.source = new uri("assemblyname;component/foofiles/page1.foo", urikind.relative));
for stream reading resources use
var streamresourceinfo = application.getresourcestream(uri); using (var stream = streamresourceinfo.stream) { // fancy stuff stream }
Comments
Post a Comment