我遇到的问题是我有一个名为“浏览器”的 WebBrowser 控件。浏览器由 MEF 创建的不同控件填充:
//Apply Button that cretes the HTML that is loaded into the Browser
btnCreateHTMLPreview(object sender, RoutedEventArgs e)
{
//Clears the content of the observable collection
contents.Clear();
//usedControls is an observable collection of all loaded controls in the stack panel
foreach(var control in usedControls)
control.CreateHTML();
}
//Code in the MEF Control that creates the HTML that is will be loaded into the browser
CreateHTML()
{
string tempStuff = null;
tempStuff += "<h1>" + MyControl.lblHeader.Content + "</h1>\n";
tempStuff += MyControl.txtTextGoesHere.Text + "\n";
//Give the HTML back to the Application
parent.AcceptReport(tempStuff);
}
//Application side where it accepts the HTML and loads it into the Browser
AcceptHTML(string passedInContent)
{
//Observable collection that holds the content of all possible MEF Controls
contents.Add(passedInContent);
string tempStuffForBrowser = null;
tempStuffForBrowser = "<html>\n";
tempStuffForBrowser += "<body>\n";
//Add the content that was saved earlier
foreach (var strInd in contents)
tempStuffForBrowser += strInd;
tempStuffForBrowser = "<html>\n";
tempStuffForBrowser += "<body>\n";
Browser.NavigateToString(tempStuffForBrowser);
}
我在上面尽了最大努力尝试提供我如何填充 html 并将其添加回浏览器的内容。如果有任何不清楚的地方,请告诉我。
如果我只添加了一个控件,以便当我单击按钮加载 html 时“内容”中只有一个控件,它工作得很好并加载它。但是,如果我加载了多个控件,我有单击该按钮两次以使其显示。我觉得这非常奇怪。我尝试解决这个问题是在离开活动之前检查浏览器中是否有内容,但我不确定如何执行此操作。如果有人对如何解决此问题有任何建议或任何其他想法,请告诉我。