我在一個專案中有一個 XAML 檔案,我已將其抽象為:
<Grid>
<ContentControl KeyboardNavigation.IsTabStop="False" Content="{Binding }" Grid.Row="8" Grid.ColumnSpan="5" x:Name="Bob">
<ContentControl.Resources>
<DataTemplate x:Key="FooView">
<wpf:ChromiumWebBrowser x:Name="CefBroswer"
Address="{Binding Path=HTML}">
</DataTemplate>
<DataTemplate x:Key="BarView">
<TextBox Text="{Binding Path=Bar}"></TextBox>
</DataTemplate>
</ContentControl.Resources>
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource FooView}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsBar}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource BarView}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</Grid>
此代碼的想法是ViewModel.Bar如果為真則顯示在文本框中ViewModel.IsBar,否則導航到ViewModel.HTML- 顯示 HTML 或可編輯文本框背后有商業原因。
當我想下載檔案時,我試圖讓 CefSharp 瀏覽器以某種方式運行。當我們使用 WebBrowser 時,指向 Word 檔案的鏈接會導致 IE 出現并提供一個打開/保存框。CefSharp 不會在沒有被告知要做什么的情況下處理這個問題。雖然這提供了更多的功能,但這確實意味著我們已經從能夠處理一系列檔案退回到能夠處理 CefSharp 可以本地顯示的內容(即 HTML 和影像)
我已經嘗試了一些我在網上找到的代碼片段來獲取代碼CefBrowser隱藏。我能到達的最接近的是Bob,我找不到FindName上班的方法——我發現的幾乎每個片段都在談論 ListViewItems,以及我沒有的屬性。
我正在嘗試在代碼隱藏中獲取物件,以便可以附加myDownloadHandler到瀏覽器:
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
CefBrowser.DownloadHandler = myDownloadHandler; //This is what documentation says to use for this
DataTemplate bob = Bob.FindResource("FooView") as DataTemplate;
我嘗試過的另一件事是直接在 xaml 中分配處理程式:
<wpf:ChromiumWebBrowser x:Name="CefBroswer"
Address="{Binding Path=HTML}"
DownloadHandler="{Binding Path=myDownloadHandler}">
這讓它感到不安 - 我假設它系結到處理程式的結果,而不是處理程式本身,這讓事情變得混亂
<wpf:ChromiumWebBrowser x:Name="CefBroswer"
Address="{Binding Path=HTML}"
DownloadHandler="MyDownloadHandler">
這也不起作用,并且給出了關于從字串轉換的智能感知錯誤。當然,現在我把它放回去嘗試復制訊息,它沒有顯示它。這與字串和 IDownloadHandler 之間的轉換有關
所以,問題是: 1:有什么辦法可以CefBrowser在后面的代碼中找到嗎?2:有什么辦法可以在 xaml 中附加處理程式?3:有沒有更好的方法可以不破壞功能?
uj5u.com熱心網友回復:
添加加載的事件處理程式:
<wpf:ChromiumWebBrowser Loaded="BrowserLoaded" ...>
轉換sender為 ChromiumWebBrowser 型別并分配 DownloadHandler:
private void BrowserLoaded(object sender, RoutedEventArgs e)
{
var browser = sender as ChromiumWebBrowser;
browser.DownloadHandler = myDownloadHandler;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/422325.html
標籤:
