1

私の目標はこの質問に似て
いますが、それでも私の問題に対する答えは得られませんでした

そのため、レポート テンプレートを設計および生成するための WYSIWYG HTML 編集サポートを備えたアプリを作成する必要があります。上記の質問のように、WPF で WebBrowser コントロールを使用しました。最大の問題は、designMode が on に設定された後、WPF WebBrowser が常に null を HTML 本文に設定することです。そのため、WinForm WebBrowser をアプリにホストします。また、HTML ドキュメントを WebBrowser から処理するように設定または取得するのは非常に困難です。

Q:

  1. WinForm を変更せずに WPF を使用してこれ (WebBrowser コントロールを使用する HTML エディター) を実現する方法はありますか?
  2. または、そうでない場合。WYSIWYG HTML Editor を Visual Editor で作成するための回避策、記事、コード、またはその他のものはありますか?

更新:
MVVM の目的で、これら 2 つの添付プロパティがあります。そのため、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";
            };
        }
    }
}

HTMLソース

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>
4

2 に答える 2

1

WInForms の代わりに、いくつかのオープン ソースの代替手段を試しましたか?

これは相互作用が良く、wpf への JavaScript コールバックも処理できると思います。

http://wpfchromium.codeplex.com/

于 2012-06-27T10:34:21.717 に答える
0

埋め込みリソースを HTML として使用し、次のコードを含む非常に単純な WPF アプリを作成しました。

  Stream s = GetType().Assembly.GetManifestResourceStream("WpfApplication5.HTMLPage1.htm");
  webBrowser1.NavigateToStream(s);
  IHTMLDocument2 doc = webBrowser1.Document as IHTMLDocument2;
  doc.designMode = "On";

コードは期待どおりに機能し、ページのコンテンツを編集できました-あなたが提案したように designMode に設定すると、コードは無効になりませんでした。おそらく、上記のコードから始めて、それが機能することを確認してください。

于 2012-06-27T12:31:22.060 に答える