gpt4 book ai didi

wpf - WPF 中的 WebBrowser 使用 MVVM 模式

转载 作者:太空狗 更新时间:2023-10-29 15:02:17 26 4
gpt4 key购买 nike

我正在尝试使用 MVVM patten 在 WPF 的 WebBrowser 窗口中打开 HTML 文件。

注意:我已经解决了遇到的问题。现在这段代码可以正常工作了。

ViewHTMLPageView.xaml

 <Window x:Class="MyProject.ViewHTMLPageView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProject.Utility"
Title="HTML Report" Height="454" Width="787"
>
<Grid Name="grid1">
<WebBrowser local:WebBrowserUtility.BindableSource="{Binding ReportPage}" />
</Grid>
</Window>

ViewHTMLPageViewModel.cs

 namespace MyProject
{
public class ViewHTMLPageViewModel: ViewModelBase
{
public ViewHTMLPageView()
{
//Testing html page on load
_reportPage = "<table border='5'><tr><td> This is sample <b> bold </b></td></tr></table>";
OnPropertyChanged("ReportPage");
}

private string _reportPage;
public string ReportPage
{
get
{
return _reportPage;
}

set
{
if (_reportPage != value)
{
_reportPage = value;
OnPropertyChanged("ReportPage");
}
}
}
}

WebBrowserUtility.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;

namespace MyProject.Utility
{
public static class WebBrowserUtility
{
public static readonly DependencyProperty BindableSourceProperty =
DependencyProperty.RegisterAttached("BindableSource", typeof(string),
typeof(WebBrowserUtility), new UIPropertyMetadata(null,
BindableSourcePropertyChanged));

public static string GetBindableSource(DependencyObject obj)
{
return (string)obj.GetValue(BindableSourceProperty);
}

public static void SetBindableSource(DependencyObject obj, string value)
{
obj.SetValue(BindableSourceProperty, value);
}

public static void BindableSourcePropertyChanged(DependencyObject o,
DependencyPropertyChangedEventArgs e)
{
var webBrowser = (WebBrowser)o;
webBrowser.NavigateToString((string)e.NewValue);
}
}
}

最佳答案

在 WebBrowserUtility.cs 中,更改以下语句:

using System.Windows.Forms;

using System.Windows.Controls;

现在,对于你的问题 +1,你能告诉我为什么这可以解决你的问题吗?

关于wpf - WPF 中的 WebBrowser 使用 MVVM 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7728707/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com