c# - WPF中的Web浏览器控件

标签 c# wpf mvvm webbrowser-control caliburn.micro

我的目标类似于this question
但是我仍然没有得到我的问题的答案

因此,我需要制作一个具有所见即所得HTML编辑支持的应用程序,以设计和生成报告模板。就像上面的问题一样,我在WPF中使用了WebBrowser控件。最大的问题是WPF WebBrowser在将designMode设置为on后始终将null设置为HTML正文。因此,我将WinForm WebBrowser托管到我的应用程序中。从WebBrowser设置或获取要处理的HTML文档非常困难。

问:

  • 是否可以使用WPF在不更改WinForm的情况下使此操作(使用WebBrowser控件的HTML编辑器)发生?
  • 或者,如果不是。是否有任何解决方法,文章,代码或任何其他方法可以使带有可视化编辑器的所见即所得HTML编辑器成为可能?

  • 更新:
    我有这两个附加属性用于MVVM。因此,我可以使用HTMLSource获取/设置HTML,并在应用启动时设置“设计模式”。

    IsDesignMode
    public static readonly DependencyProperty IsDesignModeProperty =
        DependencyProperty.RegisterAttached("IsDesignMode", typeof (Boolean), typeof (WebBrowserHelper),
                                            new UIPropertyMetadata(false, IsDesignModePropertyChanged));
    
    public static Boolean GetIsDesignMode(DependencyObject obj)
    {
        return (Boolean)obj.GetValue(IsDesignModeProperty);
    }
    
    public static void SetIsDesignMode(DependencyObject obj, Boolean value)
    {
        obj.SetValue(IsDesignModeProperty, value);
    }
    
    public static void IsDesignModePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        WebBrowser browser = obj as WebBrowser;
        if (browser != null)
        {
            Boolean designMode = (Boolean) args.NewValue;
            if(designMode)
            {
                browser.LoadCompleted += (s, e) =>
                {
                    var htmlDoc = (s as WebBrowser).Document as IHTMLDocument2;
                    htmlDoc.body.setAttribute("contenteditable", "true");
                    htmlDoc.designMode = "On";
                };
            }
            else
            {
                browser.LoadCompleted += (s, e) =>
                {
                    var htmlDoc = (s as WebBrowser).Document as IHTMLDocument2;
                    htmlDoc.body.setAttribute("contenteditable", "false");
                    htmlDoc.designMode = "Off";
                };
            }
        }
    }
    

    HTMLSource
    public static readonly DependencyProperty HTMLSourceProperty =
        DependencyProperty.RegisterAttached("HTMLSource", typeof (String), typeof (WebBrowserHelper),
                                            new UIPropertyMetadata(null, HTMLSourcePropertyChanged));
    
    public static String GetHTMLSource(DependencyObject obj)
    {
        return (String)obj.GetValue(HTMLSourceProperty);
    }
    
    public static void SetHTMLSource(DependencyObject obj, String value)
    {
        obj.SetValue(HTMLSourceProperty, value);
    }
    
    public static void HTMLSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs args)
    {
        WebBrowser browser = o as WebBrowser;
        if (browser != null)
        {
            browser.NavigateToString(args.NewValue as String);
        }
    }
    

    查看
    <UserControl x:Class="Delay.View.LayoutView"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:browser="clr-namespace:Delay.Helper"
                 xmlns:cal="http://www.caliburnproject.org"
                 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
        <UserControl.Resources>
            <Style x:Key="ButtonStyle" TargetType="Button">
                <Setter Property="Width" Value="100" />
                <Setter Property="Height" Value="40" />
                <Setter Property="Margin" Value="2.5,0" />
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <StackPanel>
                                <ContentPresenter Content="{TemplateBinding Content}"
                                                  TextBlock.FontSize="15" />
                            </StackPanel>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </UserControl.Resources>
        <Grid Background="Lavender">
            <DockPanel>
                <TextBlock HorizontalAlignment="Center" Text="Layout Designer"
                           DockPanel.Dock="Top" FontSize="20" />
                <WebBrowser Name="webBrowser" HorizontalAlignment="Stretch" DockPanel.Dock="Top" Margin="8" Height="435"
                            browser:WebBrowserHelper.HTMLSource="{Binding HtmlPage}" browser:WebBrowserHelper.IsDesignMode="True" />
                <StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom" HorizontalAlignment="Center">
                    <StackPanel Orientation="Vertical" Margin="5,0">
                        <ComboBox ItemsSource="{Binding LayoutTags}" SelectedItem="{Binding SelectedTag}"
                                  HorizontalAlignment="Stretch" Margin="0,5" MinWidth="100">
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock FontSize="12" Text="{Binding TagName}" />
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                        <ListBox ItemsSource="{Binding LayoutValueTypes}" SelectedItem="{Binding SelectedType}"
                                 Width="{Binding ElementName=cmbTag, Path=ActualWidth}" Height="70">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock FontSize="12" Text="{Binding TypeName}" />
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </StackPanel>
                    <StackPanel Orientation="Vertical">
                        <StackPanel Orientation="Horizontal">
                            <CheckBox Name="IsDesignMode" Content="Design Mode" TextBlock.FontSize="12">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Checked">
                                        <cal:ActionMessage MethodName="DesignModeOnOff">
                                            <cal:Parameter Value="{Binding ElementName=webBrowser}" />
                                            <cal:Parameter Value="{Binding IsDesignMode}" />
                                        </cal:ActionMessage>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </CheckBox>
                        </StackPanel>
                        <Button Name="PutComponent" Style="{StaticResource ButtonStyle}"
                             Content="Put" />
                        <Button Name="SaveLayout" Style="{StaticResource ButtonStyle}"
                            Content="Save" />
                    </StackPanel>
                </StackPanel>
            </DockPanel>
        </Grid>
    </UserControl>
    

    最佳答案

    您是否尝试过一些开源替代方案而不是WInForms?

    我认为这具有良好的交互性,也可以将JavaScript回调处理到wpf中。

    http://wpfchromium.codeplex.com/

    关于c# - WPF中的Web浏览器控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11224193/

    相关文章:

    c# - .NET 多线程访问共享登录 session

    c# - 如何在 C# 中将字符串转换为位

    C# 等待和脉冲

    c# - Windows 8 需要创建向导自定义控件

    c# - 如何使自定义 XAML 用户控件可绑定(bind)?

    wpf - 在 MVVM 中,ViewModel 响应 View 中用户操作的最佳方式是什么?

    .net - ViewModel中的ObservableCollections和模型中的IEnumerables?

    c# - C# WPF 窗口中带有一行和一个标签的水平分隔符

    c# - WPF 多复选框选中/取消选中

    wpf - 重构 WPF MVVM 以提高可测试性