I'm in the process of removing the XAML from my Silverlight project and making it use only code (as per this article).
Here is my very simple startup event for a Silverlight application (with the standard App.xaml from the template project):
private void Application_Startup(object sender, StartupEventArgs e)
{
Grid grid = new MainPage();
this.RootVisual = grid;
var mediaElement = new MediaElement();
mediaElement.MediaFailed += (s, ea) => { mediaFailed = true; };
mediaElement.Source = new Uri(@"/Content/Some Music.mp3", UriKind.Relative);
grid.Children.Add(mediaElement);
}
Where the MP3 file is set to "Build Action: None, Copy if newer" (ie: it's beside the XAP). Here's the XAML for MainPage:
<Grid x:Class="TestGame.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Grid>
And the C# - nothing unusual here at all.
public partial class MainPage : Grid
{
public MainPage()
{
InitializeComponent();
}
}
That all works so far. So my question is this: why is it that when I change
Grid grid = new MainPage();
to
Grid grid = new Grid();
the mediaElement.MediaFailed
event gets called (with a AG_E_NETWORK_ERROR)?
The only interesting thing that InitializeComponent
is doing is calling Application.LoadComponent
(it's the default generated code). So what might that function be doing that allows source URIs to work?
It seems that Application.GetResourceStream
still works just fine. But I need to be able to get a few resources external to the XAP.
(Note: it seems this guy is having the same problem - but no one answered his question.)