好的,所以我认为这很简单,即使我之前刚刚使用WPF项目资源文件时也是如此。
当我的应用程序启动时,我想将本地(随应用程序一起提供)html文件加载到Webbrowser控件中。但是我找不到一个简单的方法来做到这一点!有什么想法吗?
答案 0 :(得分:7)
我刚才遇到了同样的问题。我希望能够做到这一点:
<WebBrowser x:Name="myWebBrowser" Source="page.html"/>
但相反,我得到了这个错误:
不允许使用相对URI。 参数名称:source
这太烦人了。相反,我最终得到了一个与你的代码非常相似的解决方案:
myWebBrowser.Source = new Uri(new System.IO.FileInfo("page.html").FullName);
我确信有一些XAML柔术功夫方法来解决这个问题,但我不知道它是什么。 ^ _ ^
答案 1 :(得分:4)
最后通过将文件的构建操作设置为“始终复制”然后使用Environment.CurrentDirectory
来获取应用程序目录来解决这个问题:
string appDir = Environment.CurrentDirectory;
Uri pageUri = new Uri(appDir + "/page.html");
myWebBrowser.Source = pageUri;
这似乎工作正常。
答案 2 :(得分:3)
演出的后期,但当我在寻找同样的事情时,我偶然发现了这一点。让它以更加WPF的方式工作。 WebBrowser有一个方法NavigateToStream(),因此您只需设置资源流。
StreamResourceInfo info = Application.GetResourceStream(new Uri("Page.html", UriKind.Relative));
if (info != null)
myWebBrowser.NavigateToStream(info.Stream);
通过这种方式,您可以将其保留为资源,而无需将其复制到磁盘。
答案 3 :(得分:3)
设置&#34;构建动作&#34;该文件到&#34;嵌入式资源&#34;然后使用它:
webBrowserMain.NavigateToStream(System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("PsHandler.Resources.gnu.html"));
以下是项目的展示方式:http://i.stack.imgur.com/dGkpH.png
答案 4 :(得分:2)
您可能希望改用以下方案:
使用以下URI方案:
<WebBrowser Source="pack://siteoforigin:,,,/Resources/page1.html" />
从.NET 4.5开始,这对我来说很好。
答案 5 :(得分:0)
我将它包装在usercontrol中以处理WebBrowser控件的一些问题。 设置&#39;构建动作&#39; Html页面的属性,以及任何嵌入内容的内容&#39;和“复制到输出目录”&#39;如果更新则复制&#39; 像在服务器上一样维护html中的任何页面资源层次结构。 在viewmodel中,或者从代码隐藏中,您只需设置此控件的HtmlUri,HtmlString或HtmlStream属性之一(您也可以关闭恼人的咔嗒声; o)..
示例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
}