1

I am just getting started with WP8 development and I have already stumbled into my first problem: I have followed the How to create your first app for Windows Phone tutorial and now I have followed another tutorial to try and overwrite the CSS of the browser. When I run the app, the CSS is not changed and I just see the original web page.

The CSS file is called mobile.css and is found in the Resources folder. Its contents are simply

body {

background-color: blue;

}

and these are its properties:

enter image description here

The (very simple) app code so far is this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using MiniBrowser.Resources;
using System.IO;
using System.Windows.Resources;

namespace MiniBrowser
{
public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void Go_Click(object sender, RoutedEventArgs e)
    {
        string site = URL.Text;
        MiniBrowser.Navigate(new Uri(site, UriKind.Absolute));
    }

    private void BrowserLoadCompleted(object sender, NavigationEventArgs e)
    {
        ApplyStyleSheet("mobile.css");

        VisualStateManager.GoToState(this, "Loaded", true);
    }

    private void ApplyStyleSheet(string cssFilename)
    {
        //var sr = Application.GetResourceStream(new Uri(cssFilename, UriKind.Relative));
        StreamResourceInfo sr = Application.GetResourceStream(new Uri("/MiniBrowser;component/Resources/mobile.css", UriKind.Relative));
        using (var strm = sr.Stream)
        using (var reader = new StreamReader(strm))
        {
            string css = reader.ReadToEnd().Replace("\r", "").Replace("\n", "");

            var scriptToRun =
                "(function() {" +
                "  var pa= document.getElementsByTagName('head')[0] ; " +
                "  var el= document.createElement('style'); " +
                "  el.type= 'text/css'; " +
                "  el.media= 'screen'; " +
                "  el.styleSheet.cssText= '" + css + "'; " +
                "  pa.appendChild(el); " +
                "})();";
            MiniBrowser.InvokeScript("eval", scriptToRun);
        }
    }
}
}

Everything else works: the browser is displayed and correctly loads pages. The only thing is that the mobile.css file seems to be ignored.

user1301428
  • 1,743
  • 3
  • 25
  • 57

1 Answers1

0

Add the css file as Resource:

windows phone css as resource

Use the following piece of code to get the content:

StreamResourceInfo sr = Application.GetResourceStream(new Uri("/YourAppName;component/Resources/Custom.css", UriKind.Relative));
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • Hi, thanks for the answer! Unfortunately this doesn't seem to work either :( Do you have any other ideas? (PS: I have substituted the line `var sr = Application.GetResourceStream(new Uri(cssFilename, UriKind.Relative));` with your line of code and I have set the .css file as Resource) – user1301428 Nov 03 '13 at 22:30
  • @user1301428 - This is a working solution. After reading the css, make sure the content is there. Put a breakpoint on `string css = reader.ReadToEnd().Replace("\r", "").Replace("\n", "");` line and see. The problem might be that the css is not getting applied right, but the file and its content are being read all right. – manojlds Nov 04 '13 at 06:19
  • I have inserted a breakpoint there and I can't see any string for css, so that might be the problem. I have also tried manually inserting some code in `scriptToRun` but that is not working either. In my opinion it's very weird that something as easy should be so time consuming, but if this is not working, do you have any other idea on how to do this? – user1301428 Nov 04 '13 at 11:43
  • Do a clean build. Ensure your css is set to Resource. Make sure it is in the exact path as shown above. – manojlds Nov 04 '13 at 11:44
  • I have updated the question with the latest code and the screenshot of the css properties. This is all there is in the project, so you could even try copying and pasting this into a new app project and see if it is working for you... – user1301428 Nov 04 '13 at 11:56
  • I have just realized that the content of the variable `sr` in tour code is `System.Windows.Resources.StreamResourceInfo`. Is this correct? I thought it would get the content of the file.. – user1301428 Nov 05 '13 at 10:52