好库网 好库网首页 | 我的好库
微微发软的芒果

文章分类

WpWidgetLibrary - Windows Phone的HTML部件库(Widgets)

发布者:winphone
发布日期:2012/6/9 23:49:26   更新日期:2012/6/9 23:49:26
阅读次数:2899
评分:4.80
介绍:如果你想以离线模式打开WP7 HTML应用,还是有问题的。当前,没办法用WebBroserControl浏览本地HTML文件。有了WPWidgetLibrary你可以创建像本地WP7应用一样的HTML5应用。
正文:

 

WP7浏览器与桌面版的IE9基本相同,这意味着WP7能够显示HTML5内容和运行HTML5程序。

但如果你想以离线模式打开HTML应用,还是有问题的。当前,没办法用WebBroserControl浏览本地HTML文件。这意味着你在WP7上开发HTML5 Widget应用会受到一些限制。有了WPWidgetLibrary你可以创建像本地WP7应用一样的HTML5应用。之后,你可以发布在Windows Phone市场,应用程序更新也很容易。该库首先通过模拟真实的硬盘、脚本、图像、HTML页面的路径,将HTML5内容安装在独立的存储器上。

 

WPWidgetLibrary类的结构:

/// <summary>
/// Class is used to extract HTML content from the application resource 
/// and use as regular HTML Widget app
/// </summary>
public class WidgetLibrary
{
    /// <summary>
    /// list of folder structure of the HTML app
    /// </summary>
    private List<string> m_Subfolders = new List<string>();

    /// <summary>
    /// Extract all HTML5 app from the resource
    /// and create file system in Isolated Storage.
    /// </summary>
    /// <param name="htmlFolder"></param>
    public void InstalHTMLContent(string htmlFolder, string[] htmlDirs);

    /// <summary>
    /// Check is file is in corect file format
    /// </summary>
    /// <param name="webResource"></param>
    /// <returns></returns>
    private bool iswebResource(string webResource);

    /// <summary>
    /// We can restrict file to be part of HTML app.
    /// </summary>
    /// <param name="ext"></param>
    /// <returns></returns>
    protected virtual bool isWebExtension(string ext);

    /// <summary>
    /// Small helper to xtract name from full resource W7 name.
    /// </summary>
    /// <param name="assemblyReplacement"></param>
    /// <param name="webResource"></param>
    /// <returns></returns>
    private string getFileNameFromResourceName(string assemblyReplacement, 
                   string webResource);

    /// <summary>
    /// This is copying files from installed to isolatedstorage target location 
    /// </summary>
    /// <param name="isoFile"></param>
    /// <param name="webResourceFileName"></param>
    /// <param name="webResourceStream"></param>
    private void copyFile(IsolatedStorageFile isoFile, 
            string webResourceFileName, Stream webResourceStream);
        
    /// <summary>
    /// Creates the list of all subfolders from specified array.
    /// Creates from "aa/bb/cc following entries in m_Subfolders:
    /// aa
    /// aa/bb
    /// aa/bb/cc 
    /// </summary>
    /// <param name="directories">
    /// </param>
    private void resolveSubfolders(string[] directories);
        
    /// <summary>
    /// Create folder structure of our web site with all folders. 
    /// HTML folder must be in root app folder
    /// </summary>
    /// <param name="isolatedStorageFile"></param>
    /// <param name="webResourceFileName"></param>
    /// <returns></returns>
    private string createRequiredDirectory(IsolatedStorageFile 
            isolatedStorageFile,string webResourceFileName);

    /// <summary>
    /// Delete directory of previous version before
    /// you install current version is recomended.
    /// We need to go throught all folders and subfolder in order to delete every file.
    /// </summary>
    /// <param name="isolatedStorageFile"></param>
    /// <param name="root"></param>
    private void deleteDirectory(IsolatedStorageFile isolatedStorageFile,string root);

    /// <summary>
    /// Check if the app is running for the first time.
    /// </summary>
    /// <returns></returns>
    private bool IsFirstTimeRun();
}

关键函数InstalHTMLContent(string htmlFolder, string[] htmlDirs);的实现:

public void InstalHTMLContent(string htmlFolder, string[] htmlDirs)
{
    if (htmlDirs == null)
        throw new Exception("No html folder structure passed!");

    //Load HTML content first time App is running
    if (!IsFirstTimeRun())
        return;

    Debug.WriteLine("Application runs for the first time!");

    //Resolve folder structure of HTML app
    resolveSubfolders(htmlDirs);

    var asm = Assembly.GetCallingAssembly();

    using (IsolatedStorageFile isolatedStorageFile = 
             IsolatedStorageFile.GetUserStoreForApplication())
    {
        deleteDirectory(isolatedStorageFile, String.Empty);

        foreach (var webResource in asm.GetManifestResourceNames())
        {
            if (iswebResource(webResource))
            {
                var pos = webResource.ToLower().IndexOf(htmlFolder.ToLower());
                if (pos > 0)
                    copyFile(isolatedStorageFile, getFileNameFromResourceName(
                      webResource.Substring(0, pos + htmlFolder.Length + 1), 
                      webResource), asm.GetManifestResourceStream(webResource));
                else
                    throw new InvalidOperationException("invalid resource name.");
            }
        }
    }
}
使用方法:
1.在WP7项目中引用WPWidgetLibrary DLL
2.复制HTML(App)到项目文件夹下,所有文件作为EmbeddedResource使用
3.从工具箱拖出WebBrowser控件,在PageLoaded事件浏览WEB页面。
4.在MainPage构造函数中,添加下面的代码。在应用程序启动时,复制所有HTML相关内容到独立的存储器,InstallHTMLContent的第二个参数中包含了所有项目中需要拷贝的子文件夹。
5.在MainPage xaml.cs构造函数中,添加如下代码:
var wdgt = new WPWidgetLibrary();            
wdgt.InstalHTMLContent("HTML", new string[]
               { "js/jQueryMobile/images",
               "SubContent/SubSubContent",
              });
6.在MainPage Loaded事件中通过WebBrowser打开初始页面,代码如下:
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    //Navigate to initial page of the HTML app
     webBrowser1.Navigate(new Uri("StartPage.htm", UriKind.Relative));
}
库的自定义使用
比如你想添加AVI文件到HTML5应用中,你需要从基类派生新的类,例如:
public class MyWPWidgetLibrary: WPWidgetLibrary{

protected override bool IsWebExtension(string ext){
   bool res = false;
   switch (ext.ToLower())
   {
       case "avi":
           return true;
           //break;
   }
   return base.IsWebExtension(ext);
  }
}
下载全部源代码:http://www.okbase.net/file/item/3020

 

 

 


评论 [发表评论]
账号 密码 还没帐号呢,现在注册一个?

免责声明:好库网所展示的信息由买卖双方自行提供,其真实性、准确性和合法性由信息发布人负责。好库网不提供任何保证,并不承担任何法律责任。