好吧,所以我認爲這將會非常簡單,即使我以前剛剛使用WPF項目資源文件時也是如此。將項目資源加載到WPF Web瀏覽器控件中
當我的應用程序啓動時,我想將一個本地(附帶應用程序)HTML文件加載到Webbrowser控件中。不過,我找不到一個簡單的方法來做到這一點!有任何想法嗎?
好吧,所以我認爲這將會非常簡單,即使我以前剛剛使用WPF項目資源文件時也是如此。將項目資源加載到WPF Web瀏覽器控件中
當我的應用程序啓動時,我想將一個本地(附帶應用程序)HTML文件加載到Webbrowser控件中。不過,我找不到一個簡單的方法來做到這一點!有任何想法嗎?
了這一輪以文件的生成操作「始終複製」設置,然後使用Environment.CurrentDirectory
獲取應用程序目錄末尾:
string appDir = Environment.CurrentDirectory;
Uri pageUri = new Uri(appDir + "/page.html");
myWebBrowser.Source = pageUri;
這似乎很好地工作。
我剛纔遇到了同樣的問題。我希望能簡單地做到這一點:
<WebBrowser x:Name="myWebBrowser" Source="page.html"/>
但是,相反,我得到這個錯誤:
Relative URIs are not allowed. Parameter name: source
所以這很煩人。相反,我結束了非常相似,你在後面的代碼的解決方案:
myWebBrowser.Source = new Uri(new System.IO.FileInfo("page.html").FullName);
我敢肯定有一些XAML的柔術功夫方法,用以避開這個問題,但我不知道是什麼是。^_^
展會結束後,我在找同樣的東西時偶然發現了這個。得到它以更WPF的方式工作。 WebBrowser具有NavigateToStream()方法,因此您可以簡單地設置資源流。
StreamResourceInfo info = Application.GetResourceStream(new Uri("Page.html", UriKind.Relative));
if (info != null)
myWebBrowser.NavigateToStream(info.Stream);
這樣你可以保留它一個資源,你不必將它複製到磁盤。
我把它封裝在一個用戶控件中,以處理WebBrowser控件的一些問題。 設置Html頁面的'Build Action'屬性,以及將任何嵌入內容設置爲'Content'和'Copy to Output目錄「複製到」如果更新的情況下複製「。 在html中維護任何頁面資源層次結構,就像您通常在服務器上一樣。 在視圖模型,或從代碼隱藏,則只需設置HtmlUri,HtmlString或該控制(也可以關掉煩人點擊聲; O)的HtmlStream特性之一..
實施例的URI:
public Uri DefaultUri
{
get { return new Uri(@"pack://siteoforigin:,,,/Html/h1.html"); }
}
private Uri GetUri(string ItemName)
{
if (string.IsNullOrEmpty(ItemName)) return null;
return new Uri(@"pack://siteoforigin:,,,/Html/" + ItemName + ".html");
}
使用這樣的:
<controls:SmartBrowser x:Name="wbData"
Grid.Row="0"
HtmlUri="{Binding HtmlUri}"
DefaultUri="{Binding DefaultUri}" />
SmartBrowser。XAML:
<Grid>
<WebBrowser Name="wbData" />
</Grid>
器代碼背後:
public partial class SmartBrowser : UserControl
{
#region Constants
private const int Feature = 21;
private const int SetFeatureOnProcess = 0x00000002;
#endregion
#region Api
[DllImport("urlmon.dll")]
[PreserveSig]
[return: MarshalAs(UnmanagedType.Error)]
static extern int CoInternetSetFeatureEnabled(int featureEntry, [MarshalAs(UnmanagedType.U4)] int dwFlags, bool fEnable);
#endregion
#region Fields
private WebBrowser _wbData;
#endregion
#region Constructor
public SmartBrowser()
{
InitializeComponent();
}
#endregion
#region Overrides
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_wbData = (WebBrowser)FindElement("wbData");
if (_wbData != null)
if (DefaultUri != null)
_wbData.Navigate(DefaultUri);
}
#endregion
#region Properties
#region DefaultUri
/// <summary>
/// Gets/Sets
/// </summary>
public static readonly DependencyProperty DefaultUriProperty =
DependencyProperty.Register("DefaultUri", typeof(Uri), typeof(SmartBrowser),
new PropertyMetadata(null, new PropertyChangedCallback(OnDefaultUriChanged)));
public Uri DefaultUri
{
get { return (Uri)GetValue(DefaultUriProperty); }
set { SetValue(DefaultUriProperty, value); }
}
private static void OnDefaultUriChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SmartBrowser sb = d as SmartBrowser;
if (e.NewValue == null) return;
if (sb._wbData == null) return;
Uri htmlUri = e.NewValue as Uri;
sb._wbData.Navigate(htmlUri);
}
#endregion
#region HtmlStream
/// <summary>
/// Gets/Sets
/// </summary>
public static readonly DependencyProperty HtmlStreamProperty =
DependencyProperty.Register("HtmlStream", typeof(Stream), typeof(SmartBrowser),
new PropertyMetadata(null, new PropertyChangedCallback(OnHtmlStreamChanged)));
public Stream HtmlStream
{
get { return (Stream)GetValue(HtmlStreamProperty); }
set { SetValue(HtmlStreamProperty, value); }
}
private static void OnHtmlStreamChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SmartBrowser sb = d as SmartBrowser;
if (e.NewValue == null) return;
if (sb._wbData == null) return;
Stream htmlStream = e.NewValue as Stream;
sb.Navigate(htmlStream);
}
#endregion
#region HtmlString
/// <summary>
/// Gets/Sets
/// </summary>
public static readonly DependencyProperty HtmlStringProperty =
DependencyProperty.Register("HtmlString", typeof(string), typeof(SmartBrowser),
new PropertyMetadata(null, new PropertyChangedCallback(OnHtmlStringChanged)));
public string HtmlString
{
get { return (string)GetValue(HtmlStringProperty); }
set { SetValue(HtmlStringProperty, value); }
}
private static void OnHtmlStringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SmartBrowser sb = d as SmartBrowser;
if (e.NewValue == null) return;
if (sb._wbData == null) return;
string htmlString = e.NewValue as string;
sb.Navigate(htmlString);
}
#endregion
#region HtmlUri
/// <summary>
/// Gets/Sets
/// </summary>
public static readonly DependencyProperty HtmlUriProperty =
DependencyProperty.Register("HtmlUri", typeof(Uri), typeof(SmartBrowser),
new PropertyMetadata(null, new PropertyChangedCallback(OnHtmlUriChanged)));
public Uri HtmlUri
{
get { return (Uri)GetValue(HtmlUriProperty); }
set { SetValue(HtmlUriProperty, value); }
}
private static void OnHtmlUriChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SmartBrowser sb = d as SmartBrowser;
if (e.NewValue == null) return;
if (sb._wbData == null) return;
Uri htmlUri = e.NewValue as Uri;
sb.Navigate(htmlUri);
}
#endregion
#region SoundEffects
/// <summary>
/// Gets/Sets
/// </summary>
public static readonly DependencyProperty SoundEffectsProperty =
DependencyProperty.Register("SoundEffects", typeof(bool), typeof(SmartBrowser),
new PropertyMetadata(false, new PropertyChangedCallback(OnSoundEffectsChanged)));
public bool SoundEffects
{
get { return (bool)GetValue(SoundEffectsProperty); }
set { SetValue(SoundEffectsProperty, value); }
}
private static void OnSoundEffectsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SmartBrowser sb = d as SmartBrowser;
if (e.NewValue == null) return;
if (sb._wbData == null) return;
bool enable = (bool)e.NewValue;
sb.EnableSoundEffects(enable);
}
#endregion
#endregion
#region Methods
private void EnableSoundEffects(bool Enable)
{
try
{
CoInternetSetFeatureEnabled(Feature, SetFeatureOnProcess, !Enable);
}
catch { }
}
private void Navigate(Uri Html)
{
if (Html == null) return;
try
{
_wbData.Navigate(Html);
}
catch { }
}
private void Navigate(string Html)
{
if (string.IsNullOrEmpty(Html)) return;
try
{
_wbData.NavigateToString(Html);
}
catch { }
}
private void Navigate(Stream Html)
{
if (Html == null) return;
try
{
_wbData.NavigateToStream(Html);
}
catch { }
}
#endregion
#region Helpers
/// <summary>
/// Find an element in the visual tree
/// </summary>
/// <param name="name">Element name</param>
/// <returns>Element [object]</returns>
private object FindElement(string name)
{
try
{
if (this.Template != null)
return this.FindName(name);
}
catch { }
return null;
}
#endregion
}
集 「建設行動」 的文件,以 「嵌入的資源」,然後使用此:
webBrowserMain.NavigateToStream(System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("PsHandler.Resources.gnu.html"));
這是怎麼了在項目中尋找:http://i.stack.imgur.com/dGkpH.png
您可能想改爲使用以下方案:
使用下列URI方案:
<WebBrowser Source="pack://siteoforigin:,,,/Resources/page1.html" />
隨着.NET 4.5,這是對我工作的罰款。
_Best_答案爲.NET 4.5 +。打包網址可讓您在.html文件中使用圖片和其他資源。 – 2018-02-23 17:03:35