1

私は WP8 開発を始めたばかりで、すでに最初の問題に出くわしました: Windows Phoneチュートリアル用の最初のアプリを作成する方法に従いましたが、別のチュートリアルに従ってブラウザーの CSS を上書きしようとしました。アプリを実行すると、CSS は変更されず、元の Web ページが表示されます。

CSS ファイルが呼び出さmobile.cssれ、Resources フォルダーにあります。その内容は単純に

body {

background-color: blue;

}

これらはそのプロパティです:

ここに画像の説明を入力

これまでの (非常に単純な) アプリ コードは次のとおりです。

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);
        }
    }
}
}

それ以外はすべて機能します。ブラウザが表示され、ページが正しく読み込まれます。唯一のことは、mobile.cssファイルが無視されているように見えることです。

4

1 に答える 1

0

css ファイルをリソースとして追加します。

リソースとしての Windows Phone CSS

次のコードを使用してコンテンツを取得します。

StreamResourceInfo sr = Application.GetResourceStream(new Uri("/YourAppName;component/Resources/Custom.css", UriKind.Relative));
于 2013-11-03T19:57:12.123 に答える