xamarin
  • windows-phone-8.1
  • win-universal-app
  • xamarin.forms
  • windows-10-universal
  • 2016-04-25 39 views 5 likes 
    5

    在我的共享便攜式Xamarin項目,該工程對UWP Xamarin.Forms的Windows Phone 8.1和Windows 8.1:網頁視圖的BaseURL在UWP和Windows Phone 8.1

    HtmlWebViewSource htmlSource = new HtmlWebViewSource(); 
    htmlSource.Html = @"<html><body><img src='ms-appx-web:///Assets/somePicture.png' /></body></html>"; 
    htmlSource.BaseUrl = DependencyService.Get<IBaseUrl>().Get(); 
    WebView webView = new WebView 
    { 
         Source = htmlSource, 
    }; 
    

    顯然,這不是跨平臺(iOS和Android)。我想這一點,但它不會對UWP工作的Windows Phone 8.1和Windows 8.1:

    HtmlWebViewSource htmlSource = new HtmlWebViewSource(); 
    htmlSource.Html = @"<html><body><img src='somePicture.png' /></body></html>"; 
    htmlSource.BaseUrl = DependencyService.Get<IBaseUrl>().Get(); 
    WebView webView = new WebView 
    { 
         Source = htmlSource, 
    }; 
    

    IBaseUrl:對UWP

    public interface IBaseUrl { string Get(); } 
    

    BASEURL執行的Windows Phone 8.1和Windows 8.1,取自一個Windows Phone 8.0 sample project

    [assembly: Dependency(typeof(MyApp.UWP.BaseUrl))] 
    namespace MyApp.UWP 
    { 
        public class BaseUrl : IBaseUrl 
        { 
         public string Get() 
         { 
          return ""; // "ms-appx-web:///Assets/" doesn't work 
         } 
        } 
    } 
    

    我試圖返回的BASEURL的不同變化 「MS-APPX的web:///資產/」, 「MS-APPX的web:///」,放置在文件項目根目錄或「As」設置「目錄,沒有任何工程。

    據我所知,這用於在Windows Phone 8.0上工作。

    回答

    1

    作爲Xamarin形式2.3.1,WebViewRenderer用於Windows RT簡單地忽略該BASEURL(https://github.com/xamarin/Xamarin.Forms/blob/2.3.1/Xamarin.Forms.Platform.WinRT/WebViewRenderer.cs#L26)。但是,在2.3.2分支上有一個修正:https://github.com/xamarin/Xamarin.Forms/blob/2.3.2/Xamarin.Forms.Platform.WinRT/WebViewRenderer.cs

    3

    Device.OnPlatform呢?

    var urlIos = @"somePicture.png"; 
    var urlAndroid = @"somePicture.png"; 
    var urlUWP = @"ms-appx-web:///Assets/somePicture.png"; 
    htmlSource.Html = string.Format(@"<html><body><img src='{0}' /></body></html>", Device.OnPlatform(urlIos, urlAndroid, urlUWP)); 
    
    +0

    這可以工作,但它必須爲每一個單獨的URL完成,這並不是我最好的解決方案 –

    +1

    嗯,我看到你的問題。我無法通過UWP找出BaseUrl。但是Regex.Replace呢?這可以工作。 – Matt

    +0

    我正在Xamarin跨平臺項目上工作,如果將圖像放置在子文件夾內,它不會在屏幕上加載。 「ms-appx:///Assets/abc.png」 – Ashaar

    相關問題